Skip to content

Filtering data with list inputs

A filter set up in the Power Query interface has its values written into the query, so changing them means editing the query. This page shows how to filter against a list kept in a worksheet table instead, so that anyone can change the criteria and refresh.

The data is a short table of clothing items and quantities:

An Excel table with a Clothing column listing Trousers-Tall, Trousers-Short, T-Shirt-Large, T-Shirt-Medium, Shorts-Small and Shorts-Medium, and a Quantity column

The aim is to remove every row whose Clothing value begins with Trousers or Shorts. Those two words should live in a separate table on the worksheet, where they can be changed without opening Power Query:

A one-column Excel table headed Exclude, holding Trousers and Shorts above two empty rows

  • Load the Exclude table into Power Query. Its empty rows come through as nulls:

The Exclude column in the Power Query Editor, with Trousers and Shorts in rows 1 and 2 and null in rows 3 and 4

  • Use the filter button on the column header to remove the nulls.
  • Click the fx button beside the formula bar to add a step, and change its formula to the following, which takes the Exclude column out of the table as a list:
= #"Filtered Rows"[Exclude]

The query now returns a list rather than a table:

A list with two items, Trousers and Shorts

  • Name this query filterList.
  • Load the clothing table into Power Query as a second query:

The clothing table in the Power Query Editor, with its six rows of Clothing and Quantity

  • Name this query Data.

Each Clothing value has to be tested against every item in filterList, and a function is the natural way to repeat a test like that. Create a blank query and enter this code in the Advanced Editor:

The Advanced Editor showing a function named fnStartsWithFilter that takes rcd as record and field as text, and returns List.AnyTrue of List.Transform over filterList with Text.StartsWith

The function works like this:

  • Parameters. It takes two arguments: rcd, which is a record holding one row of the data, and field, which is the name of the field in that record to test.
  • List.Transform. It goes through filterList and, for each item, tests whether the field’s value starts with that item, which gives a list of TRUE and FALSE values with one entry per item:
    • For Trousers-Short tested against {Trousers, Shorts}, the result is {TRUE, FALSE}.
    • For T-Shirt-Medium, the result is {FALSE, FALSE}.
  • List.AnyTrue. It returns TRUE if any value in that list is TRUE, so the function as a whole returns TRUE when the value starts with any item in the list.
  • The underscore. Inside Text.StartsWith, the _ stands for the item of filterList that List.Transform is currently testing.

Name the query fnStartsWithFilter.

Go back to the Data query and add a custom column called Filter with this formula:

The Custom Column dialog with the new column name Filter and the formula fnStartsWithFilter(_, “Clothing”)

Here the _ stands for the current row of Data as a record, which is what the function’s first parameter expects, and "Clothing" names the field to test.

The new column shows TRUE for every row that matches the list:

The Data query with a Filter column showing TRUE for the trousers and shorts rows and FALSE for the two T-shirt rows

  • Because the matching rows are the ones to exclude, filter the Filter column to keep only FALSE.
  • Remove the Filter column, which is no longer needed.

Only the rows that match nothing in the list remain:

The Data query reduced to the T-Shirt-Large and T-Shirt-Medium rows

To check that the filter follows the list, change the Exclude table to Trousers and T-Shirt, then refresh the query. The output now holds the shorts instead:

Three tables side by side: the original clothing data, the Exclude table holding Trousers and T-Shirt, and the refreshed output holding Shorts-Small and Shorts-Medium