To filter a table by a list in Power Query, you must convert your filter criteria into a List object and then use the List.Contains function within a custom filter step. This process allows you to dynamically filter a large dataset based on a separate table of values, meaning you can update your filter list without manually checking boxes in the filter menu every time your data refreshes.
What You'll Need
- A primary data table (the dataset you want to filter).
- A secondary table containing the list of values you want to keep (the filter list).
- Access to Power BI Desktop or Microsoft Excel with Power Query installed.
- Basic familiarity with the Power Query Editor interface.
Step-by-Step
- Import Your Data: Load both your main dataset and your filter criteria table into the Power Query Editor. If your filter list is in an Excel range, ensure it is formatted as a Table (Ctrl+T) before importing it via Data > Get Data.
- Convert the Filter Table to a List: Power Query cannot directly compare a column to another table; it needs a List. Select your filter table in the Queries pane on the left. Right-click the header of the column that contains the values you want to filter by and select "Drill Down". This action transforms the table into a List object, which is indicated by a different icon in the Queries pane. Rename this query to something clear, such as
FilterList. - Return to the Main Table: In the Queries pane, select the primary data table that you intend to filter.
- Apply an Initial Filter: To generate the correct syntax in the formula bar, apply a dummy filter. Click the filter dropdown arrow on the column you wish to filter, uncheck "Select All", and check any single value. Click OK. This creates a "Filtered Rows" step in the Applied Steps pane.
- Modify the M Code: Look at the formula bar at the top of the screen. You will see a formula that looks like this:
= Table.SelectRows(#"Previous Step", each ([ColumnName] = "SomeValue")). You need to replace the equality check([ColumnName] = "SomeValue")with a list containment check. - Insert the List.Contains Function: Change the formula to:
= Table.SelectRows(#"Previous Step", each List.Contains(FilterList, [ColumnName])). In this formula,FilterListis the name of the list query you created in Step 2, and[ColumnName]is the name of the column in your main table. - Execute and Verify: Press Enter. Power Query will now evaluate every row in your main table and keep only those where the value in the specified column exists within your
FilterList. - Close and Apply: Once the data is filtered correctly, click "Close & Apply" to load the filtered results into your report or spreadsheet.
Common Mistakes
One of the most frequent errors is attempting to use the List.Contains function while the filter criteria is still a Table rather than a List. If you skip the "Drill Down" step, Power Query will return an error stating that it cannot convert a value of type Table to type List. Always ensure the icon for your filter query in the left-hand pane looks like a list (a small bulleted list icon) rather than a table (a grid icon).
Another common mistake is ignoring case sensitivity. Power Query is case-sensitive by default. If your main table has "Apple" and your filter list has "apple", the row will be filtered out. To resolve this, you must transform both the column and the list to lowercase or uppercase using the "Format" menu before applying the filter, or use a custom comparer in the M code.
Users also frequently forget to rename their filter list. If you leave the query name as "Table2", your formula will be List.Contains(Table2, [ColumnName]). While this works, it becomes confusing in complex models with many queries. Using descriptive names like ExcludedProductList or TargetRegionList makes the M code much easier to audit.
Tips
To create an "Exclude" filter (a NOT filter), simply add the not operator before the List.Contains function in the formula bar. The syntax becomes: = Table.SelectRows(#"Previous Step", each not List.Contains(FilterList, [ColumnName])). This is incredibly useful for removing a dynamic list of blacklisted IDs or discontinued products from a report.
If you are dealing with very large datasets (millions of rows), List.Contains can sometimes be slow because it evaluates the list for every single row. To improve performance, consider using a "Merge" operation instead. Perform an Inner Join between your main table and the filter table. This often leverages the database engine's optimization (Query Folding) more effectively than a custom M function, resulting in faster refresh times.
For those who want to avoid the formula bar entirely, you can use the "Merge Queries" feature. By merging the main table with the filter table using a "Left Semi Join", Power Query will automatically keep only the rows that have a match in the filter table. This achieves the exact same result as List.Contains but can be done entirely through the user interface.
Troubleshooting
If you encounter a "Token Literal Expected" or "Expression Error", check your syntax in the formula bar. Ensure that the query name (e.g., FilterList) is spelled exactly as it appears in the Queries pane, including capitalization. If your query name contains spaces, it must be wrapped in quotes and preceded by a hashtag, like this: #"Filter List".
If the filter is returning zero rows but you know there are matches, check for trailing or leading spaces. A value of "New York " (with a space) will not match "New York". To fix this, select the column in both the main table and the filter list, right-click, and select Transform > Trim. This removes invisible whitespace that often causes matching failures.
If you see a "Data Type Mismatch" error, ensure that both the column in the main table and the values in your filter list are the same data type. For example, if the main table stores IDs as Text but the filter list stores them as Whole Numbers, the List.Contains function will fail to find matches. Use the data type icon next to the column header to align them both to Text or both to Number.
Frequently Asked Questions
Can I use a filter list from an external file?
Yes. You can import the filter list from a CSV, an Excel file on SharePoint, or a SQL database. As long as the data is loaded into Power Query as a table and then converted to a list via "Drill Down", the List.Contains method will work regardless of the data source.
Does this method work for multiple columns?
To filter based on multiple columns, you can use the and or or operators within the Table.SelectRows function. For example: each List.Contains(List1, [Col1]) and List.Contains(List2, [Col2]). This allows for complex, multi-dimensional filtering based on several dynamic lists.
Will the filter update automatically when the list changes?
Yes. Because the filter is based on a query, any changes made to the source of the filter list will be reflected in the main table the next time you click "Refresh". You do not need to reopen the Power Query Editor to update the filtered results.
Is there a limit to how many items can be in the filter list?
There is no hard limit on the number of items, but performance may degrade if the list contains tens of thousands of entries. For extremely large filter lists, the "Merge" (Inner Join) approach is significantly more efficient than List.Contains.
Can I use this to filter by a date range?
While List.Contains is for exact matches, for date ranges you should use comparison operators like . In this case, your filter "list" would actually be two separate single-value queries (parameters) representing the start and end dates.