List.Contains Power Query: How to Use It
In this article
The short answer: List.Contains in Power Query checks whether a specific value exists inside a list, and it returns true if the value is found, or false if it is not. You can use it inside a custom column, a conditional column, or within other M functions to filter rows, validate data, or create flags. To use it, you need a list (which can be a column of lists, a manually typed list, or a list generated by functions like Table.Column or Text.Split) and the value you want to search for. The function is case-sensitive by default, but you can make it case-insensitive by adding a third argument or by transforming the data beforehand.
What You'll Need
- Power Query Editor open (in Excel or Power BI). You can get there in Excel via Data > Get Data > Launch Power Query Editor, or in Power BI via Home > Transform Data.
- A data source that contains either a column of lists or a column of text values that you can split into lists. If you don't have a list column, you can create one using
Text.Splitor by adding a custom column that produces a list. - Basic familiarity with adding custom columns in Power Query (the Add Column > Custom Column dialog).
Step-by-Step
- Open Power Query Editor and load the table you want to work with. For example, let's say you have a table with a column called Items that contains comma-separated text like "apple, banana, cherry".
- Convert the text to a list (if needed). Select the column, go to Transform > Split Column > By Delimiter, choose comma, and then in the advanced options select Split into Rows or Split into Columns. But to use
List.Containswith a list, you might want to keep the list in a single cell. Instead, add a custom column that usesText.Split:Text.Split([Items], ", ")gives you a list. Name this column ItemList. - Add a custom column to test for a value. Go to Add Column > Custom Column. In the formula box, type:
List.Contains([ItemList], "banana"). This returnstruefor rows where "banana" is in the list, andfalseotherwise. - Use it in a filter if you want to keep only rows that contain a value. After adding the custom column, filter that column to
trueby clicking the filter icon on the column header and uncheckingfalse. Then you can remove the helper column if you don't need it. - Use it in a conditional column for more complex logic. Instead of a custom column, you can add a conditional column from Add Column > Conditional Column. In the dialog, set the condition to use a custom formula? Actually, conditional columns don't directly support
List.Contains; you need to add a custom column for that. So stick with the custom column approach. - Handle case sensitivity. By default,
List.Containsis case-sensitive, so "Banana" won't match "banana". To make it case-insensitive, you can use the third argument:List.Contains([ItemList], "banana", Comparer.OrdinalIgnoreCase). Or you can transform both the list and the value to lowercase usingList.TransformandText.Lower. - Use it with a literal list if you don't have a column of lists. For example, to check if a value in a column Product is in a fixed set: . This is useful for grouping or categorizing.
- Combine with other functions like
List.ContainsAnyorList.ContainsAllif you need to check multiple values.List.ContainsAny([ItemList], {"apple", "pear"})returns true if any of the listed values appear.List.ContainsAll([ItemList], {"apple", "banana"})returns true only if all values appear.
Common Mistakes
- Forgetting that the second argument is a single value, not a list. If you write
List.Contains([ItemList], {"apple", "banana"}), it will look for a list that contains a list, which will almost always return false. UseList.ContainsAnyfor multiple values. - Using a text column directly instead of a list.
List.Containsexpects a list as the first argument. If you pass a text string, Power Query will throw an error. Convert the text to a list first. - Ignoring case sensitivity when your data has inconsistent casing. This leads to unexpected false results. Use
Comparer.OrdinalIgnoreCaseor normalize the data. - Not handling null values. If your list contains nulls or the value you're searching for is null,
List.Containsmay return false or error. UseList.Containswithtryor check for nulls beforehand. - Using
List.Containson a column of records or tables without extracting the field first. If your list items are records, you need to specify the field:List.Contains(List.Transform([ListColumn], each _[FieldName]), "value").
Tips
- Use
Table.Containsfor row-level checks if you need to check if a whole row exists in another table.List.Containsis for lists, not tables. - Optimize performance by avoiding
List.Containson very large lists (thousands of items) inside loops. Instead, consider merging tables or usingList.Containson a distinct list. - Use
List.Containswitheachfor complex conditions, like checking if any item in a list meets a condition:List.Contains([ItemList], "apple", (x, y) => x = y)is basic, but you can write custom comparers for fuzzy matching. - Combine with
Text.Containsfor partial matches if you need to check if a string contains a substring, not an exact list item.Text.Containsworks on text, not lists. - Use
List.Containsin a custom function to reuse the logic across multiple queries. Create a function that takes a list and a value, and returns a boolean.
Troubleshooting
If List.Contains is not working as expected, follow these steps:
- Check the data type of the first argument. Ensure it's actually a list, not a text string. You can see the data type in the column header (List icon). If it's text, you need to split it.
- Verify the exact value. The function is exact-match, so "apple" won't match "Apple" or "apples". Use
Comparer.OrdinalIgnoreCasefor case-insensitive matching, or trim whitespace withText.Trim. - Look for nulls or errors in the list. If any item in the list is an error,
List.Containsmight propagate the error. UseList.Transformto replace errors with a sentinel value, or useList.Containswith a try-catch. - Test with a simple example. Create a new query with a hardcoded list like
{1,2,3}and checkList.Contains({1,2,3}, 2)to confirm the function works. Then adapt it to your data. - Check the comparer argument. If you're using a custom comparer, make sure it returns a logical value (true/false) and not a number. The comparer should be a function that takes two arguments and returns a logical.
- If you get an error like "Expression.Error: We cannot convert the value ... to type List", that means the first argument is not a list. Convert it using
Text.Split,List.Combine, or by referencing a list column.
Frequently Asked Questions
Is List.Contains case-sensitive?
Yes, by default it is case-sensitive. To make it case-insensitive, use the third argument: List.Contains(list, "value", Comparer.OrdinalIgnoreCase).
Can I use List.Contains to check if a value is in a column of a table?
No, List.Contains works on lists, not tables. To check if a value exists in a column, use Table.Contains(Table.SelectRows(YourTable, each [ColumnName] = "value")) or simply filter the table.
How do I check if a list contains any of several values?
Use List.ContainsAny. For example, List.ContainsAny({1,2,3}, {2,5}) returns true because 2 is in both.
What does List.Contains return if the list is empty?
It returns false, because there is nothing to match. If the list is null, it returns null (or an error depending on context).
Can I use List.Contains in a filter step in Power Query?
Yes, you can add a custom column with List.Contains and then filter that column to true. Alternatively, you can use Table.SelectRows with a custom function: Table.SelectRows(YourTable, each List.Contains([ListColumn], "value")).
Is there a way to do a partial match with List.Contains?
No, List.Contains does exact matches only. For partial matches, you can use List.Transform with Text.Contains and then check if any result is true: List.Any(List.Transform([ListColumn], each Text.Contains(_, "substring"))).