Contextures

Excel FILTER Function Examples

These Excel FILTER function examples show how to create dynamic filtered lists that update automatically, when the source data changes. Video, written steps and free workbook

NOTE: FILTER is one of the Excel spill functions (dynamic arrays) that are available in Microsoft 365 plans. See more dynamic array formulas on the Spill Function Examples pagefood sales data for filter function examples

Intro: FILTER Function

The Excel FILTER function creates dynamic filtered lists, based on criteria, that update automatically, when the source data changes.

In this video, you'll see three FILTER function examples, to help you get started with this powerful Excel function. The written instructions are below the video.

 

FILTER Syntax

The FILTER function syntax has 3 arguments -- array, include, and if_empty (optional)

Note: The comma separator is used in these examples. For other regional settings, a semi-colon separator might be required.

filter function syntax

  1. array - select the range of cells that you want in the results
  2. include - enter one or more criteria settings
  3. if_empty - (optional) enter a value to show if there are no results (result array of values is empty)

Food Sales Data

In the first four FILTER function examples shown on this page, the original data set is stored in a food sales table, named Sales_Data. These are food product orders, for an imaginary food company, and you can get this data in the Download section, at the bottom of this page.

In the Sales_Data table:

  • There are 9 columns of data, including 1 column with a calculation.
  • There are 244 rows of data in the food sales table.

Each row in the food sales range of data shows values for the following fields:

  • Date: Date the order was placed
  • Region: geographic region where order will be shipped
  • City: city where order will be shipped
  • NameL: last name of sales representative
  • Category: product category - Bars, Cookies, Crackers or Snacks
  • Product: product name
  • UnitPrice: product selling price per unit
  • Qty: number of units ordered (quantity)
  • SalePrice: total price of order - calculation - Qty x UnitPrice

The FILTER function examples shown below will pull data from this table, based on one criterion, or multiple criteria.

food sales data for filter function examples

Ex 1: One Criterion, One Column

The first example shows how to use the new FILTER function to create a list of cities in a specific region.

For this FILTER formula:

  • there is 1 criterion -- Region name entered in cell B4
  • result is in 1 column - list of City names from specified region
  • City and Region are columns in the table named Sales_Data

Formula for One Criterion One Column

The following formula is entered in cell D4, and includes a cell reference to cell B4:

  • =FILTER(Sales_Data[City], Sales_Data[Region]=B4, "--")

The 3 arguments in that formula are:

  1. array - Sales_Data[City] -- Use City column as source data array
  2. include - Sales_Data[Region]=B4 -- for cities in the specified region
  3. if_empty - "--" - show 2 dashes if no matching records are found
    • Tip: Use an empty string ("") as the last argument, if you want the result cell appear blank

Note: If you don't use the if_empty argument, and no results are found for the FILTER function, the formula will return a #CALC! error.

no results so if_empty setting is shown

Results for One Criterion One Column

If there are NO records for the selected region, the formula result is two dashes (the if_empty setting).

However, in the screen shot below there ARE records for the selected region - Southwest

  • FILTER formula is in cell D4, where the first City name is shown
  • Filter returns more than one record from the selected region, so the remaining results spill down, into the cells below, as far down as needed
  • City names are listed in the order they appear in the data
  • Some City names are repeated, because they are in the Sales_Data table multiple times.

food sales data for filter function examples

Cells in the Spill Range

The cells with formula results are called the Spill Range.

If any cell in the spill range is selected:

  • There is a thin blue border around the outside of the spill range
  • The formula can be seen in the Formula bar
    • If cell D4 is selected, the formula is in black font
    • If any other cell in the spill range is selected, the formula is in black font

For more details, see this Microsoft article on spilled array behaviour, for FILTER and other dynamic array functions

How Does FILTER Function Work?

Here's the FILTER function formula, from Example 1, shown above:

  • =FILTER(Sales_Data[City], Sales_Data[Region]=B4, "--")

The formula is in cell D4, and refers to the Region name entered in cell B4

no results so if_empty setting is shown

Filter Function Arguments

And here's how the first two arguments in that FILTER formula work:

  • 1st argument tells Excel to return data from the City column in the Sales_Data table
    • Sales_Data[City]
  • 3rd argument tells Excel to show 2 dashes if no matching records are found
    • "--"

The powerful work is done in the 2nd argument, where the criterion is entered:

  • the formula tests each row in the table, to see if Region name is equal to region name in cell B4
    • Sales_Data[Region]=B4
  • For each row, the result of the comparison is a Boolean value - TRUE or FALSE
    • If the result is TRUE, city name from that row will be included in filter results
    • If the result is FALSE, city name from that row will NOT be included in filter results

