Skip to main content

How to Count the Items in an Array in Power Automate with length()

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

length() returns how many items an array has. Put it in a Condition, length(...) is greater than 0, and the flow takes one branch when the array has items and the other when it's empty.

Microsoft's functions reference says length() will Return the number of items in a collection. Its example, length(createArray(0, 1, 2, 3)), returns 4.

The example is the approval flow of the Overtime Timesheet app. Approvers other than the employee, a Filter array action, leaves the department's approvers without the person who logged the overtime. If anyone is left, they approve; if not, the person's manager does.

Prerequisites​

Step 1: Write the Expression​

The array is the body of the Filter array action, body('Approvers_other_than_the_employee'), and length() counts it:

Expression
length(body('Approvers_other_than_the_employee'))

body() reads an action's output by the action's name, with underscores for spaces (How to Use the body() Function in Power Automate).

Step 2: Compare It in a Condition​

In the condition's first row, put the expression in the left value (How to Add an Expression in Power Automate), choose is greater than, and type 0 on the right:

The Check for other approvers pane on its Parameters tab with the Condition expression: And, then a row with the expression token length(...), the operator is greater t…, and the value 0

The condition's code, from Code view:

Check for other approvers → Code view (expression)
"expression": {
"and": [
{
"greater": [
"@length(body('Approvers_other_than_the_employee'))",
0
]
}
]
}

When I opened the saved condition again, the designer showed an empty row under this one. Code view didn't have it, so it isn't part of the flow.

The Result​

One approver left. For an entry Maya Patel logged in Radiology, the filter left one approver, Daniel Brooks, so the condition was true, and the True branch set the variable the approval goes to:

The raw inputs of Ask_the_other_approvers in a run, with name ApproverEmails and value daniel.brooks@example.com

None left. For an entry Daniel logged himself, the filter returned an empty array, {"body": []}:

The raw outputs of Approvers_other_than_the_employee in a run: body with an empty array

length() returned 0, the condition was false, and the run went down the False branch, to his manager:

The run view of the condition Check for other approvers, succeeded in 0.6s: under True, Ask the other approvers was skipped; under False, Get the manager succeeded in 0.6s and Ask the manager succeeded

length() or empty()?​

empty() answers is it empty? with true or false (How to Check Whether an Array Is Empty with empty() in Power Automate). length() gives the number, which also works for more than one or exactly two. For any at all, either works.

Common Mistakes​

MistakeWhat happensDo this instead
Counting the array before it's filteredThe person who logged the overtime is counted tooCount the Filter array action's body
Comparing with '0' in quotesCompares a number with textType 0 as a number
Expecting the empty row in a reopened condition to matterIt isn't in Code viewLeave it

Troubleshooting​

SymptomLikely causeFix
The condition is always trueThe expression counts something that's never emptyCheck it reads the array in Code view
An error about the function's argumentThe value isn't an array or textPass the array, such as body('...')
The wrong branch runsThe operator or the number is offis greater than 0

FAQ​

Does length() work on text?​

Yes. Microsoft's example length('abcd') returns 4, the number of characters.

Can I use length() in a trigger condition?​

A trigger condition is checked before any action runs, so it can count something in the trigger's outputs, but not the output of an action such as Filter array.

Conclusion​

length(array) is the number of items. In a Condition, length(...) is greater than 0 sends the flow one way when the array has items and the other when it's empty.

Additional Resources​