Iterating Arrays with Apply to Each
The Apply to Each action is Power Automate's core iteration construct: point it at any array — such as the rows returned by a SQL query or the attachments on an email — and it runs its inner actions once for every item in that array, exposing the current item through the items() expression.
Cricket analogy: It's like a scorer going through every ball of an over one at a time, updating the tally for each delivery before moving to the next — Apply to Each processes array items the same way.
Concurrency Control and Performance
By default, Apply to Each runs iterations concurrently up to 20 at a time, which speeds things up but means the order actions complete isn't guaranteed. The Settings pane's Concurrency Control lets you set the Degree of Parallelism anywhere from 1 (fully sequential) up to 50, and dropping it to 1 is often necessary when later steps depend on the previous iteration's result.
Cricket analogy: It's like running twenty net bowling stations simultaneously before a match instead of one bowler bowling to every batter in turn — faster, but you lose strict order.
Do Until: Condition-Based Looping
Unlike Apply to Each, which iterates a known array, Do Until repeats a block of actions until an expression becomes true or a limit is hit, making it suited for polling scenarios like waiting for an approval status to change. By default it caps out at 60 iterations or one hour, both adjustable in the action's Settings, so a poorly bounded polling loop won't run forever.
Cricket analogy: It's like a bowler continuing to bowl bouncers at a stubborn tailender until either he's dismissed or the over limit is reached, whichever comes first.
Nested Loops and Filtering Before You Iterate
Looping inside a loop multiplies action executions fast — 50 outer items times 20 inner items is 1,000 runs — so it pays to shrink the array first with a Filter Array action rather than looping over everything and skipping items with a Condition. This cuts run time, connector API calls, and the flow's consumption cost simultaneously.
Cricket analogy: It's like a selector shortlisting only players with a strike rate above 140 before running detailed video analysis on each, instead of analyzing the entire domestic roster.
{
"type": "Foreach",
"foreach": "@body('Get_rows')?['value']",
"runtimeConfiguration": {
"concurrency": { "repetitions": 1 }
},
"actions": {
"Increment_RunningTotal": {
"type": "IncrementVariable",
"inputs": {
"name": "RunningTotal",
"value": "@items('Apply_to_each')?['Amount']"
}
}
}
}Filter Array before Apply to Each whenever possible — trimming a 500-row array down to the 12 rows you actually need cuts connector calls, run time, and consumption cost all at once.
Incrementing or appending to the same variable from inside a concurrent Apply to Each can silently drop updates due to race conditions. Set Concurrency Control's Degree of Parallelism to 1, or restructure the logic to use Select/union() instead of a shared variable.
- Apply to Each iterates a known array, exposing the current item via items('Apply_to_each').
- Do Until repeats until an expression is true or a limit is hit (default 60 iterations / 1 hour).
- Default concurrency runs up to 20 iterations in parallel, which can reorder completion but improves speed.
- Set Degree of Parallelism to 1 when a later iteration depends on the result of an earlier one.
- Filter Array before looping to reduce the number of iterations and API calls.
- Shared variables updated inside a concurrent loop are prone to race conditions.
Practice what you learned
1. What is the default maximum degree of parallelism for Apply to Each?
2. Which action is best suited for repeatedly polling an API until a status changes?
3. What is the default iteration cap for a Do Until loop?
4. Why should you Filter Array before an Apply to Each rather than filtering inside the loop with a Condition?
5. What is the main risk of updating a shared variable inside a concurrent Apply to Each?
Was this page helpful?
You May Also Like
Conditions and Branching
How the Condition and Switch actions let a flow evaluate expressions and branch execution down different paths.
Variables in Flows
How to declare, set, and accumulate values in flow-scoped variables, and where they go wrong inside loops.
Working with Data Operations
How Compose, Parse JSON, Select, Filter Array, and table actions reshape data inside a flow without external calls.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics