© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
A. MurrayAdvanced Excel Formulashttps://doi.org/10.1007/978-1-4842-7125-4_13

13. FILTER Function: The Game Changer

Alan Murray1  
(1)
IPSWICH, UK
 

When the dynamic array formulas were covered in Chapter 10, the FILTER function was omitted. FILTER was introduced in Excel at the same time as the other dynamic array formulas – SORT, UNIQUE, SEQUENCE, etc. This function will make you wonder, how did I work before the FILTER function? And for this reason, it has its own chapter.

A common question I receive in my training sessions and comments on my YouTube channel is how to write a lookup formula to return all matching results. Functions such as VLOOKUP, the INDEX-MATCH combo, and XLOOKUP cannot do this. This is where the FILTER function steps up.

Now we did see the INDEX and XLOOKUP functions returning ranges, and therefore multiple values, earlier in this book. However, that is not truly returning all results for the value you are looking up. The FILTER function will return the values for each matching item found and not only the first or last matching value.

The FILTER function is quite simply a formula alternative to the popular filter tool in Excel. The filter tool is amazing but is limited in that it requires a user to reapply the filter every time they want to update the results.

FILTER will automate this task of filtering a table of data. It also gives us the capability of combining it with other formulas and Excel features for an even richer Excel experience. FILTER is *insert your chosen superlative here*!

In this chapter, we will begin with the basics of FILTER and learn how to structure the logical tests that determine the results to return. We will then start using FILTER with other functions such as SORT, TEXTJOIN, and NETWORKDAYS.INTL to become familiar with how FILTER can be applied in different scenarios.

The chapter is concluded with examples of using FILTER with other Excel features such as Data Validation and charts.

File

filter.xlsx

Introduction to the FILTER Function

Availability : Excel 2021, Excel for Microsoft 365, Excel for the Web, Excel 2021 for Mac, Excel for Microsoft 365 for Mac

The FILTER function filters a range or array based on specified criteria. It returns a spill range with the results of the filter.

This function is terrific for creating dynamic reports and models where the values to return are dependent upon the value of an input cell.

The syntax for FILTER is short and simple:
=FILTER(array, include, [if_empty])
  • Array: The range or array that you want to filter and return

  • Include: The filter criterion that determines the rows or columns to return

  • [If empty]: The action to take, or value to return, if no results are returned by FILTER

Simple FILTER Example

Let’s jump straight into a simple example of the FILTER function in action.

For the next few examples, the FILTER function will be used to assist the tracking of due dates for a review process. Figure 13-1 shows the first few rows of a table named [tblReviewDates]. It contains only three columns – an individual’s name, their region, and the date that their review is due.
Figure 13-1

First few rows of the table with review due dates

Note

The [Review By] column contains a formula to ensure that the sample file, and therefore FILTER results, behaves the same for you as it does for me in these examples.

In our first FILTER example, we will return a range containing all three columns from [tblReviewDates] for the rows where the review due date has passed.

In Figure 13-2, the following formula is entered in cell B3:
=FILTER(tblReviewDates,
tblReviewDates[Review By]<TODAY(),
"No review dates have passed"
)
The criterion tblReviewDates[Review By]<TODAY() is entered as the include argument to ensure only rows with a past date in the [Review By] column are returned. The text “No review dates have passed” would be returned if the returned array contained no results.
Figure 13-2

FILTER returning overdue review dates

FILTER with Multiple Conditions

Often, there may be more than one condition that determines the results to return. Let’s progress from our first example and see examples that include multiple conditions and handle both AND and OR logic.

In the first of these examples, we will create a report that returns those who have a review due in the next x number of days. The number of days will be determined by a cell value.

In Figure 13-3, the following formula is entered in cell B5:
=FILTER(tblReviewDates,
(tblReviewDates[Review By]>=TODAY())*
(tblReviewDates[Review By]<TODAY()+B2),
"No review dates are due"
)
Figure 13-3

FILTER function with multiple conditions and AND logic

