Declaring and Typing Variables
Every variable in a Power Automate flow must be created with an Initialize Variable action before it can be set or read, and you declare its type up front as String, Integer, Float, Boolean, Array, or Object. Unlike a Compose action's output, a variable can be updated later in the flow with Set Variable, which is what makes variables useful for running totals and accumulated values.
Cricket analogy: It's like declaring a scoreboard's 'total runs' field at the start of an innings at zero, then updating it after every ball rather than recalculating from scratch each time.
Set, Increment, and Append Actions
Beyond the generic Set Variable action, Power Automate offers Increment Variable for adding a number to an Integer or Float, and Append to Array Variable or Append to String Variable for building up collections or text without manually re-writing the whole value each time. These specialized actions are both clearer to read in the flow designer and slightly cheaper than a Set Variable with a concat() or union() expression.
Cricket analogy: It's like a scorer using a dedicated 'add four runs' button rather than typing the whole new total by hand after every boundary.
Variable Scope Inside Apply to Each
A variable's scope is the entire flow, not just the loop it happens to be set inside, which is exactly why variables are the standard way to accumulate a result across Apply to Each iterations — for example, summing an Amount field from every row of a table into a RunningTotal variable. A Compose action, by contrast, only ever holds the output of its own single execution and can't accumulate across iterations.
Cricket analogy: It's like a match's overall run total persisting across every over, not resetting per over, which is exactly why it can track the whole innings.
When Variables Cause Trouble: Concurrency Conflicts
The same flow-wide scope that makes variables useful for accumulation also makes them dangerous inside a concurrent Apply to Each: if twenty iterations run in parallel and each one tries to Increment Variable on the same counter, race conditions can silently drop updates. The fix is either to set Concurrency Control to a Degree of Parallelism of 1, forcing sequential execution, or to avoid shared-state variables in loops altogether and use array-based operations like union() instead.
Cricket analogy: It's like two scorers updating the same paper scoresheet simultaneously without talking to each other — one's update can overwrite the other's, silently losing a run.
[
{
"type": "InitializeVariable",
"inputs": { "name": "RunningTotal", "type": "integer", "value": 0 }
},
{
"type": "IncrementVariable",
"inputs": {
"name": "RunningTotal",
"value": "@items('Apply_to_each')?['Amount']"
}
}
]Initialize every variable your flow will need near the top, even ones you'll only set later — Power Automate requires Initialize Variable to run before any Set/Increment/Append action touches that name, and grouping them makes the flow easier to audit.
Never Increment Variable or Append to Array Variable on the same variable from inside a concurrent Apply to Each — set Degree of Parallelism to 1 first, or you risk silently losing updates to race conditions.
- Variables must be created with Initialize Variable and typed as String, Integer, Float, Boolean, Array, or Object.
- Set Variable overwrites a variable's value; Increment/Append actions update it incrementally.
- A variable's scope is the whole flow, not just the loop it's set inside, which enables cross-iteration accumulation.
- Compose only ever holds its own single execution's output and cannot accumulate across iterations.
- Concurrent Apply to Each loops updating a shared variable risk race conditions.
- Set Degree of Parallelism to 1, or use array operations like union(), to avoid variable race conditions.
Practice what you learned
1. Which action must run before a variable can be used anywhere else in the flow?
2. Which action is purpose-built for adding a number to an existing Integer variable?
3. What is a variable's scope in Power Automate?
4. Why can't Compose be used to accumulate a running total across loop iterations?
5. What is the safest fix for race conditions when incrementing a shared variable inside a concurrent Apply to Each?
Was this page helpful?
You May Also Like
Loops: Apply to Each
How Apply to Each and Do Until iterate over data, including concurrency control and performance trade-offs.
Conditions and Branching
How the Condition and Switch actions let a flow evaluate expressions and branch execution down different paths.
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