Filter An Array in Power Automate

By Dipak Shaw

October 14, 2024


Array filtering Power Automate, automate business processes, automate workflows, Filter Array Power Automate, Filter data Power Automate, JSON filtering Power Automate, Microsoft Flow, Microsoft Power Automate, Power Automate, Power Automate expressions, Power Automate guide, Power Automate tips, Power Automate tutorial, Power Automate variables, Power Platform

In Power Automate, array variables are often used to store lists of data, and managing them effectively is crucial for automating business processes. One common requirement is to filter an array based on specific conditions. In this blog post, I’ll Walk you through how to filter array variables in Power Automate, step by step.

Why Filter an Array?

Filtering an array helps you narrow down data to just the items that match your criteria. For example, if you are dealing with a list of customer orders, you may want to extract only the orders above a certain value or filter out the orders placed in a particular date range.

Use Case Scenario

Let’s take an example where we have a list of employee records stored in an array. Each employee has attributes like Name, Department, and Age. Our goal is to filter out only the employees who belong to the “IT” department.

Step-by-Step Guide to Filtering Arrays

Step 1: Initialize the Array Variable

First, you need to create an array variable in Power Automate. You can either populate it dynamically (e.g., from a SharePoint list, Excel sheet, or API response) or manually for testing purposes.

For demonstration, let’s manually initialize an array using the “Initialize Variable” action. Set the type as Array and define your sample data in JSON format, like this:

Initialize Array Variable
[
  {
    "Name": "John Doe",
    "Department": "IT",
    "Age": 28
  },
  {
    "Name": "Jane Smith",
    "Department": "HR",
    "Age": 32
  },
  {
    "Name": "Mike Brown",
    "Department": "IT",
    "Age": 26
  },
  {
    "Name": "Anna White",
    "Department": "Finance",
    "Age": 29
  }
]

Step 2: Apply the Filter Array Action

To filter the array, we will use the “Filter Array” action.

  • Add the “Filter Array” action after your array initialization.
  • Select the array variable you created earlier as the From input.

Now, we’ll specify the condition that will filter out only the employees from the “IT” department.

In the Filter Array condition:

  • In the left input, select item() to refer to each item in the array.
  • In the middle input, select is equal to.
  • In the right input, type “IT” to filter out items where the “Department” field equals “IT”.

The condition should look something like this:

Filter Array Action
item()['Department']

Step 3: Process the Filtered Data

Once the array is filtered, you can process the resulting filtered data based on your business needs. For example, you can loop through the filtered items using the Apply to Each action and send an email or perform other actions based on the filtered data.

Step 4: Test the Flow

After setting up your flow, it’s time to test it. Manually trigger the flow, or if your flow is triggered automatically (e.g., by new entries in a SharePoint list or a specific event), monitor the run.

If everything is configured correctly, the Filter Array action will output an array containing only the employees from the “IT” department:

Output of the Filter Array

Additional Tips

  • Multiple Conditions: You can filter arrays with multiple conditions. For example, to filter employees from the IT department who are older than 25, you would add another condition by editing the filter array into advanced mode:
Multiple Condition in Filter
and(
    equals(item()?['Department'],'IT'),
    greater(item()?['Age'],26)
    )
  • Empty Results: If no items meet your criteria, the output array will be empty. Make sure to handle this case in your flow, e.g., by checking if the array is empty before processing it.
  • JSON Parsing: If your array data comes from external sources like APIs, ensure the JSON is parsed correctly. You might need to use the Parse JSON action before applying the filter.

Additional Microsoft Learn URLs:

Conclusion

Filtering arrays in Power Automate is a powerful technique to make your automation workflows more efficient. With the Filter Array action, you can easily extract only the data you need and take actions based on specific criteria. This capability is especially useful when dealing with large datasets or dynamic data from external systems.

I hope this guide has helped you understand how to filter array variables in Power Automate. Feel free to leave a comment or reach out if you have any questions or if you want to share your own experiences with filtering arrays!

Happy Automating!


I hope this will be helpful to the community! Stay tuned for more tips on Power Platform. If you enjoyed the post, don’t forget to check out other articles on my blog.

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}