Do you need to get a single element from an array in Power Automate?
This post is going to show you how to get the Nth item from an array object!
Working with arrays can be tricky if you’re not familiar with them. An array, also called a collection, is just an ordered set of items. The items inside the array can be text, numbers, dates, Booleans, or even other arrays.
When working with an array, you may only want a particular item. This post will show you how to do that.
["A", "B", "C", "D", "E"]
All the examples in this post will use the above array in a Compose action named myArray.
Get the Nth Item from an Array
Arrays are ordered lists. This means you can extract an item from the list based on the ordering.
The above syntax will get the third item in myArray which will result in the value C.
Notice the 2 in the syntax returns the 3rd item? This is because arrays in Power Automate use a zero-based index for their order. 0 is the 1st item, 1 is the 2nd item, 2 is the 3rd item, etc.
Get the First Item from an Array
You can use the 0 index to get the first item in your arrays, but there is a special formula that can also be used to get the first item.
The above expression syntax will return the first item from the myArray collection. This uses the first workflow expression to return the first item in the collection.
In this example, it will return the value A.
Get the Last Item from an Array
Getting the last item in an array would be tricky if you were only able to extract items using the index. You might not know exactly how many items your array will contain and it could change with each run.
Thankfully there is also a last workflow expression which will return the last item in any collection.
The above expression will return the last item from the myArray collection which in this example will be E. This uses the last workflow expression to return the last item in the collection.
Get the Second to Last Item from an Array
Perhaps you don’t want the last item in your array, but rather the second to last or third to last item?
In this situation, you can’t use the last expression.
You will need to find how many total items the array has, and then subtract 1 from this number. This will give you the proper index number of that item.
You can then use the index to return the value.
The above syntax will return the second to last item in the myArray collection. In this example, it will return the value D.
length(outputs('myArray')) returns a count of how many items are in the array. The expression then subtracts 2 from this value using the sub expression to get the index number of the second last item.
Arrays are a common object you will come across in Power Automate and they can be tricky to work with when you don’t know how.
Getting the values in your arrays is an easy process. These can be accessed using a zero-based index or with several expressions.
Do you have any other tips for accessing the values of an array? Let me know in the comments section below!
I have an issue I can’t seem to figure out, that is a little similar.
Is there an easy way to have a dynamic expression that gets all the items of an array EXCEPT for the last one?
I manipulate form data from an email and map it to certain columns upon creating a new customer subject. I split the “Full name” row into an array, and use first and last now, but I’d love to use all array items except for the last one in our firstname column, and last one in the surname column.
Sure you could use the take and length functions to achieve this.
See the collection functions documentation for more details.