Skip to main content

Notifications

Community site session details
Power Apps - Building Power Apps
Answered

Performing a FOR loop in PowerApps

Like (6) ShareShare
ReportReport
Posted on 14 Jan 2020 15:51:19 by

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")
 )

 

  • Community Power Platform Member Profile Picture
    on 18 Dec 2022 at 20:52:55
    Re: Performing a FOR loop in PowerApps

    Thanks. Its the first time I had need of a loop, so I wouldn't know how long the Sequence function has been around, but it does the trick quite nicely. I had to make a horizontal gallery that acted as a timeline for a project. To do that, I needed to create a record for every day in the project scope. This worked great: sequence DateDiff +1. Then stet a variable for the day defore you want to start your timeline. Run it in your ForAll statement and you're in business. Throw some formatting in there to pick up milestone dates and you have a beaufiful thing.  

  • Craig_Humphrey Profile Picture
    166 on 18 Dec 2022 at 19:17:24
    Re: Performing a FOR loop in PowerApps

    Good find! That's got to be a new function!

  • Community Power Platform Member Profile Picture
    on 16 Dec 2022 at 15:58:16
    Re: Performing a FOR loop in PowerApps

    Here.  This is just the thing you need: 

     

    Sequence function in Power Apps - Power Platform | Microsoft Learn

     

     

  • joshmyg Profile Picture
    2 on 21 Nov 2022 at 05:52:23
    Asking question about loop

    How to write this kind of nested loops in PowerApps

     

    Main loop -         1 2 3 4 5 6 7 8 9 10 ... 123 124 125 126 127 128   129   130  131 ...

    Backend loop -    1 2 3 4 5 6 7 1 2 3  ...  1     2       3     4     5     6   7          1       2  ....

     

    Please also give me some references

  • Richard_Ogoma Profile Picture
    8 on 04 Nov 2022 at 11:04:45
    Re: Performing a FOR loop in PowerApps
    ClearCollect(LoopMaster, 
     RenameColumns([0,1,2,3,4,5,6,7,8,9],"Value","Index"))

    Correction: To create the 'Loop' collection....

  • Richard_Ogoma Profile Picture
    8 on 03 Nov 2022 at 19:23:00
    Re: Performing a FOR loop in PowerApps

    This was surely written in love. So brilliant ☺️

  • JonP2 Profile Picture
    2 on 04 Aug 2022 at 10:36:07
    Re: Performing a FOR loop in PowerApps
    • Open an excel spreadsheet.
    • type “1” in cell A1
    • type “=A1+1” in cell B1
    • with B1 selected, Shift+Right Arrow to select the remainder of row 1
    • Ctrl+R to fill the ++ integers across row 1
    • file > save as > comma separated values CSV
    • open the CSV in any text editor
    • copy and paste all (or what you need) of the CSV contents between the array brackets “[]” in PowerApps

     

    Should take less than a minute to produce what’s needed.

  • MarcCoton Profile Picture
    2 on 26 Jan 2021 at 09:33:30
    Re: Performing a FOR loop in PowerApps

    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

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  • 007James_Bond Profile Picture
    58 on 29 Jul 2020 at 06:29:47
    Re: Performing a FOR loop in PowerApps

    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>

  • Community Power Platform Member Profile Picture
    on 09 Mar 2020 at 19:52:24
    Re: Performing a FOR loop in PowerApps

    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}))

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,743 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,079 Most Valuable Professional

Leaderboard