Resolved - Avoid variables in Apply To Each steps in Power Automate Microsoft Power Automate avoid variables in apply to each steps

Yesterday I was asked about how to avoid variables when you want an index number of the item processed by an apply to each step.

Variables or Compose?

The variables or compose question is quite an old question in the Power Automate world. The main issue with variables is that inside an apply to each variables lock the processing of items by the Apply to each. This disables the possibility to run the processing in parallel using the Concurrency settings on an apply to each.

And when when I looked at some forum posts the only solution given was to use variables.

Use variables to get iteration ID inside an apply to each
Use variables to get iteration ID inside an apply to each

https://powerusers.microsoft.com/t5/General-Power-Automate/How-to-get-the-numbered-iteration-in-an-Apply-To-Each-function/td-p/165872

As also mentioned in the above community forum post using variables is the only known option. (up to now! Just keep reading). But we want to avoid variable when performance is important and we have a lot of items to process. Make that flow that runs for hours run within seconds!

Avoid variables

A couple of approaches that you could consider:

  1. The function iterationIndex does exist but that only works on Do until loops and not on apply to each loops.
  2. Adding a column to each item you can’t do as there is no other way of counting the items while processing the items

Making it all work!

I’m going to start with a simple flow. In my case I use a Compose action to build an array.

Compose action with 4 items
Compose action with 4 items

And as I run the flow, I can see the objects in my array:

Compose action with 4 items in the flow run output
Compose action with 4 items in the flow run output

If I process this array in an apply to each, I will have the starting point of my problem. I will not be able to get to the 1 – 4 number as displayed in the apply to each step:

Running through the 4 items in an apply to each
Running through the 4 items in an apply to each

But how about if I processed the IDs? So I would create an array that just contains the IDs using the range function.

range(0,length(outputs('Compose')))
Avoid variables in apply to each steps
Avoid variables in apply to each steps

When I run the above flow I will find a list of numbers. In my case I generated the numbers 0-3. The ID that I want is 1-4. But you will find out shortly why I’m doing this.

Listing the IDs
Listing the IDs

In my apply to each I will now take the array of IDs rather than the items that I want to add the ID to.

Notice that the ID is created using the following expression:

Add(items('Apply_to_each_2'),1)

And the item is selected form my array of items with the following line:

outputs('Compose')[items('Apply_to_each_2')]

The arrays start with item 0 hence the list of IDs starting with 0 as well.

Create an iteration ID without variables in Apply to each
Create an iteration ID without variables in Apply to each

And now finally, I could take the list of items using Pieter’s method so that I get a single array with IDs:

Pieter's method to collect all the items
Pieter’s method to collect all the items

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

Leave a Reply

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

Discover more from SharePains by Microsoft MVP Pieter Veenstra

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

Continue reading