The include argument of FILTER in this example contains two conditions. When writing multiple conditions in the FILTER function, each condition must be enclosed in its own set of brackets.

In this example, we need to apply AND logic between the two conditions. They are that the [Review By] date must be between today’s date and the date in the number of days specified by the value in cell B2.

To apply AND logic, the asterisk (*) operator is used between the two conditions. If OR logic was required, the plus (+) operator would be used (we will see an example of this shortly).

How the FILTER Function Logic Works

Let’s briefly recap on why the * and + operators are used to apply AND and OR logic, respectively.

Let’s imagine we have six rows of data in a table, and the two conditions returned the following two arrays:
{TRUE; FALSE; FALSE; TRUE; TRUE; FALSE}
and
{TRUE; TRUE; FALSE; FALSE; TRUE; FALSE}
Note

A semicolon separates rows in an array, and a comma separates columns in an array.

The asterisk is the multiplication operator and ensures that the corresponding values in each array are multiplied. This converts the TRUE and FALSE values to 1 and 0, respectively, and performs the operation between each corresponding value.

The following array is returned. 1*1=1, 0*1=0, 0*0=0, and so on.
{1; 0; 0; 0; 1; 0}

The rows of the array that are equal to 1 are then returned. So, rows 1 and 5 are returned in this example.

If OR logic is applied between the two arrays, the following array is returned. 1+1=2, 0+1=1, 0+0=0, and so on. Any value except 0 is equal to TRUE in a logical expression. So, rows 1, 2, 4, and 5 are returned in this OR logic example.
{2; 1; 0; 1; 2; 0}
Note

This same application of logical expressions with arrays was seen with the SUMPRODUCT and SUM functions earlier in this book.

Further Examples

Now that we understand why the * and + operators are used to perform the different logical operations, let’s proceed and see further examples with the FILTER function.

In Figure 13-4, the following formula returns the rows where the [Review By] date is in the next x number of days for a specified region only. The region is stated by the value in cell C2.
=FILTER(tblReviewDates,
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+B2)*
(tblReviewDates[Region]=C2),
"No review dates are due"
)
Figure 13-4

FILTER function with multiple conditions

Two AND logical tests are applied between the three conditions. Each condition is enclosed in its own set of brackets. Let’s now add some OR logic into the formula.

In Figure 13-5, the following formula includes a second specified region. In this example, the number of days is stated in cell B2, and the regions are in cells C2 and D2. The OR logical tests are enclosed in its own set of brackets to ensure that it calculates independently (multiplication has precedence over addition in the order of calculation).
=FILTER(tblReviewDates,
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+B2)*
((tblReviewDates[Region]=C2)+(tblReviewDates[Region]=D2)),
"No review dates are due"
)
Figure 13-5

FILTER function with AND and OR logic

Sort the FILTER Results

The results of the FILTER function will appear in the same order that they appear in the source data. In this example, that is the [tblReviewDates] table. This may not be the order that you require. Also, if someone sorts the data in [tblReviewDates], this change will propagate through to the FILTER results.

To specify the order for our FILTER results, we can add the SORT function.

In Figure 13-6, the following formula uses the SORT function to order the returned array by the third column, the [Review By] column of the table, in ascending order. This ensures that the more urgent review dates will appear first in the results.
=SORT(FILTER(tblReviewDates,
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+B2)*
((tblReviewDates[Region]=C2)+(tblReviewDates[Region]=D2)),
"No review dates are due"
),3,1)
Figure 13-6

Sorting the filter results by review date in ascending order

Number 1 is entered for the sort order argument of the SORT function to specify the ascending order. This was not necessary as ascending is the default order, but I like to be explicit in a formula and specify the order.

The SORT function supports the ability to sort an array by multiple columns. In Figure 13-7, the following formula is used to sort the FILTER results by region (column 2) in ascending order followed by the review date (column 3) in ascending order:
=SORT(FILTER(tblReviewDates,
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+B2)*
((tblReviewDates[Region]=C2)+(tblReviewDates[Region]=D2)),
"No review dates are due"
),{2,3},{1,1})
An array of constants has been used in the sort index and sort order arguments of the SORT function to specify this multilevel sort.
Figure 13-7

