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!

You will also like the below posts

Debajit Dutta
Business Solutions MVP