Smart Systems for Home

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

List.Contains Power Query: How to Use It

· 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 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.Split or 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

  1. 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".
  2. 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.Contains with a list, you might want to keep the list in a single cell. Instead, add a custom column that uses Text.Split: Text.Split([Items], ", ") gives you a list. Name this column ItemList.
  3. 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 returns true for rows where "banana" is in the list, and false otherwise.
  4. 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 true by clicking the filter icon on the column header and unchecking false. Then you can remove the helper column if you don't need it.
  5. 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.
  6. Handle case sensitivity. By default, List.Contains is 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 using List.Transform and Text.Lower.
  7. 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.
  8. Combine with other functions like List.ContainsAny or List.ContainsAll if 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. Use List.ContainsAny for multiple values.
  • Using a text column directly instead of a list. List.Contains expects 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.OrdinalIgnoreCase or normalize the data.
  • Not handling null values. If your list contains nulls or the value you're searching for is null, List.Contains may return false or error. Use List.Contains with try or check for nulls beforehand.
  • Using List.Contains on 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.Contains for row-level checks if you need to check if a whole row exists in another table. List.Contains is for lists, not tables.
  • Optimize performance by avoiding List.Contains on very large lists (thousands of items) inside loops. Instead, consider merging tables or using List.Contains on a distinct list.
  • Use List.Contains with each for 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.Contains for partial matches if you need to check if a string contains a substring, not an exact list item. Text.Contains works on text, not lists.
  • Use List.Contains in 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:

  1. 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.
  2. Verify the exact value. The function is exact-match, so "apple" won't match "Apple" or "apples". Use Comparer.OrdinalIgnoreCase for case-insensitive matching, or trim whitespace with Text.Trim.
  3. Look for nulls or errors in the list. If any item in the list is an error, List.Contains might propagate the error. Use List.Transform to replace errors with a sentinel value, or use List.Contains with a try-catch.
  4. Test with a simple example. Create a new query with a hardcoded list like {1,2,3} and check List.Contains({1,2,3}, 2) to confirm the function works. Then adapt it to your data.
  5. 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.
  6. 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"))).