Sorting the filter results by multiple columns

#CALC! Error with FILTER

The #CALC! error is returned by the FILTER function when there are no results for the FILTER function to return.

In the previous examples, the text “No review dates are due” was entered in the if empty argument of FILTER. This text would be returned instead of the #CALC! error if there were no results to return.

In Figure 13-8, the following formula is used. The if empty argument is omitted, and the returned array is empty as there are no review dates in the next seven days for the south region. The #CALC! error is returned.
=FILTER(tblReviewDates,
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+B2)*
(tblReviewDates[Region]=C2)
)
The solution to this would be to use the if empty argument to return a logical response such as the text “No review dates are due.”
Figure 13-8

CALC error due to the returned array being empty

However, another common cause for the #CALC! error is when an input value is omitted. In Figure 13-9, the region in cell C2 has not been specified. The #CALC! error is returned.
Figure 13-9

CALC error caused by an empty input cell

In this scenario, it would be good to return an alternative response. The cause of the #CALC! error is different, and the response shown in Figure 13-10 is not relevant. There are definitely review dates due within the next 14 days; the cause is the empty region cell.

We will adapt the formula to show all review dates due within the specified timeframe regardless of the region if the cell C2 is left empty.
Figure 13-10

If empty argument returning a message

In Figure 13-11, the following formula is entered in cell B5. It uses an IF function to test if cell C2 is blank. If it is, the value TRUE is returned so that the FILTER function can use all regions in its returned array. If cell C2 is not blank, then the region in that cell is used in the logical expression as before.
=FILTER(tblReviewDates,
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+B2)*
(IF(ISBLANK(C2),TRUE,tblReviewDates[Region]=C2))
)

This formula can be enhanced further to handle cell B2 being left empty and other considerations. However, you cannot plan for every eventuality, and the #CALC! error is good for telling the user that they have done something wrong, such as omitting required information. Not all errors are bad.

This was a demonstration of how we could refine the formula beyond the built-in if empty argument.
Figure 13-11

Results returned even when criteria are omitted

Adding a Calculated Column to the Returned Array

You may need to add calculated columns to your report that use the results returned by the FILTER function. This will require being able to access specific columns, and maybe rows, of the returned array.

In this example, we will add a calculated column to return the number of days remaining until the review date. This provides a puzzle to us, because the FILTER function in this example returns an array that is three columns wide. Yet, we want to use only the third column [Review Date] in our calculation.

In Chapter 11, we covered the INDEX function and saw how it is the ultimate function for working with arrays. Let’s use it here to isolate the third column of this multicolumn array for our calculated column.

