← Al Davies, 250 High Street

How to get the first item of an array in Power Automate

Use the first() expression. Write first(outputs('Filter_array')) in a Compose action. It returns the first item in the array. Use last() for the final item.

What does first() do in Power Automate?

The first() function returns the first item of a collection. Microsoft documents it as first('<collection>'). It works on a string or an array. For a string it returns the first character. For an array it returns the first element.

The last() function works the same way and returns the final item. Microsoft shows first(createArray(0, 1, 2)) returning 0 and last(createArray(0, 1, 2, 3)) returning 3.

How do you get the first item step by step?

  1. Run the action that makes the array. For example, use Filter array, Select, or Get items.
  2. Add a Compose action below it.
  3. Select the input box and open its Expression tab.
  4. Type first(body('Filter_array')), with the name of your own action.
  5. Choose Add, then save the flow and run it.
  6. Open the run history and check that the Compose output shows one item, not a list.

How do you get one field from the first item?

Add a property name after the function. Write first(body('Filter_array'))?['Title'] to read the Title column of the first match. The question mark makes the expression return null instead of an error when the property is missing.

For a SharePoint list, the rows sit in the value property of Get items. Write first(outputs('Get_items')?['body/value'])?['ID'] to get the ID of the first row. Add a Filter array action first when you want the first row that matches a rule.

What happens when the array is empty?

An empty array has no first item. The expression gives you no value, and a later step that needs text or a number can fail. The error often reads that the value is null. See invalid type, expected string but got null.

Guard the step. Add a Condition action that checks length(body('Filter_array')) is greater than 0. Put the first() step in the Yes branch. You can also wrap the value with coalesce() to supply a default. Test with an empty result before you trust the flow.

Can you use an index instead of first()?

Yes. Write body('Filter_array')?[0] to get the first item. Use ?[1] for the second. The index starts at 0. This form reads well for a fixed position.

Use last() for the final item, since you do not need to know the length. To count the items yourself, see count items in an array with length().

How do you get the newest or oldest item?

Sort first, then take the first item. In Get items, set Order By to Created desc and Top Count to 1. The first row is then the newest. This also avoids loading every row. See why Get items returns only 100 rows.

For data already in a flow, use the sort array method, then apply first() to the sorted result.

FAQ

What is the difference between first() and an index of 0?

They return the same item. The first() function is easier to read. The index form, such as ?[0], also lets you pick the second or third item.

Does first() change the original array?

No. It returns one item and leaves the array as it was.

Can first() read the first letter of a text value?

Yes. Microsoft shows first('hello') returning h.

Sources