How to Filter an Array in Power Automate [with Multiple Conditions]

Do you need to filter an array in your flow?

When working with an array of records in Power Automate, you might need to filter the array based on a condition to get only certain items from your array.

Thankfully, there is a Data Operation action specifically to filter an array of records based on a condition. You can also filter based on multiple conditions using the advanced mode editor.

This post is going to show you how to filter your array in Power Automate.

Filter Array with Single Condition

[
  {"name": "Allison", "gender": "F", "age": 35},
  {"name": "Adam", "gender": "M", "age": 47},
  {"name": "David", "gender": "M", "age": 26},
  {"name": "Jen"," gender": "F", "age": 31},
  {"name": "James", "gender": "M", "age": 33}
]

    This example will show you how to filter the above array created in a Compose action.

    If your data comes from another source such as a SharePoint list or an Excel table, the process will be even easier as you will be able to use dynamic content to reference the fields in the array.

    Here’s how to use the Filter array action to filter your array based on a single condition.

    1. Add the Filter array action to your flow somewhere after the array.
    2. Add the Array reference as dynamic content in the From input field of the Filter array action.

    Now you will be ready to create your filter condition.

    The Filter array action iterates through your array similar to an Apply to each action. This means you can reference the current item in the array using the item() function.

    item()?['age']

    Then you can reference a field in the current item by using the square braces [] with the field name like above. This will reference the age key-value pair of the current record inside the Filter array action.

    1. Add the item reference to the field you want to base your filter condition on to the left side of the filter condition statement. For example, add the expression item()?['age'] to the filter based on age.
    2. Select the comparison type from the dropdown list. There are many text and number based comparisons available. In this example, is greater than has been selected to filter on records where the age field is greater than a given value.
    3. Add the Value to compare and filter based on, in the right-most input of the Filter array condition. This example will filter on records with an age over 30.

    The above example shows a filter on the age field in each record of the array. The Filter array action will return all the records in the array where the age is greater than 30.

    Filter Array with Multiple Conditions

    The Filter array action also allows for building filters with multiple conditions using both AND & OR logic.

    In fact, you can use any combination of these to create the exact logic you want.

    Click on the Edit in advanced mode link and you will reveal the formula behind the condition you’ve built from the user interface.

    This is an easy way to build out all the parts of your complex AND or OR condition logic. It will show you the formula behind the condition and you can use these to build more complex conditions.

    @and(
    	startsWith(item()?['name'], 'A'),
    	greater(item()?['age'], 30)
    )

    Use the and() or or() functions to build the full logic. For example, the above formula will filter the records where the name starts with the letter A and the age is over 30.

    You can build the condition on the name field with the starts with option in the dropdown, then build the condition on the age with the is greater than option in the dropdown.

    Make sure the outside function starts with the @ character. This tells Power Automate it is an expression to evaluate and not just text.

    @or(
    	startsWith(item()?['name'], 'J'),
    	less(item()?['age'], 30)
    )

    Similarly, the above formula will filter on all the records where the name starts with J or the age is under 30.

    You can use any other logical comparison function in your condition such as the not() function. There is no limit to how complex you make your filter conditions.

    Conclusions

    Filtering data in your flow is essential if you only want to act on certain items in your array of data.

    Power Automate has a built-in action for filtering arrays, but the condition builder only allows for creating filters with one condition.

    Using the advanced mode editor allows you to craft filters with multiple conditions.

    Did you know how to create multiple condition filters on your arrays? Let me know in the comments!

    About the Author

    John MacDougall

    John MacDougall

    John is a Microsoft MVP and freelance consultant and trainer specializing in Excel, Power BI, Power Automate, Power Apps and SharePoint. You can find other interesting articles from John on his blog or YouTube channel.

    Related Articles

    Comments

    0 Comments

    Get the Latest Tech Tips

    Write For Us

    Are you a tech enthusiast with a talent for writing great content? Come write for us!

    Follow Us

    Follow us on social media to stay up to date with the latest in tech!