Skip to content

Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Problems
  • Triggers
  • Application specific solutions
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
Menu

Using variable in a parallel loop in Power Automate flow

Posted on December 29, 2021December 29, 2021 by Tom

“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.

Power Automate variable parallel loop

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']
Power Automate variable parallel loop

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.


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

2 thoughts on “Using variable in a parallel loop in Power Automate flow”

  1. Kell says:
    January 3, 2022 at 5:15 pm

    Every example explanation you do is extremely powerful.
    Thank you!

    Reply
  2. Linn Zaw Win says:
    April 4, 2022 at 9:24 pm

    That’s amazing solution. Thanks for sharing.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Now available:

The Ultimate Power Automate expressions cheat sheet
Equip yourself with the tool to translate your thoughts into Power Automate expressions!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Format results of date calculation in SharePoint columnJune 29, 2022
  • Why is your Power Automate flow creating duplicatesJune 26, 2022
  • How to create a unique identifier in your Power Automate flowJune 22, 2022
  • How to assign custom SharePoint permission level with Power AutomateJune 19, 2022
  • Remove permissions from a specific SharePoint user with Power AutomateJune 15, 2022

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

© 2022 Let's POWER Automate | Powered by Superbs Personal Blog theme