Power Query List.MatchesAll Function Guide
In this article
The List.MatchesAll function in Power Query (M language) is used to determine if every single item in a list satisfies a specific condition defined by a predicate function. It returns a logical TRUE if all elements meet the criteria, or FALSE if at least one element fails the test. This is essentially a "universal quantifier" for your data, allowing you to validate entire sets of records before proceeding with a transformation or filtering step.
What You'll Need
- Microsoft Excel (with Power Query enabled) or Power BI Desktop.
- A dataset that contains lists (either as actual List objects in M or as columns that you intend to convert into lists).
- Basic familiarity with the Power Query Formula Bar and the Advanced Editor.
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.
- Prepare Your List: Ensure you have a list to evaluate. If your data is in a table column, you can create a list by referencing that column (e.g.,
Source[ColumnName]) or by using a custom column that generates a list for each row. - Insert a Custom Column: Navigate to the Add Column tab on the ribbon and select Custom Column. This is where you will write the
List.MatchesAlllogic. - Define the Function Syntax: The basic syntax for the function is
List.MatchesAll(list as list, condition as function). You must provide the list you want to check and a function (usually a lambda or a defined function) that returns a true/false value for each item. - Write the Predicate Function: Use the
eachkeyword to define the condition. For example, if you want to check if every number in a list is greater than 10, your formula would look like:List.MatchesAll([MyListColumn], each _ > 10). The underscore (_) represents the current item being evaluated in the list. - Execute and Verify: Click OK. Power Query will now iterate through every item in the list for every row. If every single item in the list is greater than 10, the cell will show TRUE. If even one item is 10 or less, it will show FALSE.
- Filter Based on Results: Now that you have a TRUE/FALSE column, click the filter arrow on the column header to keep only the rows where the entire list matched your criteria.
- Clean Up: Once the filtering is complete, right-click the custom helper column and select Remove to keep your final table clean.
Common Mistakes
One of the most frequent errors is confusing List.MatchesAll with List.AnyTrue or List.MatchesAny. While List.MatchesAll requires 100% compliance, List.MatchesAny only requires a single item to meet the condition to return TRUE. Using the wrong one will lead to overly restrictive or overly permissive data filtering.
Another common mistake is ignoring case sensitivity when dealing with text. If your condition is each _ = "Completed", an item that says "completed" (lowercase) will cause the entire List.MatchesAll function to return FALSE. To avoid this, use Text.Lower or Comparer.OrdinalIgnoreCase within your predicate function.
Users often forget that List.MatchesAll expects a list as its first argument. If you accidentally pass a single value (like a text string or a number) instead of a list object, Power Query will throw a Type Error stating that the value cannot be converted to a list. Always verify that your column contains the { } list notation in the preview window.
Lastly, attempting to use List.MatchesAll on a null list can produce unexpected results. Depending on the context, an empty list may return TRUE because there are no elements that fail the condition (vacuous truth). If your data contains nulls or empty lists, you must handle those cases explicitly using an if statement before calling the function.
Tips
To make your formulas more readable and maintainable, consider creating a separate named function in the Advanced Editor for complex conditions. Instead of writing a long each statement inside the custom column, you can call your named function: List.MatchesAll([MyList], MyCustomValidationFunction).
Combine List.MatchesAll with List.Transform if you need to modify the data before checking the condition. For example, if you need to trim whitespace from every item in a list before checking if they are all equal to a certain value, you can wrap the list argument: List.MatchesAll(List.Transform([MyList], each Text.Trim(_)), each _ = "Active").
For performance optimization on very large datasets, try to filter your rows as much as possible before applying List.MatchesAll. Because this function must iterate through every element of every list in every row, it can become a bottleneck if applied to millions of records with long lists.
Use the List.Select function as a debugging tool. If List.MatchesAll is returning FALSE and you don't know why, create a temporary column using List.Select([MyList], each not (_ > 10)). This will return a list of only the items that caused the failure, making it easy to spot the "offending" data point.
Troubleshooting
If the function returns an error, first check the data type of the column being passed. If the column type is "Any" or "Text," but it looks like a list, ensure it is actually a List object and not a comma-separated string. If it is a string, use Text.Split([Column], ",") to convert it into a list first.
If you are seeing Expression.Error: We cannot convert the value... to type List, it means the function encountered a cell that is not a list. You can wrap your function in a try-otherwise block or use an if statement to check for nulls: if [MyListColumn] = null then false else List.MatchesAll([MyListColumn], each _ > 10).
If the results are unexpectedly TRUE for empty lists, this is standard M behavior. To ensure that empty lists are treated as FALSE, add a condition to check the list count: if List.Count([MyListColumn]) > 0 and List.MatchesAll([MyListColumn], each _ > 10) then true else false.
If the query is running extremely slowly, check if you are performing a "heavy" operation inside the each predicate. For instance, calling another external function or performing a complex merge inside List.MatchesAll will multiply the execution time by the number of items in every list. Simplify the predicate to basic logical comparisons whenever possible.
Frequently Asked Questions
What is the difference between List.MatchesAll and List.MatchesAny?
List.MatchesAll returns TRUE only if every single item in the list meets the condition. List.MatchesAny returns TRUE if at least one item in the list meets the condition.
Can I use List.MatchesAll with text values?
Yes. You can check if all text items in a list start with a certain character, match a specific word, or meet a minimum length requirement using Text functions within the predicate.
Does List.MatchesAll work with null values inside the list?
It depends on your condition. If your condition is each _ <> null, it will return TRUE only if there are no nulls. If your condition is each _ > 10 and the list contains a null, the comparison will result in an error or FALSE, causing the whole function to return FALSE.
How do I check if all items in a list are identical?
The most efficient way is to compare the list to a list containing only its first element repeated, or more simply, check if the number of distinct items is 1: List.Count(List.Distinct([MyList])) = 1. While List.MatchesAll can do this, it requires referencing the first element as the constant for the predicate.
Is List.MatchesAll case-sensitive?
The function itself is not case-sensitive, but the each condition you write is. Standard M comparisons (like =) are case-sensitive. Use Text.Lower or Comparer.OrdinalIgnoreCase to handle case-insensitive matches.