Showing results for 
Search instead for 
Did you mean: 
Advocate II
Advocate II

Dynamic Loops in PowerApps

PowerApps does not have a loop construct so it's not possible to iterate through a set of values and then perform some action within the loop.
However, PowerApps does have a ForAll function which evaluates a formula for all records of a table.
ForAll can be nested, so we can use a nested pattern to generate collections with quite a large number of rows.
For example, a collection with 10 rows allows us to generate a new collection with 100 rows.
The following code snippet allows us to build a collection with a specified number of rows.
//define a base collection from which you will generate your target collection.
ClearCollect(colBase, [1,2,3,4,5,6,7,8,9,10]);
//specify target rowcount
Set(vMaxRows, 57);
//setup collections to be used as inner and outer counters
ClearCollect(colOuter, colBase);
ClearCollect(colInner, colBase);
Set(vOuterMax, CountRows(colOuter));
//generate target collection
ForAll(colOuter, ForAll(colInner, Collect(colTarget, {RowId: colInner[@Value] + (vOuterMax * (colOuter[@Value] - 1)), Inner: colInner[@Value], Outer: colOuter[@Value]})));
RemoveIf(colTarget, RowId > vMaxRows)
To visualise the above, insert a Data Table into your app and set it's datasource to colTarget and show the columns RowId, Inner, Outer.
I hope the community finds the above useful.
Regular Visitor

How about generate a number from min to max. Say from 5 to 8=[5,6,7,8]. In VB just For x=min to max: next x. In power apps I use timer and many counter. What is the better work around for this.




Helpful resources

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

365 EduCon 768x460.png

Microsoft 365 EduCon

Join us for two optional days of workshops and a 3-day conference, you can choose from over 130 sessions in multiple tracks and 25 workshops.

Users online (4,109)