Compose or variable in Power Automate: which should you use?
Use Compose for a value you set once and read later. Use a variable only when the value must change during the run, such as a counter or a running total.
What is the difference between Compose and a variable?
A Compose action holds one value or the result of one expression. You cannot change it after it runs. A variable is a named store that you create with Initialize variable and update with other actions.
A variable has a type, such as string, integer, array or object. A Compose has no type. It keeps whatever you give it.
When should you use Compose?
- You calculate a value once, such as a date, a joined text or a cleaned email address.
- You want to test an expression and read the result in the run history.
- You need to hold a block of JSON to pass to a later action.
- You do not need to change the value later.
When should you use a variable?
Use a variable when the value changes while the flow runs. Common cases are a counter in a loop, a running total, a list that grows with each item, and a status text you update as steps finish.
Variables have their own actions: Set variable, Increment variable, Decrement variable, Append to string variable and Append to array variable. Compose has no matching actions.
How do you read each value in a later action?
For a variable, use the dynamic content item with the variable name, or write variables('Total'). For a Compose, use the Outputs item, or write outputs('Compose').
Rename each Compose to say what it holds, such as Compose due date. The expression then reads outputs('Compose_due_date'), which is easier to follow.
Why can you not add Initialize variable inside a loop or condition?
Initialize variable works only at the top level of the flow. Microsoft says you can declare variables only at the top level, not in scopes, conditions or loops. That rules out Condition, Switch, Scope and Apply to each. Add it near the start, then use Set variable inside the loop.
A Compose has no such limit. You can add it anywhere, which makes it handy for a temporary value inside a branch.
Does a variable work with Apply to each concurrency?
Not safely. When you turn on concurrency, several loop passes run at the same time and can overwrite one shared variable. Results can differ from run to run.
Microsoft warns that setting a variable inside a parallel loop can give unexpected results. Turn concurrency off, or set Degree of parallelism to 1, or build the result with Select or Filter array instead. See our guide on Apply to each concurrency.
Which one is faster or cheaper?
Both count as one action in the run, and neither adds a license cost on its own. The choice is about clarity, not speed.
Fewer variables make a flow easier to read. If you can solve a step with Compose, do that.
What should you check when a value looks wrong?
- Open the run history and select the Compose or the Set variable action.
- Read the output. A null or empty value points to the step before it. See Invalid type, expected string but got null.
- Check the variable type. An integer variable cannot take text.
- For an expression error, see InvalidTemplate.
FAQ
Can I change a Compose output later?
No. A Compose is set once. Create a new Compose or use a variable.
Can a variable hold a list?
Yes. Use an array variable and the Append to array variable action.
Do I need a variable to test an expression?
No. Put the expression in a Compose and read the output in the run history.
Sources
- Use data operations in Power Automate (Compose)
- Create variables to store values in Azure Logic Apps
- Work with variables in Power Automate