Fastest Way To Append To Array In Power Automate (3 Methods)

Fastest Way To Append To Array In Power Automate (3 Methods)

Knowing how to append to an array in Power Automate is useful because you can take a set of individual objects and add them into a single array. Or to speak plainly, you can create a series of rows and combine them into a table. Using the append to an array variable action is the most obvious method but there are other techniques that are much faster.

Table of Contents
• Introduction: The Case ID ArrayMethod #1: Append To Array With VariablesMethod #2: Append To Array Using The Compose ActionMethod #3: Use The Select Action Instead Of Append To ArrayComparing Append To An Array Methods Performance




Introduction: The Case ID Array

We will explore how to use Power Automate to create an array of Case IDs. Each array item has an ID and Name as shown in the table below.

IDName
1Case-0001
2Case-0002
3Case-0003
4Case-0004
5Case-0005
6Case-0006
7Case-0007
8Case-0008
9Case-0009
10Case-0010




Method #1: Append To Array With Variables

The first method we will use to “append to an array” in Power Automate uses variables. We start by initializing an empty array variable named varCaseArray. Then we generate a range of the numbers 1-to-10 and loop over them in an Apply To Each action. Inside of the loop we format the case number and use the Append To Array Variable action to add the result to the array.



Write this code in the Select output from previous steps field of the Apply To Each action to generate an array of the numbers 1-to-10.

range(1,10)



When the flow is run the array is output into a Compose action containing the case id and name.




Method #2: Append To Array Using The Compose Action

The second method to “append to an array” in Power Automate removes variables and the Append To Array Variable action from the flow. Instead, we use Compose as the final action inside the Apply To Each loop to create objects (i.e. individual array items). Then we assemble all of the objects into an array using the another Compose action outside of the loop.



Write this code in the Select output from previous steps field of the Apply To Each action to generate an array of the numbers 1-to-10.

range(1,10)



Then use this expression in the Compose: Case Array Variable Method action to take the Case objects and append them to an array.

outputs('Compose:_Case_Array_Object')



To make the Apply To Each action run faster go to the settings and enable concurrency control. This allows the loop to process several threads at once instead of running them sequentially. We could not do this in the “append to array with variables” action because concurrency cannot be used in loops which contain variables.



Once again, we can run the flow and see an array inside the Compose action containing the case id and name.




Method #3: Use The Select Action Instead Of Append To Array

The third and final method to “append to an array” in Power Automate uses the Select action



Write this code in the Select output from previous steps field of the Select action to generate an array of the numbers 1-to-10.

range(1,10)



In the Map field, use the current item as the id value.

item()



And write this code for the name value to generate a case name in the format of “Case-0001”.

concat('Case-',formatNumber(item(),'0000'))



When we run the flow the Select action outputs an array containing the case id and name just as the other methods using Apply To Each did.




Comparing Append To An Array Methods Performance

Now we know 3 methods we can use to append to an array in Power Automate. But which method should you use? The Select Action method is the fastest, followed by the Compose Action method. The appending to an array using variables method is the slowest.

#MethodDuration (seconds)
1Append To Array With Variables5
2Append To Array Using Compose Action2
3Select Action Instead Of Append To Array<1



Here is screenshot of the flow run used to create the Case ID array with all of the different methods as proof. Remember, the examples we worked through only had 10 records. Performance will improve greatly as the number of records appended to the array becomes larger.





Questions?

If you have any questions or feedback about Fastest Way To Append To An Array In Power Automate (3 Methods) please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest

7 Comments
Oldest
Newest
Inline Feedbacks
View all comments
shalem120
shalem120
4 months ago

Thanks, Matthew, for another great post. These Select magics are pretty cool. The one from last week with validating allowed characters was awesome as well.

In the second method you used the wrong screenshots (the variable is used as the dynamic value for the outer compose action) and you called it Case Array Variable Method, which is confusing because Variable is method 1. But it is less important because the 3rd method is why we are here today…

Luke
Luke
3 months ago

I have a doubt tho, how does the second example guarantee an ordered output?

I’ve tested it myself and I see it does, but I can’t grasp how.

With concurrency set to 1 it is guaranteed due to it cycling as a for loop, but 20 concurrency in this instance it should run them all 10 at the same time, so the 4th could be processed before the 3rd and so on.

L Hewitt
L Hewitt
3 months ago

In #2, I had NO CLUE that the using the outputs of compose outside a loop would create an array – this changes so much!!! It feels so counterintuitive since the actions within a loop are usually untouchable outside of it.

Tomas Lukovics
Tomas Lukovics
1 month ago

Hello Matthew, I looked at this post and you other about apply to each loops 20x faster with Parrarelism, but unfortunately I am not able to find anything relevant.
I tried many many things in my flows, with no avail.

My scenario is: 1 ShP list needs to be copied out to another SP Site Collection.
The issue is I have 1 multichoice lookup column.
After Get Items, the first loop (LoopItems) is created Create Item, since we need all those items coming from source list.

Now after that I need to collect the array of multiple choices (from the lookup).
This creates the second loop (LoopArray). If I use append to array variable, inside the LoopArray, and use the varArray after the LoopArray, the values are wrong. (ofc because it is only 1 Array variable and in the sequential loops of LoopItems it got appended and the Create Item was filled with incorrect array.

The issue is with multiple level nesting.
I cannot use composes instead of variables, because outside the LoopArray, I won’t see them.


I have a second flow which is triggered based on Item created /modified and there it of course works, since the variable value is not owerwritten.

I checked also Pieter’s website but I am pretty desperate, as I cannot find a solution to Create those Items.
Ofc fastest way would be recreate the Source ShP list with something different then a lookup columns 😀 but that is not feasible at this point.

All help is apprecianted.

Thank you.

Tomas

Tomas Lukovics
Tomas Lukovics
1 month ago

Hey Matthew,
since I was not able to reply to my comment awaiting approval I am writing this as second comment. But is basically a solution the issue. 🙂

I was try everything to not go down this road as I thought it will be crazy hard on performance. It isn’t.

So since I could not utilize variable inside multi-level nesting as the array values were re-set, or too many appends at the same time. ( and also in my previous comment the append was a wrong move. I know that now, I mean I knew before, but didn’t realized.
I could not use append inside the second loop because it would hold all the values from the previous iterations. My bad. 🙂 )

I also could not utilized the compose and select action because of the multi-level nesting.

So finally what solved it for me was to create a child flow which is called inside the Main loop for Creating Items. In there I am calling Get Item and creating and array for 1 item only and passing the value back to Parent flow, and this way a Compose can be utilized and everything is working and I am happy.

I must say to create and start using the parent-child flow solution successfully took only few minutes. But the decision to go the parent-child flow solution, took almost 2 days.

Your blog again helped in many many things and is helping for 3 years already. Cat threats are definitely coming this month.

Thank you 🙂