Excel FILTER Function – How To Use

The FILTER function is an Excel function that lets you fetch or "filter" a data set based on the criteria supplied via an argument. The FILTER function was introduced in Office 365 and will not be accessible in Office 2019 or earlier versions.

FILTER is an in-built worksheet function and belongs to Excel's new Dynamic Arrays function category. The FILTER function returns an array of values that are spilled onto your worksheet unless the function is nested to relay the output to another function.

FILTER is a dynamic function. This means when you alter the values in the source data or resize the source data array, the Excel FILTER function will automatically update the returned values.

Excel FILTER Function

Syntax

The syntax of the Filter function is as follows:

=FILTER(array, include, [if_empty])

Arguments:

array – This is a required argument where you specify the range or array that you want to filter.
include – This is a required argument where you supply the filtering criteria as a Boolean array.
if_empty – This is an optional argument where you specify a value that the function should return if none of the entries meet the supplied criteria.

Important Characteristics of the FILTER function

  • You can use the FILTER function for filtering both horizontal as well as vertical arrays.
  • When you use the FILTER function between two separate workbooks, ensure that both of them are open. If they are not, the function will return a #REF! error.
  • Since the FILTER function spills the output (assuming your data is so organized), ensure that you have sufficient empty cells to the right and below. If you do not, the function will return a #SPILL error.
  • The FILTER function returns a #VALUE! error if the dimensions supplied in the include argument are incompatible with the array argument.
  • The FILTER function was introduced in Office 365 and is not available in Office 2019 or earlier version.

Examples of the FILTER function

Let's have a look at the examples of the FILTER function:

Example 1 – Plain-vanilla Version of the Filter function

Suppose we want a list of students that have Grade "A" in the data set.

Plain-vanilla Version of the Filter function

In order to do this, we will input the entire data set, i.e., A2:C16, in the array argument.

In the include argument, we will input C2:C16="A" which will produce a Boolean array that assigns TRUE for all 'A's.

The last argument (if_empty) is an optional string where you will let the formula know what it should return if it does not find any values. In our case, we use a text string "No matches", but you could also have the formula return nothing by passing an empty string ("").

So the final formula would be –

=FILTER(A2:C16,C2:C16="A","No matches")

If you prefer, you could also have a cell reference instead of adding a character or a text string in the include argument. For example, let's say you write "A" in cell G6 and then input the include argument as C3:C17=G6.

cell reference instead of adding a character or a text string in the include argument

So the formula would be –

=FILTER(A2:C17,C2:C17=G6,"No matches")

The FILTER function will also work smoothly for horizontally organized data sets. Just ensure that the width of ranges defined in the array and include arguments are the same.

Also, in case of no matching records the formula, returns the string no match as shown.

in case of no matching records the formula, returns the string no match as shown

Since none of the students in the above dataset has Grade "D", the result is the "No matches" string.

Example 2 – FILTER function Used in Combination With the EXACT function

Nesting the EXACT function inside the FILTER function enables you to fetch an exact, case-sensitive match.

=FILTER(A2:C16,EXACT(A2:A16,"Bing"),"No matches")
FILTER function Used in Combination With the EXACT function

The EXACT function has a simple role here and works as the include argument in the FILTER function. EXACT returns TRUE for an exactly matching string and FALSE otherwise. As such, the EXACT function will return 15 Boolean values (for each cell in the B2:B16 range), which in our case will look something like this:

{FALSE; FALSE; FALSE; FALSE; TRUE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE; FALSE}

At position 5, "Bing" is the only match and hence returns TRUE.

This array is relayed to the FILTER function as the include argument. Next, FILTER uses the array returned by EXACT to return only those rows that contain the string "Bing" (i.e., rows that have been assigned a TRUE value by the EXACT function).

Note: Please note that we can also get to the same output by just adding the condition (A2:A16)="Bing" in the include argument, but in that case, the search will be case-insensitive.

Example 3 – Filtering with Wildcards Using the FILTER function

Let's discuss some big-boy formulas now. The FILTER function does not in itself support wildcard characters, but we can input a logical test in the include argument to get the output we want.

