Power Automate: Get first item in output

One of the most annoying things about Power Automate for me is when I am trying to use the output of a previous action and as soon as I click that dynamic content button, it pops my new action into a For Each loop.

It does this because the previous action could have more than 1 item returned and this would clearly fail unless it was in a loop.

But if you have used the Row Count or Top option in the proceeding action, or you know full well that only 1 item will return, it is nice to avoid bloating the flow with an unnecessary loop.

There are a couple of way to do this:

First function

You can craft your filter expression so that the output is wrapped in the First function. This will tell Power Automate to only look at the first item.

First(variables('Array'))

Calling Index

You can edit your filter expression to tell it which item from the source you want. Zero being the first item e.g.

variables('Array')[0]


Which one to use?

I use the index and the main reason is due to the fact that the First function will only work with arrays and I tend to work with objects more e.g. List Rows in dataverse. If you try to
use First here, it will fail as it can only be used against strings or arrays.

Using Index allows you to get specific information returned too

e.g.

To get the name of a business unit after List Rows, you can use the following

outputs('List_business_unit')?['body/value'][0]?['name']

Conclusion

This might seem like a straightforward blog post, but whilst using the index function is better, I tend to always forget the exact syntax. This blog post more a reminder for myself than anything else! Hope you find it useful too.

Comments

Popular posts from this blog

Assigning Windows 10/11 Enterprise Subscription Activation Licences to Hybrid Azure AD Joined Devices

Autopilot Hybrid Azure AD Join with Customised First Login Status

Upgrade Samsung Galaxy Ace 2 (I8160) to Android Jelly Bean