Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 38 ยท Actions and Expressions

Variables and Compose Actions in Power Automate

Use Initialize variable, Set, Increment, Append to array and Append to string variables, understand when to prefer Compose, and avoid variable limits inside loops.

Flows often need to remember something: a running total, a list of names, a counter, a status text. Power Automate offers two tools for this. Variables are mutable containers you can change as the flow runs. Compose stores a single value once, and the value never changes afterward.

Variables

All variable actions live in the Variables group.

Action Purpose
Initialize variable Creates the variable with a name, a type and an optional initial value
Set variable Replaces the value
Increment variable / Decrement variable Adds or subtracts a number
Append to string variable Adds text at the end
Append to array variable Adds one item to the end of an array

Types are Boolean, Integer, Float, String, Array and Object.

Rules to remember:

  • Initialize variable must be at the top level of the flow. You cannot place it inside a condition, loop or scope.
  • A variable has one type forever. Setting a string into an integer variable fails.
  • You read a variable with dynamic content or with variables('total').
  • Variable names are case sensitive.

Example: sum of line totals

Imagine an array of order lines. Initialize total as a Float with value 0, loop over the lines with Apply to each, and inside it run Increment variable with mul(item()?['qty'], item()?['price']).

TEXT
Initialize variable   total (Float) = 0
Apply to each         items('...') 
   Increment variable total by mul(item()?['qty'], item()?['price'])
Compose               formatNumber(variables('total'), 'N2')

Here is the same pattern as a stand-in.

Python Stand-in: accumulate a total in a loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compose

Compose (Data Operation group) takes one input, either a value or an expression, and gives it back as its Outputs. Its uses:

  • Test an expression and see the result in run history.
  • Calculate a value once and reuse it in several later steps, instead of repeating a long expression.
  • Build a JSON object by typing JSON into the Inputs box.
  • Hold a constant such as an email address.
TEXT
Compose  (name: Build subject)
Inputs:  concat('Invoice ', triggerBody()?['number'], ' - ', formatDateTime(utcNow(), 'yyyy-MM-dd'))

Later steps use outputs('Build_subject'), or pick the Outputs token.

Variable or Compose?

Variable Compose
Can change later Yes No
Position Initialize at top level Anywhere
Works in parallel loops Risky Safe
Cost Extra actions One action

Use Compose by default. Reach for a variable only when you must update a value repeatedly, as with counters and accumulators.

Variables and loops: the concurrency trap

An Apply to each loop can run iterations in parallel (the default concurrency in the designer is often 20 for newly created loops, depending on version). Parallel iterations that all modify one variable overwrite each other's work, so totals come out wrong. There are two safe options:

  1. In the loop's Settings, turn Concurrency control on and set the degree to 1.
  2. Avoid the shared variable: use Select to compute one value per item, then reduce the result in a later step.

In practice, set concurrency to 1 whenever a loop writes to a variable.

Python Stand-in: why parallel updates lose data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Append to array and string

Append to array variable builds a list as you go, which is handy for collecting ids or names, and Append to string variable builds text such as an HTML table row. Then use Compose with join(variables('names'), ', ') for a readable message.

TEXT
Initialize variable  names (Array) = []
Apply to each  Get items value
   Append to array variable  names  item()?['Title']
Compose  join(variables('names'), ', ')

For HTML, you can send an array of objects to the Create HTML table action instead of building strings by hand (see Arrays, objects and JSON).

Limits and tips

  • A variable holds data in the run's memory; very large arrays slow the flow. Use Select and Filter array to reduce data first.
  • Name variables for what they contain: overdueCount, not var1.
  • Initialize all variables together near the top, so the flow reads like a declaration section.
  • Use Compose with a comment on the card to explain why the value exists.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where must Initialize variable be placed?

    Variables must be initialized at the top level, not inside loops, conditions or scopes.
  2. 2. Which action permanently holds a value that cannot be changed later?

    Compose stores a value once; later steps can read it but not change it.
  3. 3. Why can a total be wrong when a parallel loop increments a variable?

    Parallel iterations read the same old value and overwrite each other's results; set concurrency to 1.
  4. 4. Which variable type can hold a list of items?

    The Array type holds a list; Append to array variable adds items.

Summary

  • Variables are mutable and must be initialized at the top level; Compose holds a one-off value.
  • Use Increment, Append and Set to update variables.
  • Set loop concurrency to 1 when iterations write to a variable.
  • Prefer Compose when a value does not need to change.

Next: Text, dates and time zones