Power Automate Filter Array with an OR criteria - the odd way

When you are writing your Power Automate Flow it is sometimes necessary to filter an array based on an OR criteria. This basically means that you need to have all items of the array filtered which are true for any of the filter criteria you want.

What is an OR filter?

An OR filter is a filter where the result set is true for any match of the filter. The or gatter looks like this:

True False
True Yes Yes
False Yes No

If for example the following list of people is your basis:

Firstname Lastname
Test Person
Max Mustermann
John Doe

And you want to filter on all people where the first name starts with T OR M, then the result would be:

Firstname Lastname
Test Person
Max Mustermann

Apply an OR filter criteria to your Power Automate Flow

The idea of the Power Automate Flow to filter an array with an OR criteria is to apply each criteria on the base array and to add the result to a result array.

In the following example the previous mentioned list of people is filtered on all entries where the first name starts with a T OR a M.

The example Power Automate Flow to filter an array with an OR criteria looks like this:

Power Automate Flow to filter an array with an OR criteria

Let's break down what is happening here:

  1. Manually trigger a flow: This is the trigger for the flow, I have chosen a manual trigger here. You can also start this e.g. when a Forms is submitted or a new entry is entered in a list
  2. Initialize variable PEOPLE: Initialization of the PEOPLE array variable to have some data to filter.
  3. Initialize variable RESULT: Initialization of the RESULT variable to collect the filtered data
  4. Filter array starts with T: Filter PEOPLE on first name starts with T
  5. Apply to each PEOPLE with T: Loop over the resultset of PEOPLE starting with T and add it to the RESULT array
  6. Filter array starts with M: Filter PEOPLE on first name starts with M
  7. Apply to each PEOPLE with M: Loop over the resultset of PEOPLE starting with M and add it to the RESULT array
  8. Compose: At the end the Compose only adds the RESULT array for showing the result

In detail the single steps look like this.

Initialize variable PEOPLE

The PEOPLE variable is initialized as an array with the content:

[
  {
    "firstname": "Test",
    "lastname": "Person"
  },
  {
    "firstname": "Max",
    "lastname": "Mustermann"
  },
  {
    "firstname": "John",
    "lastname": "Doe"
  }
]

Initialize variable RESULT

The RESULT variable is initialized with an empty array [].

Filter array starts with T

As next step the array is filtered on all entries where the first name starts with T. To do so you need to add the PEOPLE array in the from field, and item()?['firstname'] in the first field of the filter as an expression. The comparison is on starts withand in the second filter of the filter you put the character T.

Apply to each PEOPLE with T

Now the results of the filter with T need to be added to the result field. For this you need to apply a "Apply to each" element with the "Body" of the "Filter array starts with T" in the "Select an output..." field. In the loop you need to add an "Append to Array variable" element, where you add the "current item" to the RESULT array.

The same thing you do for the M and then that's it, you have implemented an OR filter criteria for a Power Automate Flow.

Caveats

This flow is very hard to maintain, as the more OR criteria you add, the more complex it gets. That's why I will explain you in my next post, how this can be done much more efficient.

In addition my flow does not contain a duplicate check, which needs to be added.