Variables inside Apply To Each steps in Power Automate

For many years I’ve been saying, don’t use variables in Power Automate as it hurts the flow’s performance. Recently it was pointed out to me that variables don’t lock Apply to Each loops. But is it safe to use variables in loops?

My Variables inside Apply to each Test flow

I created a flow that will process an array of 100 items, using an Apply to each step. The following expression is creating 100 items for me:

range(1,100)

Then I’m setting a variable to the current item being processed before I wait a couple of seconds and then set a Compose action to the value of my variable.

Variable used inside an apply to each
Variable used inside an apply to each

Then finally I collect all the values of my Compose action in a second Compose actions.

When I run this flow without setting the concurrency on the Apply to each, I will get the expected result back

[
1,
2,
3,
...
99,
100
]

The expected result when concurrency is not set.
The expected result when concurrency is not set.

However if I switch the concurrency on and set this to 50, I’m getting my performance improvement however I get the unexpected result for free as well.

Set concurrency to 50
Set concurrency to 50

The below screenshot shows the result.

Random numbers generated
Random numbers generated

Ok, so what is happening here?

When I run with a concurrency set to 50, my variable is being set 50 times as the first 50 iterations are running. Then the flow is waiting 1 second in each iteration. During this 1 second my variable will be set 50 times. Then my Compose action inside the apply to each is set 50 times to the then current value of my variable.

[
20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 100, 100, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 20, 20, 20, 20, 100, 20, 20, 20, 20,
20, 20, 20, 20, 20, 20, 20, 20, 80, 20,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79,
79, 79, 79, 79, 79, 79, 79, 79, 79, 79
]

So this is a pretty random result.

Append to array

Now there is a second action that I find commonly used inside apply to each steps. When I ran a similar set with the append to array action instead of the set Variable action I got other unexpected behaviours. I kept my delay in between my append to array variable action and my Compose action to 1 second.


[
  [
    50,    29,    2,    8,    27,    5,    37,    17,    79,    100,    58,    44,    10, ...    
  ],
  [
    50,    29,    2,    8,    27,    5,    37,    17  
  ],
  ...  
]

As I collected the results I found that the arrays would contain a random number of elements during each iteration. As you can see the numbers are added to the array, but inside the apply to each we don’t know what the value of the array is. However, if your aim is to build an array you could use this approach, as long as you don’t use the array inside the apply to each. The earlier mentioned method of collecting arrays using Compose actions is however a lot faster.

My Thoughts

Where in the past I said don’t use variables because it hurts performance. I’m now saying absolutely don’t use variables as you get unexpected results as you might end up with a lot of data corruptions if anybody was to enable concurrency on your apply to each steps.

Should you use compose or variable actions, for me it is still 100% Compose. Have you ever come across a situation where you can’t use compose then please comment on this post. I would be interested to see any situation where you had to use a variable.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains

Subscribe now to keep reading and get access to the full archive.

Continue reading