cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
EricLott
Resident Rockstar
Resident Rockstar

Performing a FOR loop in PowerApps

Until FOR loops are added in PowerApps, I have a workaround that I've been using and it's really come in handy.

I start by adding a collection in my App's OnStart property (I'll show at the end how to do this in Flow, which I prefer).

I've attached a template for this from 0-1000 (For.txt at the bottom)

 

ClearCollect(Loop,
{Index:0},
{Index:1},
{Index:2},
{Index:3},
...
{Index:100}
)

 

 You can now perform a FOR loop by filtering this collection. For example, if I want to get a collection of dates that are 7 days from a selected date, I can do this:

 

Clear(NextDates);
ForAll(Filter(Loop,Index<=7),
Collect(NextDates,
 {
     Date:DateAdd(DatePicker1.SelectedDate,Index,Days),
     DaysSincePickedDate:Index,
     DayOfWeek:Weekday(DateAdd(DatePicker1.SelectedDate,Index,Days))
}
))

 

I get this result:
2020-01-14 08_52_38-Window.png\

Remember that the numbers don't have to be hardcoded. You can use CountRows(),a numeric input, or any other integer to drive this code.

The shell of the FOR loop looks like this:

 

Set(i,0);
ForAll(
Filter(Loop,Index<=7,Index>=i),
//Code
)

 

This would be the equivalent to something like this:

 

for (i = 0; i <= 7; i++)
{
 //code
}

 

To clean up the Loop collection, I use Flow. Here's what that solution looks like:

for.png

I can now use this code to get my Loop collection (I like to rename the column to Index, since Value is the generic term from Flow)

 

ClearCollect(Loop,RenameColumns(For.Run(1000),"Value","Index"));

 

That will return a collection of 1000 rows without all the manual coding! This Flow is pretty slow, so you can also setup this logic to only load it once for each user (this saves the collection to memory on mobile):

 

LoadData(Loop,"Loop", true );
If(CountRows(Loop)=0,
  ClearCollect(Loop,RenameColumns(For.Run(100),"Value","Index"));
  SaveData(Loop,"Loop")
  )

 

12 REPLIES 12
Anonymous
Not applicable

True. And you wouldn't necessarily need to bother with incrementing since you already have the loop number to either use or operate on. Of course a count table inside the loop would work too.  You could use the below code (with what I posted earlier) to make your number list real quick.

 

 

 

Collect(countTable, {nextNum: 0});

ForAll(FirstN(alongtable, 10),

Collect(countTable, 

{nextNum: Value(Last(countTable).nextNum) + 1}))

 

 

007James_Bond
Helper I
Helper I

Hi.

 

I have similar kind of requirement to find users consecutive months.

 

We have a table in sharepoint that have ID,Name,Months column.

 

Let say find the 5 consecutive month of users but always value should dynamic .

 

please suggest can we apply loop>

MarcCoton
New Member

Another option for creating an Index of numbers for managing your processing:

You can add an index column to any collection using these functions:

ClearCollect, AddColumns, CountIf, ForAll, Table

 

 

//Create a collection containing my shopping list
ClearCollect(colShopping1, Table({List: "Apples"},{List:"Bananas"},{List: "Cherries"}));

//Append two columns to the collections
ClearCollect(colShopping2,AddColumns(colShopping1,"Processed",false, "Index", 0));

 

 

Shopping2.png

 

Then use ForAll to loop through each record, updating the Index value with the CountIf running total, as follows:

 

//Process each row in turn using ForAll, Adding in the count of those already true and then updating Processed to be true (for next time around)
ForAll(colShopping1, Patch(colShopping2, ThisRecord, {Index: CountIf(colShopping2,Processed=true), Processed: true}));

 

 

Shopping2b.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (3,022)