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 Array
โข Method #1: Append To Array With Variables
โข Method #2: Append To Array Using The Compose Action
โข Method #3: Use The Select Action Instead Of Append To Array
โข Comparing 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.
ID | Name |
1 | Case-0001 |
2 | Case-0002 |
3 | Case-0003 |
4 | Case-0004 |
5 | Case-0005 |
6 | Case-0006 |
7 | Case-0007 |
8 | Case-0008 |
9 | Case-0009 |
10 | Case-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.
# | Method | Duration (seconds) |
1 | Append To Array With Variables | 5 |
2 | Append To Array Using Compose Action | 2 |
3 | Select 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.
Did You Enjoy This Article? ๐บ
Subscribe to get new Power Apps & Power Automate articles sent to your inbox each week for FREE
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.
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…
Shalem,
Thank you for sending me the correction. I make these blogs so fast that sometimes I miss a few small details ๐
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.
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.
Hewitt,
And the cool part is the array retains its original order ๐
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
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 ๐
Hi,
is there a way to use the Select function when the input is a JSON object that would like to be converted into an array?
Amazing, thanks for sharing.
In my case, I have a list of ids, prices and dates. For each id, I have several dates and prices. My goal is to build an array of the max date for each id.
Here is my solution using the 2nd method:
1. List rows
2. Select: id, date
3. Select id
4. Union (3.) to remove duplicates
5. Apply to each:
5.1 from (4.)
5.2 Filter array from (2.) filtering by id
5.3 Select dates from filtered array (5.2)
5.4 Compose: max (5.3) to hold the max date
5.5 Compose: {id: current item, max date: (5.4)}
6. Compose: from (5.5)
Its’ taking 18s to process 114 rows. In this case, is it possible to use #3 method ?
I was trying to aggregate the select (2.), but I got stuck..
At least, the second method is already a great solution for me, thank you.