← Al Davies, 250 High Street

How to count items in an array in Power Automate

Use the length() expression. Put your array inside it, such as length(outputs('Get_items')?['body/value']), and the flow returns the number of items as a whole number.

What does length() do in Power Automate?

Microsoft describes length() as a function that returns the number of items in a string or an array. For an array, you get a whole number. For a string, you get the number of characters.

Power Automate has no separate Count action. Use length() in a Compose action, in a Condition, or in any field that accepts an expression.

How do you count the rows that Get items returns?

  1. Add a Get items action and run the flow once so the action has sample output.
  2. Add a Compose action after it. Open the Expression tab in the input box.
  3. Type length(outputs('Get_items')?['body/value']) and select Add.
  4. Save and run the flow. Open the Compose step in run history. The output is the row count.

How do you count only the items that match a rule?

Run a Filter array action first. Put the list in From and write the rule in the condition. Then count the result: length(body('Filter_array')).

This way you do not need a loop and a counter variable. See our guide on the Filter array action for the rule syntax.

How do you check if an array is empty?

Use a Condition with the expression greater(length(body('Filter_array')),0). The Yes branch runs when at least one item exists.

You can also use empty(body('Filter_array')). Microsoft documents empty() as a check that returns true when a collection is empty. Both work. The length() version is easier to read in the run history because you see the number.

If the field you test may be blank, see checking if a field is empty.

Why does the count come out wrong?

  1. The count stops at 100. Get items returns a limited page of rows by default. See Get items returning only 100 items.
  2. The expression returns an error. The action name is wrong. Spaces in a name become underscores, so Get items is Get_items.
  3. The count is the number of characters. You passed a string, not an array. Check that the path ends in /value or points to a Filter array body.
  4. The expression fails on one record. The value is a single object, not an array. Point at the array, or wrap the object with createArray().
  5. The count looks high. The array holds blank rows. Filter them out first.

How do you use the count in an email or a condition?

Pick the Compose Outputs from the dynamic content list, or paste the expression into the field. In an email body, you can write: You have @{length(body('Filter_array'))} open items.

To send the email only when the count is above zero, put the email in the Yes branch of the Condition from the earlier step. See also the Select action if you need to list the items, not only count them.

FAQ

Is there a Count action in Power Automate?

No. Use the length() expression on the array.

Does length() work on a string?

Yes. Microsoft documents it for strings and arrays. On a string it returns the number of characters.

Should I count with a loop and a variable?

Not for a simple count. Filter array plus length() uses fewer actions and is easier to read in run history.

Sources