“I’m adding multiple attachments to an email but the appending loop seems to be quite slow, how can I make loops faster in Power Automate?”
As already explained in previous articles, if your flow is slow, it’s probably due to unnecessary loop(s). An ‘Apply to each’ that can be often replaced with a select or a filter. But in some situations you still need a loop. For example, if there’s another action involved, e.g. ‘Get file content’ when preparing email attachments. Can these loops be at least a little faster?
Enable parallelism
If you really need a loop, one way to make it faster is to turn on parallelism. Go into the ‘Apply to each’ settings under the three dots…
… and enable the Concurrency control. Move the slider to the right to enable the maximum, 50 parallel branches running at once.
With this setting it’ll start processing all the items in the input array at the same time. Just be careful about using variables in parallel loops.
Use ‘Compose’ instead of variables
The other improvement comes with the use of ‘Compose’ action instead of a variable. If you used variable, you’d probably append to that variable in the loop and use it later in the flow.
Yet you don’t need a variable. You can use just the ‘Compose’ action where you define the input for each loop…
…and collect the composed output after the loop with an expression. You must always use an expression as it’s not something the designer will offer among the dynamic contents.
outputs('Compose')
This approach will give you the same result as a variable, but in a faster way. Use it later in the flow in the same way as you’d use the variable, in this example to add the email attachments.
Summary
If there’s no way to remove loops from your Power Automate flow, yet you need them to run faster, you can try one of the solutions above. If you enable parallelism it’ll process up to 50 loop runs at the same time, giving a significant performance boost. And if you use the ‘Compose’ workaround instead of variables you can improve it even more.
Current implementation does not support accessing Compose like that in a nested Apply to Each.
Hi,
Firstly I would like to complement you on your blog and newsletter. They have greatly helped my team and I implement Power platform solutions in our organisation, and we have borrowed from a lot of the approaches you describe.
I am writing today because we have noticed that the behaviour of the ‘outputs’ function described in this post has become unstable recently. We have noticed this week that occasionally it will not append all elements to the output array. The items in the loop look fine, but the outputs expression only grabs the last item, or only the first and last… We have seen different cases and haven’t yet been able to establish real pattern, although we think it has something to do with the difference in the duration of the different iterations of the loop. The first examples we found had one iteration that took four days to complete, while the rest of the iterations only took a couple of hours. I am currently running some tests, but I can already see that even when one iteration has a duration of one day, and the others only a couple of seconds, the result is not as expected.
I was wondering if you have previously come across any issues like this or if you are aware of any limitation regarding the maximum duration?
Thanks,
Colum
Hello Colum,
I didn’t encounter such behaviour and I’ve been using it in many of my flows, but probably none of them is taking hours/days in the loop. If it’s doing some simple task then I’d expect it’s some issue on Microsoft side.
When I built flows that were running for long in the past (e.g. parallel approvals in a loop) I think I used appending variables as in that case the performance didn’t matter.