Warning: If the result of any comparison is not a Boolean value, the FILTER formula result will be an error value

Formula Bar Demonstration

To see the TRUE and FALSE results, I did a quick test of the FILTER formula

  1. First, I selected cell D4 on the worksheet, where the formula is entered
  2. Next, in the formula bar, I selected the second argument:
    • Sales_Data[Region]=B4
  3. After that, I pressed the F9 key on my keyboard, to calculate just the selected part of the formula.

In the screen shot below, highlighted in yellow, you can see the first few results in the calculated section.

  • Results are a Boolean array: {FALSE; TRUE; TRUE
  • Array starts with a curly bracket
  • Values are separated by semi-colons
  • At the end of the array (not shown), is a closing curly bracket

food sales data for filter function examples

TRUE and FALSE Results

To confirm those results, you can compare the Boolean array with the Sales Data records:

  1. FALSE: First sales order is NOT from the Southwest region
    • City name from that row, Charleston, WILL NOT be included in filter results.
  2. TRUE: Second sales order IS from Southwest region.
    • City name from that row, Austin, WILL be included in filter results.
  3. TRUE: Third sales order IS from Southwest region
    • City name from that row, Santa Fe, WILL be included in filter results.

cities in sales data records

Safely Exit Formula Cell

After calculating part of a formula, press the Esc key on your keyboard, to exit the cell, without saving the calculation. Otherwise, the calculated results will be saved in the formula

Tip: If you accidentally press the Enter key:

  • you'll see the boolean array in the formula, instead of the criteria test.
  • Press the Undo shortcut immediately -- Ctrl+Z -- to fix the formula

Edit FILTER Formula

In the next section, the FILTER formula will be edited, to create a list where each city name only appears once.

If you want to make changes to the FILTER formula, start with these steps:

  • Select cell D4, where the FILTER formula was entered
  • With cell D4 selected, the formula shows in the Formula bar, in black font
    • You will be able to make changes to the formula in the Formula bar

Note:

If any other cell in the spill range is selected (not cell D4):

  • The formula can be seen in the Formula bar, but it is in grey font (dimmed out)
  • If you click in the Formula bar, the formula disappears
  • You cannot edit the formula in the "spill" cells
  • You can only change it in the top cell, where the formula was entered

Create a Unique Sorted List

In some cases, when using the FILTER function, it will be acceptable to see items listed multiple times.

However, for this example, we want a list of the cities in the selected region, with these enhancements:

  • Unique: show each city name only once
  • Sorted: show the city names in alphabetical order - A to Z

To add those enhancements, follow the instructions below, to add two more functions to the formula.

Unique List of Names

To get a list where each city name only appears once, follow these steps to add the UNIQUE function to the formula:

  • Select cell D4, where the formula was entered
  • Click in the Formula bar, to start editing the formula
  • Click to the right of the equal sign
  • Type the UNIQUE function name, and an open bracket: UNIQUE(
  • Click at the end of the formula, and type a closing bracket
  • Press Enter, to complete the change

Here is the revised formula, to create a unique list of city names:

  • =UNIQUE(FILTER(Sales_Data[City], Sales_Data[Region]=B4, "--"))

The result is a list of the unique city names in the selected region.

unique list of city names

Sorted Unique List of Names

To sort the list of unique city names, follow these steps:

  • Select cell D4, where the formula was entered
  • Click in the Formula bar, to start editing the formula
  • Click to the right of the equal sign
  • Type the SORT function name, and an open bracket: SORT(
  • Click at the end of the formula, and type a closing bracket
  • Press Enter, to complete the change

Here is the revised formula, to create a unique list of city names:

  • =SORT(UNIQUE(FILTER(Sales_Data[City], Sales_Data[Region]=B4, "--")))

The result is a sorted list of the unique city names (A to Z) in the selected region.

city names in alphabetical order

Ex 2: One Criterion, 2 Columns

The second example is similar to the first one, but the results will spill into 2 columns, to show the cities and sales reps for the selected region.

To do this, we'll change the first argument in the FILTER function:

  • Start with a copy of the completed FILTER formula from Example 1
    • =SORT(UNIQUE(FILTER(Sales_Data[City], Sales_Data[Region]=B4, "--")))
  • Select the table reference (Sales_Data[City]) in the first argument, and delete it
  • Go to the FoodSales sheet, and select the City and NameL columns (data only, not the headings)
  • Press Enter, to complete the change

2 columns in array argument

Here is the revised formula in cell D4:

  • =SORT(UNIQUE(FILTER(Sales_Data[[City]:[NameL]], Sales_Data[Region]=B4, "--")))

Now there are 2 columns in the formula's spill cells, showing the city names and sales rep names.

City names are in A-Z order, but the sales rep names are not sorted.

2 columns in array argument

Add SORT Function Optional Argument

To fix that, we'll add a second SORT function, so both columns are sorted.

But first, we'll add the optional arguments for the existing SORT function - sort index and sort order.

This will be the SORT for the sales rep names, which should be sorted AFTER the city names are in alphabetical order. So, its

  • sort index is 2
  • sort order is 1 (Ascending)

=SORT(UNIQUE(FILTER(Sales_Data[[City]:[NameL]], Sales_Data[Region]=B4, "--")) ,2,1)

2 columns in array argument

Add a Second SORT Function

Next, add another SORT function at the start of the formula, for the City column. It should be sorted first, so its

  • sort index is 1
  • sort order is 1 (Ascending)

=SORT(SORT(UNIQUE(FILTER(Sales_Data[[City]:[NameL]], Sales_Data[Region]=B4, "--")) ,2,1)) ,1,1)

Now the cities are sorted in ascending order, and for each city, the sales reps are sorted in ascending order.

both results columns sorted

Ex 3: Two Criteria, 1 Column

The third example will show product names that were sold in the selected region, with the price greater than a set amount.

On the filter results sheet,

  • There is a region name selected from the drop down list in cell B4
  • There is a minimum price type in cell B7
  • In cell B2, the FORMULATEXT function shows the formula from cell D4
  • Labels are typed in cells B3, D3 and B6
    • Those labels are not used in the formulas

both columns sorted

Multiple Criteria for FILTER Function

Previous examples only had one criterion in the FILTER function's include argument:

  • Sales_Data[Region]=B4

In this example, there are 2 criteria

  1. Region name = B4
  2. Price > B7

To use multiple criteria in the FILTER function,

  • enclose each criterion in round brackets
  • use the asterisk multiplier operator (*) between the criteria, to test if a record meets BOTH criteria
    • for the FILTER criteria, the asterisk operator means AND
    • to be included in the filter results, record must be in selected region AND price must be greater than set amount

Here is the include argument for the Example 3 FILTER function:

  • (Sales_Data[Region]=B4) * (Sales_Data[SalePrice]>B7)

And here is the completed formula in cell D4, to create a list of product names for the selected region, with a price greater than the set amount.

  • =SORT(UNIQUE(FILTER(Sales_Data[Product], (Sales_Data[Region]=B4) * (Sales_Data[SalePrice]>B7), "--")))

Note: There is only one multiplication operator in this formula, but you can add more, if needed, to create additional criteria tests.

Ex 4: Dates Formatted YYYY-MM

Instead of pulling unformatted data from the source data set, you can add formatting within the FILTER function formula.

The fourth example will show a list of months (yyyy-mm) when there were sales in the selected region, with the price greater than a set amount.

Just like the third example, this formula has 2 criteria in the FILTER function,

  • (Sales_Data[Region]=B4) *(Sales_Data[SalePrice]>B7)

list of months in yyyy-mm format

Format the Dates

In this formula the FILTER function will return items from the Date field. However, we don't want a long list of specific dates.

Instead, we want a list of the months, formatted as yyyy-mm. That will group the individual dates by year and month.

To get that result, use the Excel TEXT function -- it formats numbers based on your specifications. Here is the array argument in the FILTER function, with the TEXT function formatting the results:

  • FILTER(TEXT(Sales_Data[Date],"yyyy-mm")

And here is the completed formula in cell D4, returning the list of formatted dates

  • =SORT(UNIQUE(FILTER(TEXT(Sales_Data[Date],"yyyy-mm"), (Sales_Data[Region]=B4) *(Sales_Data[SalePrice]>B7), "--")))

More Filter Examples

To see more examples of using the FILTER function, these pages, here on the Contextures site:

Hide Used Employee Names

After an employee name has been used in the "On Call" list, the FILTER function prevents it from appearing in the selection list. Employee List - Dynamic Arrays example

Region Employees Drop Down List

Select a region name from one drop down, and the FILTER function creates a dynamic list of employees from that region. Dependent Drop Down from Dynamic Arrays

Get the Sample Files

Four Examples: To see the four FILTER examples, download the FILTER function examples workbook. The file is zipped, and is in Excel xlsx format, with no macros.

More Functions Tutorials

Interactive FILTER Example

FILTER with Criteria List

FILTER Function Lookup

FILTER Function Reports

Spill Function Examples

Dynamic Drop Down Lists

Named Excel Tables

Last updated: July 23, 2023 3:24 PM