Skip to main content

How to Add Items to an Array Variable in Power Automate

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

An array variable holds a list that a flow builds as it goes. Create it once with Initialize variable, type Array and value [], then add to it with Append to array variable wherever you find something to keep, usually inside a loop. Read the whole list with variables('Name').

Microsoft's variables article describes the action: it appends a value as the last item in the array, and the value can have any type.

The example is Get Waiting Approvals, a flow of the Overtime Timesheet app. It goes through the departments and keeps the ID of each one the person running it approves.

Prerequisites​

Step 1: Initialize the Variable as an Empty Array​

Add Initialize variable at the top level of the flow. Microsoft's article says you can declare variables only at the global level, not within scopes, conditions, and loops. I named the action Initialize MyDepartmentIds and set:

FieldValue
NameMyDepartmentIds
TypeArray
Value[]

The Initialize MyDepartmentIds pane with Name MyDepartmentIds, Type Array and Value [], next to the canvas with the trigger, Get the caller and Initialize MyDepartmentIds

[] is an empty list. Value is optional, but the article recommends setting it so you always know the start value for your variable. Here, a person who approves nothing ends up with the empty list.

Step 2: Append Inside the Loop​

Where the flow finds a value to keep, add Append to array variable. In the example, it's in the True branch of a condition inside For each department. I named it Add the department:

Add the department → Value
items('For_each_department')?['ID']

The Add the department pane with Name MyDepartmentIds and Value set to the expression token items(...), next to the canvas showing For each department containing Approver emails and Check the caller approves it, with Add the department in its True branch

Step 3: Read the List After the Loop​

After the loop, variables('MyDepartmentIds') is the whole list. The example uses it to keep only the overtime entries from those departments (How to Filter an Array by Two Conditions in Power Automate):

Is this department in the list?
contains(variables('MyDepartmentIds'), int(string(item()?['DepartmentId'])))

The Result​

For an approver of one department, Radiology, the variable held one ID after the loop: [2]. For someone who approves nothing, nothing is appended, and it stays [].

Keep the Loop Sequential​

The same article warns: If you use variables in the apply to each loops, you must run the loop iterations sequentially if it's important for your loop variables to return predictable results. Loops run one at a time unless you turn on Concurrency control in the loop's settings, so leave it off for a loop that appends to a variable.

Common Mistakes​

MistakeWhat happensDo this instead
Putting Initialize variable inside a loop or conditionVariables can be declared only at the top levelInitialize it before the loop
Leaving Value emptyThe start value isn't statedValue []
Concurrency on a loop that appendsUnpredictable resultsLeave the loop sequential
Comparing a SharePoint number with the IDscontains() can miss itFixed: contains() Returns False for a SharePoint Number in Power Automate

Troubleshooting​

SymptomLikely causeFix
The variable isn't in the Name listIt isn't initialized, or isn't an arrayInitialize it as Array above the append
The array is empty after the loopThe condition was never trueCheck the condition's inputs in the run
An item appears twiceThe append runs on more than one pathAppend only in the branch that should add

FAQ​

Should I use Select instead?​

If you want one value from every item, yes: Select does it in one action. An array variable fits when only some items qualify, and deciding takes several steps, like the condition here.

How do I see what's in the variable during a run?​

Open the run and select the Append to array variable action: its inputs show the value added on each pass (How to See a Flow's Run History in Power Automate).

Conclusion​

Initialize an Array variable with [] at the top level, append to it with Append to array variable where the flow finds a value, and read the list with variables(). Keep the loop sequential.

Additional Resources​