We will nest the ISNUMBER and SEARCH functions, like so:

=FILTER(A2:A16,ISNUMBER(SEARCH("G*",A2:A16)),"No matches")
Filtering with Wildcards Using the FILTER function

Let's walk through each component of the formula and see what role they play. We will work out the SEARCH function's output first. The search function is looking for any string in column A that has the letter "G". We have 3 last names that contain a G – Bing, Geller, and Green. Remember, that search function only returns the position of the searched character.

Therefore, wherever the SEARCH function finds a G, it will return a number (i.e., the position of G), while for others, you will get a #VALUE! error. If you want to perform a case-sensitive SEARCH, you can replace the SEARCH function with the FIND function. Let's replace the SEARCH function's return in our formula:

=FILTER (A2:A16, ISNUMBER ({#VALUE!, #VALUE!, #VALUE!, #VALUE!, 4, 1, 1, #VALUE!, #VALUE!, #VALUE!, #VALUE!, #VALUE!, #VALUE!, #VALUE!, #VALUE!), "No matches")

Next, we have the ISNUMBER function. ISNUMBER function has a simple role to play. It will look at the SEARCH function's return and assign TRUE for results that are a number and FALSE for results that are not a number (i.e., are a #VALUE! error in our case).

If you follow what is happening in the formula so far, the remaining part is a cakewalk. The ISNUMBER function relays this output (i.e., 3 TRUEs for the numbers and 12 FALSEs for the #VALUE! errors) to the FILTER function. The FILTER function will then let only those data points make it to the final output, for which the result was TRUE.

So, as our final output, we should have 3 names – Bing, Geller, and Green.

Example 4 – FILTER function With Multiple Criteria (AND/OR operator)

We have so far not used the Scores column from our data set. Let's remedy that, shall we?

FILTER function With Multiple Criteria (AND/OR operator)

So, here we are trying to find the students who have Grade "A" and a score of above 100.

We will first use the AND criteria, with an asterisk (*). To do this, we will extend the include argument using Boolean logic, like so:

=FILTER(A2:C16,(C2:C16=G6)*(B2:B16>100),"No matches")

Using the '*' multiplies the Boolean values, where True equals 1, and False equals 0. So, when the expressions inside the parentheses are multiplied in the formula, there are two possibilities:

  • You get a FALSE (or a 0): If the expressions inside both the parentheses return False (0*0) or one of them returns FALSE (0*1).
  • You get a TRUE (or a 1): If the expressions inside both the parentheses return TRUE (1*1).

Therefore, when the first parenthesis finds an A and returns TRUE and the second parenthesis finds the score to be greater than 100 and returns TRUE as well, we will have our return (or get the string "No matches"). Using logical expressions like this is an efficient approach to filter criteria.

Much the same way, we can use the OR criteria by using a plus (+) between two parentheses in the include argument, like so:

=FILTER(A2:C16,(C2:C16=G6)+(B2:B16>100),"No matches")

This will get us all the students who either have Grade "A" or scores greater than 100.

students who either have Grade "A" or scores greater than 100

Similar to what we saw for the AND criteria, we can get two possible outcomes when Boolean values in the include argument are added:

  • You get a FALSE (or a 0): If the expressions inside both parentheses return a FALSE (0+0).
  • You get a TRUE (or a 1): If the expression inside one of the parenthesis returns a TRUE (0+1) or both parentheses return a TRUE (1+1).

Wait, if both parentheses return 1, it will add up to 2, right?

Fortunately, that does not matter. Excel will consider anything that is 0 as FALSE, anything that is not 0 as TRUE.

This is how you can incorporate the AND or OR criteria in your formulas.

Example 5 – Filtering Duplicates Using the FILTER and COUNTIFS functions

If your data set is massive, it is quite likely that you may need to deal with duplicates. When a data point has more than a single instance, we can filter the duplicate instances by using a nested formula, like so:

=FILTER(A2:C20,COUNTIFS(A2:A20,A2:A20,B2:B20,B2:B20,C2:C20,C2:C20)>1,"No matches")
Filtering Duplicates Using the FILTER and COUNTIFS functions

Essentially, we are using the COUNTIFS function to count the instances of data points and extracting those data points that occur more than once. By nesting the COUNTIFS function in the include argument, you are instructing the FILTER formula to include only those results that occur more than once (>1).

You can add or remove the number of columns passed to the COUNTIFS function for filtering duplicate data based on your own requirements.

The FILTER function will spill the return with duplicate instances into a range of cells, as shown in the image.

Recommended Reading: How to find duplicates in excel

Example 6 – Filtering Blank Records Using the FILTER function

We will revisit what we learned in a previous example where we discussed the use of AND criteria using an '*'. We will use a <> operator (i.e., not equal to) inside the parenthesis to check if a cell has an empty string (""), like so:

=FILTER(A2:C16,(A2:A16<>"")*(B2:B16<>"")*(C2:C16<>""),"No matches")
Filtering Blank Records Using the FILTER function

Here, we are checking the data points contained in the A, B, and C columns and retrieving only those data points that have a value in all three columns. To do this, we instruct Excel to check for empty cells by defining a column and checking for empty cells by adding a '<>' (not equal to) operator and an empty string (""). To add an AND criteria, we will use a '*' between the parenthesis.

If a cell in any of those columns is empty, the return will exclude that particular row entirely.

Example 7 – FILTER function Used in Combination with SUM, MIN, MAX, and AVERAGE functions

The FILTER function packs in a punch. In addition to performing a whole list of tasks discussed above, it is also capable of summarizing the data it returns after filtering.

To accomplish this, we will use the aggregation family of functions – like SUM, MIN, MAX, and AVERAGE.

FILTER function Used in Combination with SUM, MIN, MAX, and AVERAGE functions

It is actually pretty straightforward. All we need to do is relay the return from the FILTER function to any of the mentioned aggregation functions, like so:

=SUM(FILTER(B2:B16,C2:C16="A",0)) //Sum of Grade A scores
=MIN(FILTER(B2:B16,C2:C16="A",0)) //Min of Grade A scores
=MAX(FILTER(B2:B16,C2:C16="A",0)) //Max of Grade A scores
=
AVERAGE(FILTER(B2:B16,C2:C16="A",0)) //Average of Grade A scores

Here, the FILTER function will include all rows that have the letter "A" and return 0 for rows that do not match. It will then pick up the values that correspond to "A" from column B. These values will be handed over to the aggregation functions, giving us our final output.

Note: It is important to specify the if_empty argument as 0. Using a string like "No matches" will result in a #VALUE! error, since the aggregation functions cannot handle strings in case of non-matching records.

Example 8 – FILTER function to return Non-Adjacent Columns

Let's say we want just the Name and Score columns of students with Grade "A". How must we explain to the FILTER function that we want just these two columns when they are non-contagious?

While there is no separate formula to do this, we can accomplish this by sprinkling some logic into the FILTER function. We will nest one FILTER formula inside another FILTER formula and use Boolean values in the include argument of the outer FILTER formula to exclude the Names column, like so:

=FILTER(FILTER(A2:C16,C2:C16="A"),{1,0,1})
FILTER function to return Non-Adjacent Columns

Let's put this formula in words.

The nested (inner) FILTER function is just a simple formula, which we are comfortable with by now.

So, let's jump over to the outer FILTER function.

The nested FILTER function works as the outer FILTER function's array argument, informing the outer FILTER function that this is where I want the returned values to come from. The include argument is where the magic happens. We use "1, 0, 1" (alternatively, we could use TRUE, FALSE, TRUE) and let the outer FILTER function know that we want to include only the first and third columns.

Simple, isn't it?

I hope I unraveled all the mysteries surrounding the brand new FILTER function and covered a little more as well. Incorporate these formulas into your worksheets, and you will become a FILTER function ninja in no time. I will see you soon with another intriguing Excel function, until then… sayonara.

About Supriya

Supriya, ExcelTrick's Editor-in-Chief, combines her MBA in Human Resources with vast Excel proficiency for data-driven decisions. Her prior role in Corporate HR solidified Excel as her trusted companion. In her leisure, she cherishes family time, gaming, and reading. Get to know Supriya better here.