Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query List.RemoveMatchingItems Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The List.RemoveMatchingItems function in Power Query (M language) is used to remove all elements from a list that match a specific criteria defined by a comparison function. Unlike List.RemoveItems, which requires a static list of values to delete, List.RemoveMatchingItems allows you to use a logical test (a predicate) to decide which items should be discarded, making it an essential tool for dynamic data cleaning and conditional filtering of lists.

What You'll Need

  • Microsoft Excel (with Power Query) or Power BI Desktop.
  • A data source that can be converted into a List format (such as a column from a table).
  • Access to the Advanced Editor or a Custom Column formula box to write M code.
  • A basic understanding of how lists operate in Power Query (lists are denoted by curly brackets { }).

Step-by-Step

  1. 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.
  2. Identify Your Target List: Ensure you have a list to work with. If your data is currently in a table, you can convert a column to a list by right-clicking the column header and selecting "Drill Down". Alternatively, you can create a manual list in the Advanced Editor using { "Apple", "Banana", "Cherry", "Date" }.
  3. Initiate a Custom Step: Click the fx icon next to the formula bar to create a new custom step. This allows you to write the M function directly.
  4. Write the Function Syntax: The basic syntax for the function is List.RemoveMatchingItems(list, criteria).
    • The first argument is the list you want to filter.
    • The second argument is a function that returns a logical value (true or false). If the function returns true for an item, that item is removed.
  5. Define the Comparison Function: Because the second argument must be a function, you typically use the each keyword. For example, if you want to remove all items that start with the letter "A", your code would look like this: = List.RemoveMatchingItems(Source, each Text.StartsWith(_, "A")). In this context, the underscore (_) represents the current item being evaluated in the list.
  6. Apply Complex Logic: You can use logical operators like and, or, and not to refine your removals. For instance, to remove items that are either "Null" or empty strings, use: = List.RemoveMatchingItems(Source, each _ = null or _ = "").
  7. Verify the Result: Press Enter. Power Query will evaluate the list and display the remaining items. Ensure that only the items that failed the criteria function remain in the list.
  8. Convert Back to Table (Optional): If you need the result back in a table format, go to the Transform tab and select "To Table".

Common Mistakes

One of the most frequent errors is confusing List.RemoveMatchingItems with List.RemoveItems. List.RemoveItems expects a second list of values to be removed (e.g., List.RemoveItems({1,2,3}, {1,3}) results in {2}). If you provide a logical function to List.RemoveItems, it will result in a type error because it is looking for a list, not a predicate. Always remember: RemoveItems is for specific values; RemoveMatchingItems is for conditions.

Another common mistake is forgetting the each keyword. In M, each is shorthand for creating a function. Without it, Power Query will not know how to iterate through the list to apply the logic to every single element, leading to an expression error stating that the argument cannot be converted to a function.

Case sensitivity is also a major pitfall. Power Query is case-sensitive. If you write each _ = "apple", it will not remove "Apple" or "APPLE". To avoid this, wrap your comparison in Text.Lower or Text.Upper, such as each Text.Lower(_) = "apple".

Tips

To make your queries more maintainable, avoid hard-coding values inside the List.RemoveMatchingItems function. Instead, create a separate parameter or a small reference table containing the values you wish to exclude. You can then reference that parameter within your logic. This allows users to change the filtering criteria without opening the Advanced Editor.

Combine this function with List.Select for advanced filtering. While List.RemoveMatchingItems removes what matches the criteria, List.Select keeps what matches. If your logic is becoming too complex with not operators, it may be easier to switch to List.Select to define what you want to keep rather than what you want to discard.

When dealing with large lists, be mindful of the complexity of the function you pass as the second argument. Heavy text manipulations inside the each block can slow down the refresh time of your report, especially if the list contains tens of thousands of entries. Try to perform basic data cleaning (like trimming whitespace) on the entire column before converting it to a list for matching.

Troubleshooting

If you encounter a "Expression.Error: We cannot convert the value to type Function" error, check your second argument. This almost always means you provided a value or a list instead of a function. Ensure you have used the each keyword or a defined function variable.

If the function runs but does not remove any items when you believe it should, check for hidden characters. Leading or trailing spaces are common culprits. Try wrapping the current item in Text.Trim: each Text.Trim(_) = "YourValue". If the list contains non-text values (like numbers or dates), ensure you aren't using text functions like Text.StartsWith without first converting the value using Text.From(_).

If you receive a null value error, it is likely because your list contains null entries and your comparison function cannot handle them. For example, Text.StartsWith will error if it encounters a null. To fix this, add a null check at the beginning of your criteria: each _ <> null and Text.StartsWith(_, "A").

Frequently Asked Questions

How is this different from filtering a table column?

Filtering a table column removes entire rows from the table. List.RemoveMatchingItems operates on a List object, which is a single-column array. It is used when you have already drilled down into a list or are performing calculations within a custom column that requires list manipulation.

Can I remove items based on a number range?

Yes. You can use mathematical operators in the criteria. For example, List.RemoveMatchingItems(MyList, each _ < 10 or _ > 100) will remove all numbers that are not between 10 and 100.

Does this function modify the original list?

No. In Power Query, all operations are immutable. The function returns a new list with the items removed, leaving the original source list unchanged. You must assign the result to a new step name to see the changes in your query.

Can I use this to remove duplicates?

While you can technically write logic to do this, List.Distinct is the correct and much more efficient function for removing duplicates from a list.

Can I use multiple conditions?

Yes, by using and or or. For example, each _ = "Red" or _ = "Blue" will remove any item that is either Red or Blue.