web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Dynamic Loops in Power...
Power Apps
Unanswered

Dynamic Loops in PowerApps

(14) ShareShare
ReportReport
Posted on by 93
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.
Regards,
Prem_ddsl
Categories:
  • v-xida-msft Profile Picture
    Microsoft Employee on at

    Hi @Prem_ddsl,

    Do you have any other questions about PowerApps?

    Thanks for your sharing, I would share your solution to my colleagues.

    If you have any other questions, please reply here, I would provide a proper workaround for you.

     

    Best regards,

    Kris

  • Rulo_S Profile Picture
    4 on at

    Hi, I need to repeat TWO actions for all the rows of a table.

    It is no possible to do it with FORALL function, so I don't know what to use for it....

    I can do it by pressing a button manually as many times as ROWS but I want to do it for all the rows with only one press button.

    This Calculate de RUNNING  TOTAL for EACH ROW

    For example:

     

    UpdateContext({auxSaldoEfectivo:DateAdd(auxSaldoEfectivo;1)});; UpdateIf(Presu;DateValue(FechaPresu)=(auxSaldoEfectivo); {SaldoE: Sum(Filter(Presu;DateValue(FechaPresu)<=(auxSaldoEfectivo));ResultadoE)})

  • Prem_ddsl Profile Picture
    93 on at

    Hi. I agree that FORALL cannot do a running total because it's updating the entire set as in sql rather than iterating through each row as in a proper loop.

    Since running total is dependent on the last row's value, I think you need to update your running total in the same block of code which is patching the new row. So you end up with:

    1. fetch last row

    2. calculate running total for new row

    3. patch new row

     

    Hope that idea makes helps.

    Prem

  • Rulo_S Profile Picture
    4 on at

    GREAT help...I solved that with: forall...patch....lookup...sum...filter

    Many Thanks Prem_ddsl

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    @Prem_ddsl this is awesome, thank you!

     

    I've adjusted it to take dynamic values of vMaxRows and have RowId start from a dynamically set value and it works a treated. I'm using it to collect missing records from a Sharepoint List data grab and matching RowId values to the SP ID values. 

     

    Cheers again! 

     

    🙂 

  • athira_ajith Profile Picture
    2 on at

    How I get the current item in a forall loop

  • CalfordMath Profile Picture
    131 on at

    I think you should be able to simply reference the field names of your collection directly in the.  Perhaps I'm not understanding your situation.  Here's a snippit from the ForAll documentation:  "Fields of the record currently being processed are available within the formula. You simply reference them by name as you would any other value. You can also reference control properties and other values from throughout your app. " 

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    1. If else 

    2. Loop

    3. A simple loop is way more complex so what is gained here 

    4. JavaScrpt is Native to the browser and it can loop 

    5. why remove or reduce what is available

    6. why obfuscate it

    7. Productivity, seriously folks its non productive

    8. Problem with statment based code is as soon as it becomes moderately complex it becomes extremely dificult to debug

    9. There is very good debugging in JavaScript

    10. I have seen time and time again largish things built in these types of interfaces they are less maintainable and some times requires woo's or 1000's of lines of statment based code where a simple loop would have reduced the complexity greatly

    11. Have a good hard think 

     

     

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    1. If else 

    2. Loop

    3. A simple loop is way more complex so what is gained here 

    4. JavaScrpt is Native to the browser and it can loop 

    5. Why remove or reduce what is available

    6. Why obfuscate it

    7. Productivity, seriously folks its non productive

    8. Problem with statement based code is as soon as it becomes moderately complex it becomes extremely dificult to debug, because its just a very long errant string

    9. There is very good debugging in JavaScript

    10. I have seen time and time again largish things built in these types of interfaces they are less maintainable and some times require 100's or 1000's of lines of statement based code where a simple loop would have reduced the complexity greatly

    11. Have a good hard think 

     

     

  • pwak Profile Picture
    2 on at

    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.

     

    Thanks,

    Noel

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 354 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 320

#3
Mohsin Ali Profile Picture

Mohsin Ali 246

Last 30 days Overall leaderboard