In Figure 13-12, the following formula is entered in cell E5 to calculate the number of days until the review date. The INDEX function is used to access the spill range of FILTER and return all values from the third column only. Today’s date is subtracted from these to return the number of days remaining.
=INDEX(B5#,,3)-TODAY()
Figure 13-12

Calculated column on a multicolumn array

Note

By using the TODAY() function in this formula, the result in cell E5 is returned in a date format. It will need reformatting to be presented correctly.

The CHOOSECOLS function provides a neat alternative to INDEX for this task. This is exactly the type of task that the CHOOSECOLS function was developed for.

In Figure 13-13, the following formula is entered in cell E5. The CHOOSECOLS function takes the place of INDEX to return the values in the third column of the spill range for the calculated column.
=CHOOSECOLS(B5#,3)-TODAY()
Figure 13-13

CHOOSECOLS to extract the values for the calculated column

Having the calculated column separate from the FILTER function results will have its advantages. It will be easier to use in other formulas if required or for values in a chart.

But maybe we want to include this column in a combined array with the FILTER results. For this, we can use the HSTACK function.

In Figure 13-14, the following formula returns the results as a single array:
=LET(
reviews,
FILTER(tblReviewDates,
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+C2),
"No review dates have passed"),
days,CHOOSECOLS(reviews,3)-TODAY(),
HSTACK(reviews,days)
)

This formula uses the LET function which is covered in Chapter 15 of this book. Using LET ensures that the FILTER function is only calculated once. The FILTER function is assigned to the name reviews. CHOOSECOLS then references reviews for its array. Skip to Chapter 15 to read more detail on how to use LET and why the LET function is important.

Both formulas are calculated and defined as a name or variable (reviews and days). These names are then used in the HSTACK function to form the single array to return.
Figure 13-14

Results returned as a single array

Note

The CHOOSECOLS and HSTACK functions are available in the Excel for Microsoft 365 version for Windows and Mac and Excel for the Web version only.

FILTER and Return Non-adjacent Columns

The FILTER function cannot independently return specific columns from a table or array. It requires the columns to be together or adjacent as Excel will typically describe it.

For these examples, we will return the [Name] and [Review By] columns from the [tblReviewDates] table only. These are columns 1 and 3.

Fortunately, there are numerous ways to accomplish this, and they are nice and simple. Let’s start with the CHOOSECOLS function.

In Figure 13-15, the CHOOSECOLS function is used in the array argument of FILTER to provide it with the columns to return. Columns 1 and 3 from [tblReviewDates] are specified.
=SORT(FILTER(CHOOSECOLS(tblReviewDates,1,3),
(tblReviewDates[Review By]>=TODAY())*
(tblReviewDates[Review By]<TODAY()+C2),
"No review dates are due"
),2,1)
The SORT function is added to order the results in ascending order by the [Review By] column. This is stated as column 2 in the SORT function as it is the second column in the returned array.
Figure 13-15

CHOOSECOLS for non-adjacent columns with FILTER

Entering the column numbers to return is not tough when there are only two out of three to return. However, you may be looking for a more dynamic approach.

The CHOOSECOLS function will accept an array of constants to specify the columns to return, for example, =CHOOSECOLS(tblReviewDates,{1,3}). Because of this, the MATCH function could be used to return the columns stated in range B4:C4 on the worksheet. This will prevent the need to type in the column numbers manually, as the columns will be dynamically returned based on the cell values.

In Figure 13-16, the following formula uses the MATCH function to return the array of column numbers for CHOOSECOLS to use. It matches the values in range B4:C4 against the values in the header row of [tblReviewDates]. The value in cell C4 has been changed from “Review Date” to “Review By” to match the column header in [tblReviewDates].
=SORT(FILTER(
CHOOSECOLS(tblReviewDates,MATCH(B4:C4,tblReviewDates[#Headers],0)),
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+C2),
"No review dates are due"
),2,1)
Figure 13-16

CHOOSECOLS with MATCH for dynamic columns to return

If your preference is to specify the columns to return in an absolute manner, instead of entering column numbers or referencing cell values, then the CHOOSE function is a great alternative.

In Figure 13-17, the following formula uses the CHOOSE function to specify that we want the [Name] and [Review By] columns returned only. This approach is ideal when working with data formatted as a table.

Numbers 1 and 2 are entered in an array for CHOOSE. It is then provided with the columns to return for each of those index numbers. The columns could have been specified in any order.
=SORT(FILTER(
CHOOSE({1,2},tblReviewDates[Name],tblReviewDates[Review By]),
(tblReviewDates[Review By]>=TODAY())*(tblReviewDates[Review By]<TODAY()+C2),
"No review dates are due"
),2,1)
Figure 13-17

CHOOSE function for absolute column references

UNIQUE Ignoring Blanks

The FILTER function is great for removing blanks from a range. A typical scenario for this technique would be to prepare a range of values for use in a drop-down list or labels of a report.

In Figure 13-18, the following formula is entered in cell D2 to remove the blank cells from the country names in [tblCountries]. The SORT and UNIQUE functions are also applied to order them and remove any duplicate values that may occur.
=SORT(UNIQUE(
FILTER(tblCountries[Location],tblCountries[Location]<>"")
))
Figure 13-18

Removing blanks from an array of values

FILTER with NETWORKDAYS.INTL

The FILTER function can be used to provide other functions with a filtered array to be used. This provides a lot of potential and fun when you think of the different ways that this can be utilized.

In this example, we will use the FILTER function with NETWORKDAYS.INTL to return a range of holidays dates that are dependent on criteria.

Note

This example could be applied to the NETWORKDAYS function just as well. However, the NETWORKDAYS.INTL function (covered in Chapter 6) is a superior function.

Figure 13-19 shows a table named [tblClosures]. It contains the dates for three different sites/locations for when they are closed, and tasks of a project therefore cannot be worked.
Figure 13-19

Site closure dates in a table

In Figure 13-20, the following formula is entered in cell E2 to calculate the number of working days for the different tasks of a project.

The FILTER function returns the dates from the [tblClosures] table where there is a match for the site name in the header row of [tblClosures].
=NETWORKDAYS.INTL(B2,C2,,
FILTER(tblClosures,tblClosures[#Headers]=D2,0)
)
Something interesting about this example when compared to the previous examples is that this formula is filtering by column instead of by row. You typically see the FILTER function returning matching rows from a table; however, as demonstrated with this example, the FILTER function can filter by column also.
Figure 13-20

NETWORKDAYS.INTL and FILTER for dependent non-working days

The formula =NETWORKDAYS.INTL(B2,C2) is entered in column F to help demonstrate that the FILTER function successfully returns the correct dates.

FILTER and Combine Results in One Cell

The FILTER results can be combined in a single cell using the TEXTJOIN function. This can offer a neat alternative to spilling the results into the adjacent cells on the sheet.

Figure 13-21 shows a table of product data named [tblProducts]. It contains a [Product] column for the product name and a [Category] that it is assigned to.
Figure 13-21

Table of product data

Let’s imagine that we want to list the different product categories in a column and, in the adjacent column, list all products assigned to that category combined into a single cell.

In Figure 13-22, the following formula is entered in cell B3. It lists the distinct product categories in ascending order:
=SORT(UNIQUE(tblProducts[Category]))
The following formula is then entered in cell C3 and filled down to C5:
=TEXTJOIN(", ",,
SORT(FILTER(tblProducts[Product],tblProducts[Category]=B3))
)

The FILTER function returns the array of products that match the category entered in the corresponding cell of column B. The values in this array are sorted in ascending order by SORT.

TEXTJOIN then has the simple task of joining each value together in a single cell separated by a comma and space “, ”. The ignore empty argument of TEXTJOIN is ignored.
Figure 13-22

Filter results combined in one cell

Compare Lists with FILTER

The FILTER function combined with COUNTIFS enables us to easily compare two lists and return the results we require.

This could be that we want all values that occur in both lists, those that occur in one list and not the other, or even the values that occur in either list but not both.

Let’s look at answering all these queries using the tables shown in Figure 13-23. The first table is named [tblFirst], and the second is named [tblSecond].
Figure 13-23

Two tables containing ID numbers and names

Names That Occur in Both Lists

For this first example, we want to return the [Number] and [Name] columns only for the names that occur in both lists.

In Figure 13-24, the following formula is entered in cell H3:
=SORT(FILTER(tblFirst,
COUNTIFS(tblSecond[Number],tblFirst[Number]),
"No names appear in both lists"
))

It uses the COUNTIFS function to count the occurrences of the ID numbers from [tblFirst] within [tblSecond]. The resulting array is passed to the FILTER function. Remember, any non-zero value evaluates to TRUE. So, if the ID number is found, then FILTER will return the record. The returned results are sorted by the [Number] column in ascending order.

When returning the names that appear in both lists, it does not matter which list we return from, the results will be the same. The following formula returns the names from [tblSecond] that also appear in [tblFirst]:
=SORT(FILTER(tblSecond,
COUNTIFS(tblFirst[Number],tblSecond[Number]),
"No names appear in both lists"
))
Figure 13-24

Formula to return all names that appear in both tables

Names That Occur in One List Only

Let’s now return the names that occur in either the first or second table only.

In Figure 13-25, the following formula returns the names that occur in [tblFirst] only:
=SORT(FILTER(tblFirst,
NOT(COUNTIFS(tblSecond[Number],tblFirst[Number])),
"No names appear in only table 1"
))

The only adaptation that has been made from the previous formula is the addition of the NOT function to reverse the results of the COUNTIFS. It now returns the names in [tblFirst] that do not appear in [tblSecond].

The text “No names appear in only table 1” is entered for the if empty argument to provide a logical response if an empty array is returned.
Figure 13-25

Returning the names that occur in the first table only

As before, this formula can easily be switched to return the names in [tblSecond] that do not appear in [tblFirst]. This is shown in the following formula, and we will see this formula successfully returning the names that occur in the second table only, in the next example:
=SORT(FILTER(tblSecond,
NOT(COUNTIFS(tblFirst[Number],tblSecond[Number])),
"No names appear in only table 2"
))

Names That Occur in Either List but Not Both

We now want to return the names that occur in either [tblFirst] or [tblSecond] only. This is really a table with the appended results of the two formulas from the previous example.

In Figure 13-26, the following formula uses the VSTACK function to append the results of the two formulas. This combined table is then sorted by the first column [Number].
=SORT(VSTACK(FILTER(tblFirst,
NOT(COUNTIFS(tblSecond[Number],tblFirst[Number]))),
FILTER(tblSecond,
NOT(COUNTIFS(tblFirst[Number],tblSecond[Number])))
))
Figure 13-26

Formula returning the names that appear in either of the tables

Return All Names from Both Lists

Finally, let’s use an Excel formula to return a distinct list of all names that appear in both tables.

This example does not require the FILTER function; however, it has been included for a thorough coverage of comparing two lists.

In Figure 13-27, the following formula uses the VSTACK function to stack both tables vertically. Any duplicate values are then removed and sorted in ascending order by the [Number] column (the first column of the returned array).
=SORT(UNIQUE(VSTACK(tblFirst,tblSecond)))
Figure 13-27

Distinct list of all names that occur in both tables

Aggregate FILTER Results

We have seen a few aggregation functions in this book. These include SUMIFS, SUMPRODUCT, and AGGREGATE, to name a few. These amazing functions all have small disadvantages which help you decide which one is best for your chosen task.

Well, let’s add another to the list in terms of the FILTER function. It is an extremely well-rounded function that does not suffer from some of the ailments that the other aggregation functions do.

The FILTER function can be used to perform the logical expressions required and return an array of values for an aggregation function to work on. As we know from this chapter, FILTER can handle AND and OR logic, multiple column and row arrays, and arrays provided by other functions. This makes it a fantastic provider for an aggregation function.

Let’s see a few examples. We will use the table of student scores shown in Figure 13-28 for all examples. This table is named [tblScores].
Figure 13-28

Table of student scores

In Figure 13-29, the following formula is used in cell E3 to return the average for the top five student scores. The LARGE function is used to return the fifth largest score for FILTER to test. The scores that meet this criterion are then returned to AVERAGE to calculate.
=AVERAGE(
FILTER(tblScores[Score],tblScores[Score]>=LARGE(tblScores[Score],5))
)
Figure 13-29

Average the top five values

Figure 13-30 shows the results of two more examples. The following formula is entered in cell H6 to return the maximum score for a student based in the regions entered in E6 (East) or F6 (West):
=MAX(
FILTER(tblScores[Score],(tblScores[Region]=E6)+(tblScores[Region]=F6))
)
And this formula is entered in cell G6 to count the number of students from the regions specified in cells E6 and F6. This is easily done with the ROWS function working of the returned array by FILTER.
=ROWS(
FILTER(tblScores[Score],(tblScores[Region]=E6)+(tblScores[Region]=F6))
)
The FILTER function makes applying logical expressions using OR logic simple. And along with its other strengths, it can be used as the engine for a versatile aggregator.
Figure 13-30

Maximum score from two specified regions

FILTER with Other Excel Features

There are some fantastic ways that the FILTER function can be used with other Excel features. Let’s see some examples of FILTER being used to provide a filtered array for Data Validation and for charts.

Shrinking Drop-Down List

In this first example, we will create a shrinking drop-down list. This is a list that decreases in size each time an item in the list is used. Once an item is used, it is removed from the list to prevent it from being used again.

For this example, we will use the data shown in Figure 13-31. The table on the left is named [tblNames]. It contains names of individuals that we want to assign to the tasks in the table on the right, named [tblTasks].
Figure 13-31

Names to be assigned to tasks

To create the shrinking drop-down list, we will perform a three-step process:
  1. 1.

    Write the required formula and check that it works.

     
  2. 2.

    Define a name for the spill range generated by FILTER.

     
  3. 3.

    Reference the defined name as the source of the Data Validation list.

     
In Figure 13-32, the following formula is entered in cell F2. It returns the names that are not yet assigned to any task:
=FILTER(tblNames[Names],
NOT(COUNTIFS(tblTasks[Assigned],tblNames[Names]))
)

The COUNTIFS function checks if the names occur in [tblTasks]. The NOT function then reverses the logic to show TRUE if the names do not occur and FALSE if they do. FILTER then returns the unused names.

This formula is only displayed beside the tables for testing purposes. Now we are confident that it works, it is moved to another sheet named [List Data]. We will define a name for the spill range and then hide the sheet.
Figure 13-32

Formula to return the remaining names unassigned to any tasks

Figure 13-33 shows the name “lstNames” being defined by referring to the spill range of unassigned names in cell A2 of the [List Data] sheet.
Figure 13-33

Defining a name for the unassigned names’ list

Note

Please refer to Chapter 3 on defined names or Chapter 10 on dynamic arrays if you are unsure on how to define a name for a range.

This name can now be used as the source for the Data Validation list:
  1. 1.

    Select the [Assigned] column of [tblTasks].

     
  2. 2.

    Click DataData Validation.

     
  3. 3.

    On the Settings tab, select List in the Allow field and then enter “=lstNames” in the Source field (Figure 13-34).

     
  4. 4.

    Click OK.

     
Figure 13-34

Creating the shrinking Data Validation list

Figure 13-35 shows the shrinking drop-down list being used to assign names to tasks. Only the remaining unassigned names are shown in the list.
Figure 13-35

Shrinking drop-down list showing unassigned names only

Dependent Drop-Down List

For a second Data Validation example, we will see how the FILTER function offers a neat method of creating dependent drop-down lists.

A dependent drop-down list is when the items shown in a drop-down list are dependent on the item specified in a previous drop-down list.

Figure 13-36 shows a table named [tblProducts]. It contains information about products that we sell. For this example, we are interested in just the [Product] and [Category] columns of this table.

We want to create a drop-down list for the three different categories of product. And then create a second drop-down list for the product names. The second list of product names will be dependent on the selection made in the first list. Only products relating to the specified category will be shown.

The table shown in Figure 13-36 is stored on a worksheet named [Product Data & Lists]. We will use this worksheet to store the spill ranges of both formulas that are used to serve the drop-down lists. To make referencing the spill ranges easier, names will be defined for them and used in the Data Validation rules.
Figure 13-36

Table of product data

In Figure 13-37, the first drop-down list of categories is created in cell B3. The following formula is used to return a distinct list of the categories and fed to the Data Validation list via a defined name:
=SORT(UNIQUE(tblProducts[Category]))
Cell B3 has been named [rngCategory] and will be referenced in the formula for the dependent list.
Figure 13-37

First drop-down list showing the product categories

Now let’s create the dependent list. In Figure 13-38, the following formula returns the product names for the category specified in the first list (Food). That cell is defined as [rngCategory] for the purposes of this tutorial.
=SORT(FILTER(tblProducts[Product],
tblProducts[Category]=rngCategory,
"No category selected"))
Figure 13-38

Formula to return values for the dependent list

The spill range is defined as the name “lstProduct” and then used as the source for the list in the Data Validation rule.
=lstProduct
Figure 13-39 shows the dependent list showing food products only, as that has been stated via the first list.
Figure 13-39

Dependent drop-down list showing food products only

Figure 13-40

Text returned when no selection is made in the previous list

If no selection is made in the previous list, then the text “No category selected” is returned (Figure 13-40). This text was entered for the if empty argument of the FILTER function.

Interactive Chart

The FILTER function can be used to easily add an interactive element to a chart in Excel. In this example, we will use a drop-down list for a user to make a selection, and that selection determines the data to be charted.

For this example, we have a table of product sales data, named [tblProductSales] (Figure 13-41).
Figure 13-41

Table of product sales data

On a separate sheet, we have a drop-down list of the three product categories. The cell containing the drop-down list is named [rngChosenCat]. When a user selects a category, only the products relating to that category will be charted and will be sorted in descending order by their sales total.

Figure 13-42 shows two different formulas producing spill ranges in cells B2 and C2.

The following formula is entered in cell C2 to return the sales total for all products of the chosen category, sorted in descending order:
=SORT(FILTER(
tblProductSales[Total],
tblProductSales[Category]=rngChosenCat),
,-1)
And the following formula is entered in cell B2 to return the product names for the chosen category. The SORTBY function is applied to order the product names by the sales total array in descending order.
=SORTBY(
FILTER(tblProductSales[Product],tblProductSales[Category]=rngChosenCat),
FILTER(tblProductSales[Total],tblProductSales[Category]=rngChosenCat),
-1)
Figure 13-42

Formula to return the arrays to be used for the chart data source

These formulas are entered on a sheet named [Chart Data]. A name has been defined for each spill range so they can be referenced in the chart.

The product name range is defined as “rngProductNames” and the sales total range defined as “rngSalesTotals.” Figure 13-43 shows the product name range being defined.
Figure 13-43

Defining a name for the product name spill range

Now, we will insert the chart and use the defined names for the source of the axis labels and the data to be charted. For this example, a column chart will be used:
  1. 1.

    On the [Dynamic Chart] sheet, click InsertInsert Column or Bar ChartClustered Column.

     
  2. 2.

    With the chart selected, click Chart DesignSelect Data.

     
  3. 3.

    In the Legend Entries (Series) area, click Add.

     
  4. 4.

    Type “Sales Totals” for the Series name (this will become the chart title and can be changed later for something more meaningful).

     
  5. 5.

    Click in the Series values field, remove the content, click a cell on the [Chart Data] sheet, remove the cell reference, and type “rngSalesTotals” after the sheet name (Figure 13-44). Charts require an explicit reference that includes the sheet name. Click OK.

     
Figure 13-44

Adding the sales totals for the data series

  1. 6.

    Click Edit in the Horizontal (Category) Axis Labels area.

     
  2. 7.

    Click in the Axis label range field, click a cell on the [Chart Data] sheet, remove the cell reference, and type “rngProductNames” after the sheet reference. Click OK.

     
Figure 13-45 shows the completed Select Data Source window.
Figure 13-45

Completed Select Data Source window

Note

Excel will actually edit the data series and axis label references in the chart to the workbook name followed by the defined name. But we can let Excel handle that.

Figure 13-46 shows the finished chart. Improvements can and should be made to the chart to take it further.

When the value in the drop-down list is changed, the chart reacts and plots the correct values.
Figure 13-46

Completed chart dependent on drop-down list selection

Summary

In this chapter, we learned the excellent FILTER function of Excel – a very powerful and flexible function. We saw many examples that supported this and complemented the strengths of FILTER compared to other lookup functions. It has changed the game in how we perform certain tasks in modern Excel.

In the next chapter, we will look at using formulas with data types in Excel. Data types are a recent development that is still evolving and finding its place in the Excel jigsaw. They are a rich data type that enables us to store many columns of values in a single cell.

In the chapter, we will discuss in detail the different data types available and how to access their data with some of the most popular functions in Excel, plus a couple of new functions – FIELDVALUE and STOCKHISTORY.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.94.249