Microsoft Power Automate

Resolved – Avoid variables in Apply To Each steps in Power Automate

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.

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.

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

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:

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')))

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.

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.

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

Share
Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

Recent Posts

Create multi user real time apps in Power Apps

When multiple users edit data and you want your app to show real time data,…

2 weeks ago

Sort by Title in your Highlighted Content web part in SharePoint

Yesterday I was asked about how to sort Documents in the Highlighted Content web part…

2 weeks ago

Copy large files from SharePoint to Azure Blob Storage using Power Automate

When you try to copy large files from SharePoint to Azure Bob Storage with Power…

2 weeks ago

Sort an array by dates in Power Automate

When we use the Sort function to sort arrays in Power Automate, it is soo…

3 weeks ago

New! Format data by examples in Power Automate

This feature "Format data by examples" will kill one of my most popular posts! How…

1 month ago

Named formulas in Power Apps using App.Formulas

Do you use the App onstart to set a lot of variables? And then do…

1 month ago
%%footer%%