Do you need to loop through an array in Power Automate?
When you retrieve data from other sources in your flow, it will usually be in an array format where each item of the array is a record of key-value pairs.
It’s quite common to want to perform some action for each item in your data array, such as sending an email to each person in the array.
This means you will want to loop through your array.
This post will show you how to loop through an array using the Apply to each action. This action takes an array as input and iterates through each element in the array, allowing you to perform specified actions on each element as needed.
Initialize Array Variable in Power Automate
To loop through an array, you will either need to get an array from an external data source or create your own array.
[
{"first": "John", "email": "jmacdougall@example.com"},
{"first": "Mark", "email": "mwilson@example.com"},
{"first": "Amanda", "email": "athompson@example.com"}
]
This example will use an array from a compose action such as the above and it can be referenced as @{outputs('Array')}
within the flow.
You could also create an array variable.
To start working with array variables you should first initialize the array variable. Initializing a variable allows you to set the variable name, type, and its initial value.
- Add the Initialize variable to your flow. Click on the New step button and select the Initialize variable action from the list. This action lets you create a new variable and set its initial value.
- Provide a unique and meaningful Name for your array variable such as myArray.
- Choose Array from the Type drop-down menu. This indicates that the variable will be an array.
- You can set an initial value for the array with the Value input.
Once you have initialized your array variable, you can use it to loop through an array with the Apply to each action.
How to Loop in Power Automate
Usually, Power Automate will automatically add the Apply to each action to your flows when you try and reference an array within an action input that requires a single value.
But this is not always the case, for example when working with an array created with a Compose action.
This section will show you exactly how to set up your Apply to each loop manually.
Apply to Each Loops
To loop through an array in Power Automate, you can use the For Each loop. This type of loop automatically iterates through all items within an array.
Here’s how to use the Apply to each action.
- Add the Apply to each action to your flow below where your array is defined.
- Add the dynamic array reference to the array inside the Apply to each action in the Select an output from previous steps input.
- Add any actions you want to run for each element of the array inside the Apply to each action.
This loop will now iterate through each item in your array.
Using the Item Function in Your Loop
There are two ways you can reference the current item of the array during your loop.
item()
The item function allows you to get the current item from an array during any repeating action. This can be used in the Apply to each action as well as other repeating actions such as the Select action.
In this example, the item()
expression will return each record in the array.
When you run this, you will be able to see the output for each iteration through the array.
For example, in the first iteration the output is the entire first record {"first": "John", "last": "MacDougall", "email": "jmacdougall@example.com"}
.
item()['email']
Suppose you only want to use the email address from the record in each array item. This is possible by referencing the key in the key-value pair along with the item function.
The above expression will return only the email value in the record for each item in the array.
Using the Items Function in Your Loop
items(<loopname>)
Another approach to access items within an array is using the items function.
The items function allows you to get the current item from an array during any repeating action. This can only be used in the Apply to each action and requires that you reference the name of the loop action <loopname>
.
items('Apply_to_each')
For example, the above expression will access the current item in the loop named Apply to each.
items('Apply_to_each')['email']
Again, you can access the key-value pair when your array contains records with the items function by referencing the key as in the above example. The above expression will return only the email address from the current item during each iteration.
💡 Tip: Using the item instead of the items function means you don’t need to reference the Apply to each name. This will allow you to easily rename your loop or copy and paste it elsewhere without needing to update the expression.
Allow Loops to Run in Parallel
By default, your loop will run in sequence. This means each iteration runs one at a time.
This can result in very slow flows when your array contains a lot of elements.
It is possible to set your loop to run in parallel. This can significantly speed up your flows as it will allow up to 50 iterations to run at the same time.
Here’s how to set your loop to run in parallel.
- Click on the Ellipses icon on the Apply to each loop action.
- Select the Settings option from the menu.
This will open the loop settings menu.
- Turn on the Concurrency Control option.
- Set the Degree of Parallelism as desired to the maximum of 50. Setting this to 50 means the loop will perform 50 iterations at once.
- Press the Done button.
Be careful not to set the parallelism higher than the actions inside your loop allow for.
For example, suppose you want to send an email for each iteration. It might be the case that the email connector you’re using only allows 20 emails to be sent per minute. This will result in failed iterations if the parallelism is too high.
⚠️ Warning: If you are updating a Power Automate variable inside the loop, then you won’t be able to run the loop in parallel mode. The loop needs to run in sequence in order to update the variable.
This option will speed up the loop considerably.
Conclusions
This article has explored how to loop through an array in Power Automate.
The Apply to each action allows you to iterate over an array and process its elements with the item or items function. This is particularly useful when you need to perform actions for each item in your array data.
By default, the Apply to each action will iterate through the array in sequence. You can speed up the loop by allowing the loop to run in parallel.
Are you using loops in your flows? Let me know in the comments!
0 Comments