Filter Function in Power Apps

The Filter function finds records in a table that satisfies one or more conditions and returns that, and discard all others.

Power Apps Syntax

Filter(Table,Formula1,{Formula2,....}

Example:

Step 1: Select the + Insert tab in the Canvas app and add a Button control, and set its OnSelect property to the following powerapps formula.

Power Apps Formula

ClearCollect(MyNewCollection,

{Name: "Aqeel Khalid" , Country: "Pakistan", Profession: "Engineer"},
{Name: "Shaheer Ahmed", Country: "America", Profession: "Engineer"},
{Name: "Naveed Ali", Country: "Pakistan", Profession: "Doctor"},
{Name: "Dawar Zubair", Country: "America", Profession: "Lawyer"}
)

Step 2: Add a Vertical gallery control, select MyNewCollection as the data source.

Step 3: If we want to filter the data in a gallery by the Profession column and want only the data where profession is Engineer , we could filter our data by using the following formula in the Items property of our gallery.

Power Apps Formula

Filter(MyFirstCollection, Profession = "Engineer")

Now the gallery will display only the rows where the profession is Engineer.

Use of Operator with Filter Function

We can use the operators to make a more complex filter.

a) And or && operator

With the And, all conditions must be true. The And in the formula must be capitalized.

Power Apps Formula

Filter(MyFirstCollection, Profession = "Engineer" And Country = "Pakistan")

We can write the above formula as:

Power Apps Formula

Filter(MyFirstCollection, Profession = "Engineer" && Country = "Pakistan")

This formula will return all of the rows where the Profession is Engineer , and the Country is Pakistan .

b) Or or || operator

With the Or operator, if one condition is true the function returns the row. Like the And operator, the Or operator must also be capitalized.

Power Apps Formula

Filter(MyFirstCollection, Profession = "Engineer" Or Country = "Pakistan")

This formula will return all of the rows where the profession is equal to Doctor Or the Country value is equal to India. If either condition satisfied that row is returned.

We can write the above formula as:

Power Apps Formula

Filter(MyFirstCollection, Profession = "Engineer" || Country = "Pakistan")

Combining operators

We can combine more than one operator in a formula and build a more advance logic.

For example, we want all the rows where the Country is Pakistan and the Profession is either Doctor or Engineer. To do this we can include parentheses in our formula. Our formula would be the following.

Power Apps Formula

Filter(MyNewCollection, Country="Pakistan" And (Profession = "Doctor" Or Profession="Engineer"))

The formula will evaluate the parentheses first. In this formula, it will first determine if the Profession is Doctor or Engineer . If either of those evaluations is true, then the right side will be true. Next, the formula will check the value of the Country column. If that equals "Pakistan", then the row is a match and will be returned by the Filter function. Microsoft Power Apps supports a wide range of operators and the nesting of them to shape our data.