“When I turn on concurrency in a loop it totally messes up my variables, how can I use variable and a parallel loop at the same time in Power Automate?”
Using variables in a serial loop is very straightforward. You just initialise the variable, set it to a specific value in the loop, and then use it. That’s the standard setting of the ‘Apply to each’ – process the input array item by item. If you stick to the serial loop, you won’t see any surprises. But once you enable concurrency it might cause a mess. All the items in the input array will be processed at the same time, and all of them will use the same variable. One branch of the parallel loop can set a value in the variable, while another one will read that value. There won’t be any connection between the values. If the value should stay only in the current loop run, you can’t use a simple variable.
Use an array variable to store objects
Since there’s no information which loop added which value, you should add the information yourself. You shouldn’t store only the value, but also the information which loop it belongs to. That means using an array of objects (as when sending multiple reminders) instead of a simple array or a string variable.
Start by ‘Initialize variable’ to create an array variable, and get all the items you want to process.
The ‘Apply to each’ action processing the ‘Get items’ output is the one running in parallel.
As already mentioned, the variable will be shared between all the parallel loops, therefore, you shouldn’t simply ‘Set variable’ to a value. If you do that, you’ll overwrite it over and over again and provide the wrong value to the other loop runs. You should only add new values with the ‘Append to array variable’ action. And each of the values should be an object – containing not only the value, but also the current run information.
When working with SharePoint items, it might be the item ID and the value you need using a JSON object syntax.
{
"ID": "<loop unique identifier>",
"Value": "<the value to store>"
}
For example, to store Titles for all SharePoint items.
Note: it’s just an example to show the underlying principle.
You can see above that now I have 2 pieces of information in the variable. I have the value I want (Value), plus I have the loop information (ID).
Get the value from the variable
All the parallel branches will add new objects in that variable during the loop run. But you’re not interested in all the values, you want only the value relevant to the specific loop. As such, you must extract it from the full variable with the ‘Filter array’ action. That’s where the unique identifier is necessary. Filter from the variable only the objects where the ID is equal to the current loop.
The last step is to take the filtered object and access the value from its JSON.
first(body('Filter_array'))?['Value']
Summary
If you use a variable in a parallel loop, you must remember that it’s a shared variable within Power Automate. One resource that’s shared by all the loop runs. It’s ok if you only read from the variable as all the loops will read the same value. But if you want also to update it, you can’t simply ‘Set variable’. You need the information which loop run did set it so you can later access the right value, and you should only add values, never overwrite. ‘Set variable’ should be reserved only for serial loops without any concurrency.
Every example explanation you do is extremely powerful.
Thank you!
That’s amazing solution. Thanks for sharing.
What if you used compose instead of a variable? Would you run into the same issue?
Hello Andres,
I think it could work with ‘Compose’, but I didn’t try that as I needed a variable in my flow.
Fantastic guide, for anyone else wondering… in the “Filter array” action, the first value’s Expression is “item()[‘ID’]”, not just “item()”, right Tom?
Hello Caspar,
yes, it’s the item()?[‘ID’] as stored in the action note.