Power Query List.ContainsAny: Step-by-Step Guide
In this article
List.ContainsAny checks whether a list contains any of the values from another list. It returns true if at least one match is found, and false otherwise. This is useful for filtering rows where a column's list of values overlaps with a set of target values, such as finding records that mention any of several keywords. Before you start, you need Power Query available in Excel or Power BI Desktop. The function is written in M code, so you will work in the Power Query Editor's formula bar or Advanced Editor. No special add-ins are required.
What You'll Need
- Excel 2016 or later, or Power BI Desktop (any recent version).
- A dataset loaded into Power Query (e.g., from a table, CSV, or database).
- Basic familiarity with the Power Query Editor interface.
Step-by-Step
- Open the Power Query Editor. In Excel, go to Data > Get Data > Launch Power Query Editor. In Power BI Desktop, click Transform Data on the Home ribbon. Menu labels can differ slightly by version or region.
- Load your data. Select the table or range you want to work with. If you are starting from scratch, use Data > From Table/Range (Excel) or Get Data (Power BI).
- Identify the column to check. Suppose you have a column named "Tags" that contains lists of text values (e.g., {"red","blue"}). You want to flag rows where any tag matches a target list like {"red","green"}.
- Add a custom column. Go to Add Column > Custom Column. In the formula box, enter a formula using List.ContainsAny. The syntax is:
List.ContainsAny(list1, list2, optional equationCriteria). For example:List.ContainsAny([Tags], {"red","green"}). This returns true if any element of [Tags] is in {"red","green"}. - Name the new column (e.g., "HasMatch") and click OK. The column will be added with true/false values.
- Filter or use the result. You can filter the new column to keep only true rows, or use it in further calculations. To filter, click the filter arrow on the column header and select true.
- Handle case sensitivity. By default, List.ContainsAny is case-sensitive. To make it case-insensitive, you can use
Comparer.OrdinalIgnoreCaseas the third argument:List.ContainsAny([Tags], {"red","green"}, Comparer.OrdinalIgnoreCase). - Apply and close. Click Close & Load (Excel) or Close & Apply (Power BI) to load the results.
Common Mistakes
- Passing a single value instead of a list. List.ContainsAny expects both arguments to be lists. If you pass a text value, you'll get an error. Wrap single values in braces: {"value"}.
- Forgetting case sensitivity. The default is case-sensitive, so "Red" and "red" are different. Use the optional comparer if needed.
- Using List.Contains instead. List.Contains checks for one specific value, not any from a list. Use List.ContainsAny for multiple possible matches.
- Not handling nulls. If either list is null, the function returns false. Ensure your lists are not null, or handle nulls before calling the function.
- Misunderstanding the return value. It returns true if at least one match is found, not the matching value itself. To get the matching values, use List.Intersect.
Tips
- Use List.ContainsAny with a dynamic list, such as a column from another table, by referencing that column's list. For example,
List.ContainsAny([Tags], TargetList)where TargetList is a list of values. - Combine with List.Transform to check multiple columns or lists.
- For large datasets, List.ContainsAny is efficient because it stops at the first match.
- Test your formula on a small subset first to ensure it behaves as expected.
- Remember that the function is not case-insensitive by default; always specify the comparer if case matters.
Troubleshooting
If you get an error like "We cannot convert a value of type Text to type List," check that both arguments are lists. If the function returns false unexpectedly, verify that the lists are not null and that case sensitivity is not causing misses. If the custom column dialog shows an error, use the Advanced Editor to inspect the code for syntax issues. If performance is slow, consider filtering earlier in the query to reduce the number of rows. If you need to check for any match across multiple columns, use List.ContainsAny on a combined list.
Frequently Asked Questions
What is the difference between List.Contains and List.ContainsAny?
List.Contains checks if a list contains a specific value. List.ContainsAny checks if a list contains any of the values from another list.
Can I use List.ContainsAny with a column reference?
Yes, as long as the column contains lists. For example, [Tags] where each row is a list.
Is List.ContainsAny case-sensitive?
Yes, by default. Use Comparer.OrdinalIgnoreCase as the third argument for case-insensitive matching.
What happens if the lists are empty?
If either list is empty, the function returns false.
Can I get the actual matching values?
No, List.ContainsAny returns a boolean. Use List.Intersect to get the common values.
Does List.ContainsAny work with numbers?
Yes, it works with any type as long as both lists contain compatible types.