Power Query M Filter: How to Filter Data Using M Code
In this article
Filtering in Power Query M is achieved using the Table.SelectRows function. This function takes a table as its first argument and a logical condition (a predicate) as its second argument, returning a new table that contains only the rows where the condition evaluates to true.
While the Power Query user interface (UI) creates these filters automatically when you click a column header, writing or editing the M code manually allows you to create dynamic filters, use complex "OR" logic, and reference parameters or other queries that the standard checkboxes cannot handle.
What You'll Need
- Microsoft Excel or Power BI Desktop.
- A dataset already loaded into the Power Query Editor.
- Basic familiarity with the Power Query interface.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Access the Formula Bar: Ensure the formula bar is visible. If it is not, go to the View tab and check the box for Formula Bar. (Note: Menu labels may vary slightly depending on your software version or region).
- Create an Initial Filter: To see how the M code is structured, click the filter arrow on a column header and select any value. This generates a
Table.SelectRowsstep in your Applied Steps list. - Enter the Advanced Editor: For complex filters, go to the Home tab and click Advanced Editor. This opens the full M script for your query.
- Write the Filter Syntax: Locate the step where you want to filter. The basic syntax for a filter is:
#"Filtered Rows" = Table.SelectRows(#"Previous Step Name", each [ColumnName] = "DesiredValue") - Apply Logical Operators: To filter based on multiple conditions, use the following operators within the
eachstatement:- AND: Use
andto require both conditions to be true. Example:each [Status] = "Active" and [Amount] > 100 - OR: Use
orto require at least one condition to be true. Example:each [Region] = "North" or [Region] = "South" - NOT: Use
notto exclude specific values. Example:each not ([Category] = "Internal")
- AND: Use
- Implement Case-Insensitive Filters: M is case-sensitive. To filter regardless of capitalization, wrap the column reference in Text.Lower or Text.Upper . Example:
- Save and Apply: Click Done in the Advanced Editor, then click Close & Apply on the Home tab to load the filtered data into your model.
Common Mistakes
- Case Sensitivity: Forgetting that "Apple" and "apple" are different in M. This is the most common reason filters return empty tables.
- Incorrect Step Referencing: In the Advanced Editor, the first argument of
Table.SelectRowsmust be the exact name of the previous step (including quotes and the hashtag if the name contains spaces, e.g.,#"Changed Type"). - Mixing Data Types: Attempting to filter a number column using a text string (e.g.,
[Price] = "10"instead of[Price] = 10) will result in an error. - Using SQL Syntax: Using
WHEREor<>(for not equal) instead of the M-specificeachand<>operators.
Tips
- Use Parameters: Instead of hard-coding a value like
"North", create a Power Query Parameter. Replace the value in your M code with the parameter name:each [Region] = ParameterRegion. This allows users to change the filter without opening the editor. - Combine with List.Contains: If you have a long list of values to filter by, avoid multiple
orstatements. UseList.Containsinstead:each List.Contains({"Value1", "Value2", "Value3"}, [ColumnName]). - Filter Early: Place your filter steps as early as possible in the Applied Steps list. This enables "Query Folding," allowing the data source (like SQL Server) to do the filtering before the data even reaches your computer, significantly increasing performance.
Troubleshooting
If your filter is not working as expected, try these steps in order:
- Check for Trailing Spaces: Use the UI to "Trim" the column before filtering. A value like "Active " (with a space) will not match "Active".
- Verify Data Types: Check the icon next to the column header. Ensure a "Decimal Number" column is not being filtered as "Text".
- Test with a Single Row: Simplify your formula to a single, basic condition (e.g.,
each [ID] = 1) to verify the step is referencing the previous table correctly. - Check for Nulls: If your column contains null values, a standard filter might exclude them. Use
each [ColumnName] = "Value" or [ColumnName] = nullif you need to keep blanks.
Frequently Asked Questions
Is Power Query M case-sensitive?
Yes, M is strictly case-sensitive. Both the function names (e.g., Table.SelectRows) and the data values within the columns must match the case exactly.
Can I filter a table based on a value in another table?
Yes. You can convert a column from another table into a list using the syntax OtherTable[ColumnName] and then use List.Contains within your Table.SelectRows function.
What is the difference between the UI filter and the M code filter?
There is no functional difference in the result; the UI simply writes the M code for you. However, writing the code manually allows for dynamic logic and parameters that the UI checkboxes cannot produce.
How do I filter for dates in M?
Use the #date(year, month, day) intrinsic. For example: each [DateColumn] > #date(2023, 1, 1).
Why is my query running slowly after adding a filter?
This usually happens if "Query Folding" is broken. If you perform a complex transformation (like a custom M function) before the filter, Power Query must download the entire dataset and filter it locally rather than asking the server to do it.