“I’d like to pick a specific item from an array, what’s the best approach to navigate in there using Power Automate?”
Arrays are a key part of almost every Power Automate flow. It can be an array of SharePoint items or Dataverse rows, array with users inside multiple people picker field, or any other array created by splitting a string. Often you want to process all the items, but in some situations you need just one specific value. How do you navigate in an array? How do you get the value on the first place, last place, or anywhere in between?
Get the first and last item
If you need the first or last item, you can use an expression. This is very often used to avoid loops in a flow – if there’s only one item you don’t need a loop, you can just take the first one and use it directly.
The expression to get the first item is first(…).
first(<array>)
With an example array [“a”, “b”, “c”] the first(…) expression will return “a”.
The second expression, this one to get the last item is last(…).
last(<array>)
With an example array [“a”, “b”, “c”] the last(…) expression will return “c”.
Get Nth item from an array
If you’re not interested in the first or last item, you can use also indexes in the array for navigation as explained in the JSON navigation article. Adding an index in square brackets will get you the specific item. It starts from 0 (first item) and goes up to length-1 (last item).
<array>[<index>]
As an example, to get the 2nd item use index 1.
Getting item on a random index
It gets a lot more complicated if you don’t know the position. If you’re looking for some value but you’re not sure if it’s first, second, third, or anywhere else.
In general you should never get into that situation. It’s not easy to find index of a specific item. Instead of trying to do that you should pre-process the results.
Are you looking for a specific value? Use ‘Filter array’ before accessing the item to remove everything but that item from the array. Apply first(…) on the outcome and you’re done.
Are you looking for the highest/lowest value? Use the sort(…) expression to sort the array first. Move the desired item to the top or to the end of the array and then use first(…) or last(…).
Summary
When you navigate in an array in your Power Automate flow, you must always know what you’re looking for. Is it the first or last item? Use the first(…) or last(…) expression on the array. Is it always Nth item? Use the [index] to get the value. Don’t know the position of the value? Preprocess the array with sorting or filtering that’ll allow you to use one of the expressions or the index.