Power Query List.MatchesAny Function: Usage and Guide
In this article
The List.MatchesAny function in Power Query (M language) returns a logical value (TRUE or FALSE) indicating whether at least one element in a list satisfies a specified condition. To use it, you provide the function with a list and a predicate function (a conditional test); if any single item in that list makes the predicate TRUE, the entire function returns TRUE. This is most commonly used in custom columns to flag rows that contain at least one matching value from a related list or to validate data against a set of rules.
What You'll Need
- Microsoft Excel (with Power Query) or Power BI Desktop.
- A dataset containing at least one column that can be treated as a list, or a separate list object created in the Power Query editor.
- Basic familiarity with the Power Query "Add Custom Column" interface.
- A basic understanding of M language syntax, specifically how to write a simple "each" expression.
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 Data: Ensure you have a table where one of the columns contains lists. If your data is currently in a standard table format, you may need to group rows or use a merge operation to create a list of values associated with a specific ID. For example, if you have a table of Orders and a table of Order Items, you might group the Order Items by Order ID to create a list of all products in that order.
- Create a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This opens the Custom Column dialog box where you will write the M formula.
- Define the Function Syntax: In the Custom Column formula box, start by typing
List.MatchesAny(. This function requires two arguments: the list you are checking and the condition you are testing. - Specify the List: For the first argument, select the column that contains your list. For example, if your column is named "ProductList", the formula starts as
List.MatchesAny([ProductList], .... - Write the Predicate (The Condition): The second argument must be a function. In Power Query, the easiest way to do this is using the
eachkeyword. For example, if you want to check if any item in the list is equal to "Urgent", you would write:each _ = "Urgent". The underscore (_) represents the current item being evaluated in the list. - Complete the Formula: Close the parentheses. Your final formula should look like this:
List.MatchesAny([ProductList], each _ = "Urgent"). - Apply and Verify: Click OK. Power Query will generate a new column containing TRUE for every row where at least one item in the list matched "Urgent", and FALSE for rows where no match was found.
- Filter or Transform: You can now use the filter dropdown on this new TRUE/FALSE column to isolate only the rows that met your criteria, or use a Conditional Column to turn those TRUE/FALSE values into descriptive text like "Priority" or "Standard".
Common Mistakes
One of the most frequent errors is attempting to use List.MatchesAny on a text column instead of a list column. If your column contains a comma-separated string (e.g., "Apple, Orange, Banana") rather than an actual Power Query List object, the function will return an error. You must first convert that string into a list using Text.Split([ColumnName], ", ") inside the function call, like so: List.MatchesAny(Text.Split([ColumnName], ", "), each _ = "Apple").
Another common mistake is case sensitivity. Power Query is case-sensitive by default. If you search for "Urgent" but the data contains "urgent", List.MatchesAny will return FALSE. To avoid this, you should wrap the current item in a text transformation function, such as each Text.Lower(_) = "urgent".
Users also frequently forget the each keyword. Without each, Power Query does not know how to iterate through the list elements, leading to a syntax error. The predicate must be a function, and each is the shorthand way to create that function in M.
Tips
To make your List.MatchesAny logic more dynamic, avoid hard-coding values like "Urgent" directly into the formula. Instead, create a separate parameter or a small reference table containing your "Keywords of Interest." You can then reference that list in your formula. This allows you to update your search criteria without editing the M code in every custom column.
For more complex matching, you can combine List.MatchesAny with other text functions. For instance, if you want to check if any item in a list contains a specific word rather than matching it exactly, use Text.Contains: List.MatchesAny([ProductList], each Text.Contains(_, "Red")). This is incredibly powerful for scanning product descriptions or category tags for specific keywords.
If you find yourself needing to check if all items in a list meet a condition rather than just one, simply swap List.MatchesAny for List.MatchesAll. The syntax remains identical, but the logic changes from an "OR" operation to an "AND" operation.
Troubleshooting
If you encounter an "Expression.Error: We cannot convert the value ... to type List" error, it means the column you are referencing is not actually a list. Check the icon next to the column header in the Power Query editor. A list column will show a small list icon or the word "List" in the cell. If it shows "abc" or "123", it is a scalar value. Use { [ColumnName] } to wrap a single value in a list or Text.Split to turn a string into a list.
If the function returns FALSE when you are certain there is a match, check for leading or trailing whitespace in your data. A value like "Urgent " (with a space) will not match "Urgent". To fix this, use Text.Trim within the predicate: each Text.Trim(_) = "Urgent".
If the query becomes very slow on large datasets, consider whether you can achieve the same result using a Merge operation (Join) between two tables. While List.MatchesAny is convenient for small to medium lists, joining tables is often more optimized by the Power Query engine (Query Folding) when connecting to SQL databases.
Frequently Asked Questions
Is List.MatchesAny the same as a VLOOKUP?
No. A VLOOKUP finds a specific value and returns a corresponding value from another column. List.MatchesAny is a logical test that only tells you if a condition is met (TRUE/FALSE) anywhere within a list.
Can I use this function to compare two different lists?
Yes. You can use it to see if any item in List A exists in List B. The syntax would look like: List.MatchesAny(ListA, each List.Contains(ListB, _)). This effectively performs an intersection check.
Does this function work with numbers?
Yes. It works with any data type. For numbers, you can use mathematical operators: List.MatchesAny([PriceList], each _ > 100) will return TRUE if any number in the list is greater than 100.
What happens if the list is empty?
If the list is empty, List.MatchesAny will always return FALSE because there are no elements available to satisfy the predicate condition.
Can I use multiple conditions in the predicate?
Yes. You can use and or or logic within the each statement. For example: each (_ = "Urgent" or _ = "Critical") will return TRUE if either of those values is found in the list.