cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Prem_ddsl
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.
Regards,
Prem_ddsl
10 REPLIES 10
v-xida-msft
Community Support
Community Support

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

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Rulo_S
New Member

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

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

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

Many Thanks Prem_ddsl

 

Eelman
Community Champion
Community Champion

@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! 

 

🙂 

How I get the current item in a forall loop

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

Anonymous
Not applicable

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 

 

 

Anonymous
Not applicable

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 

 

 

Helpful resources

Announcements
October Events

Mark Your Calendars

So many events that are happening this month - don't miss out!

Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

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