How to write nested For loop in Power Apps Canvas apps

I am back with another blog on Canvas apps and this time it’s about on how to write nested loops inside Power Apps canvas apps. However if you are having this requirement, the blog might just be the time saver for you.

So let’s get started. How do we write For loops inside Canvas apps? Anyone working on canvas apps I guess is aware of the same. Using the popular ForAll construct.

But with nested forall, there is a problem. Let’s check the usual issue with the nested ForAll statements.

Let’s take a very simple requirement. Suppose we want to print the output in the following format.

AA, AB, AC, AD, AE
BA, BB, BC, BD, BE
.
.
EA EB EC ED EE

Writing a for loop is the most obvious thing that come to your mind. So let’s write the ForAll statement to print this.

Concat(ForAll(Sequence(5, 65, 1),
    Concat(ForAll( Sequence(5, 65, 1), Char(Value)), Value)), Value)

65-69 is the ASCII representation from A to E.

We all know we can access each item of a ForAll statement using the ThisRecord keyword. But ThisRecord is scope based. In other words, in the above formula, we won’t be able to access each item of the outer loop. Hence can’t print the desired results.

But how do we solve this problem? Here in comes the alias or should I say the AS keyword in PowerApps. The scoping issue can be easily resolved in the above formula by simply using an alias for the outer forall statement. Let’s see the modified formula below.

Concat(ForAll(Sequence(5, 65, 1) As OuterLoop,
    Concat(ForAll( Sequence(5, 65, 1), Char(OuterLoop.Value) & Char(Value) & "," ), Value)), Value)

See how easy it is. And the great part is it can be applied to any levels of nesting. And with this we get the desired result to print for our scenario as well.

Isn’t that wonderful? Hope this helped!

For more great blogs click here

About the Author:

Debajit Dutta is a five times Microsoft Business Solutions MVP and Microsoft Certified Trainer (MCT) with 14 years of experience working with Dynamics CRM platform and its related technologies.

Using his experience delivering complex CRM implementations for Microsoft, PwC, SanDisk, and Western Digital, Debajit runs CRM consulting firm XrmForYou and leads its product development and consulting streams. He also has extensive experience in training, and has delivered numerous corporate training sessions and Microsoft workshops.

Debajit is a huge contributor to the CRM community. He runs a popular blog, packed with information about Dynamics 365 Portals, Azure Integration, Microsoft flows, PowerApps and SharePoint, and also regularly shares advice on platforms such as Dynamics 365 Facebook and LinkedIn groups.

Reference:

Dutta, D. (2022). How to write nested For loop in Power Apps Canvas apps. Available at: https://debajmecrm.com/how-to-write-nested-for-loop-in-power-apps-canvas-apps/ [Accessed: 13th April 2022].

Share this on...

Rate this Post:

Share:

Topics:

PowerApps

Tags: