Smart Systems for Home

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

Power Query List.ContainsAll: How to Check Lists

· 5 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

Power Query's List.ContainsAll function checks whether a source list contains every item from a target list. It returns a logical value—true if all items are present, and false if even one is missing. This is essential for validating data completeness, such as ensuring a customer order includes all required product SKUs before processing.

What You'll Need

  • A Power BI Desktop file or an Excel workbook with Power Query enabled.
  • Two lists: a Source List (the larger dataset containing potential matches) and a Target List (the specific items you want to verify exist).
  • Basic familiarity with the Power Query Editor interface.

Step-by-Step

  1. Open the Power Query Editor. In Power BI Desktop, go to Home > Transform Data. In Excel, navigate to Data > Get Data > Launch Power Query Editor. Ensure your source data is loaded into a query.
  2. Prepare your Source List. If your source data is in a table column, select that column. Right-click the header and choose Transform > To List. Rename this new query to SourceList for clarity. This converts the column of values into a single list object.
  3. Prepare your Target List. Create a separate query for the items you need to check. If these are static values, use Home > New Source > Blank Query. In the formula bar, type a list manually, for example: = {"ItemA", "ItemB", "ItemC"}. Rename this query to TargetList.
  4. Create a validation query. Add a new blank query. Name it ValidationCheck. This query will perform the comparison logic.
  5. Write the M code. In the formula bar of ValidationCheck, enter the following syntax: = List.ContainsAll(SourceList, TargetList). Note that the first argument is the container (where we look), and the second is the content (what we look for). Press Enter to apply.
  6. Verify the result. The output should be a single cell displaying either true or false. If it shows true, every item in TargetList exists within SourceList. If false, at least one item is missing.
  7. Apply conditional logic (Optional). To use this result in a broader workflow, wrap it in an if statement. For example: = if List.ContainsAll(SourceList, TargetList) then "Complete" else "Missing Items". This allows you to flag rows or datasets based on the validation outcome.

Common Mistakes

The most frequent error is swapping the arguments. Remember that List.ContainsAll(list1, list2) asks: "Does list1 contain everything in list2?" If you reverse them, you might get false simply because the smaller list doesn't contain the extra items found in the larger source list, which is not the intended behavior for subset checking.

Another common issue involves data type mismatches. If your source list contains text values like "100" but your target list contains numbers like 100, the function will return false. Power Query does not automatically coerce types during this comparison. Always ensure both lists share the same data type (Text, Number, Date, etc.) before running the check.

Users often forget to handle null values. If the TargetList contains a null entry, List.ContainsAll will only return true if the SourceList also explicitly contains a null. Unexpected nulls can cause false negatives, so clean your data beforehand using Remove Empty or similar transformations.

Tips

For large datasets, performance can degrade if you run this check row-by-row in a table. Instead, aggregate your data first. Convert columns to lists once, perform the List.ContainsAll check, and then merge the result back if needed. This minimizes the computational overhead of repeated list scanning.

If you need to know which items are missing rather than just a true/false flag, combine List.ContainsAll with List.Difference. Use if List.IsEmpty(List.Difference(TargetList, SourceList)) then true else false. While slightly more complex, List.Difference returns the actual missing items, allowing you to log errors or display specific feedback to the user.

Consider caching your lists. If the SourceList is static (e.g., a reference table of valid codes), load it into a dedicated query and disable its "Load to" setting. This keeps the memory footprint low while making the list available for multiple validation queries without reloading the data each time.

Troubleshooting

If you receive an error stating "Expression.Error: We cannot convert...", check your data types immediately. Click on the column headers in the preview pane to see the current type. Force a conversion by selecting the column, going to Transform > Data Type, and choosing the correct format for both source and target lists.

If the function always returns false despite visual confirmation that items match, inspect for hidden whitespace. Text values like "Apple " (with a trailing space) are not equal to "Apple". Apply Text.Trim to your source and target lists before converting them to list objects. You can do this via Transform > Format > Trim on the original columns.

In cases where the query fails to load or times out, break down the problem. Test List.ContainsAll with small, hard-coded lists first. If that works, gradually introduce real data. This isolates whether the issue is logical (syntax/arguments) or performance-related (data volume).

Frequently Asked Questions

Does List.ContainsAll care about order?

No. The function checks for presence, not sequence. A target list of {"A", "B"} will return true against a source list of {"Z", "B", "A"} because both items exist, regardless of their position.

How is this different from List.Contains?

List.Contains checks if a single value exists in a list. List.ContainsAll checks if every value in a second list exists in the first. Use Contains for simple membership tests and ContainsAll for subset validation.

Can I use this with tables instead of lists?

Not directly. The function expects two list arguments. You must first convert the relevant table columns into lists using To List or by referencing the column names in a custom column expression that extracts the values.

What happens if the target list is empty?

If the second argument (TargetList) is an empty list {}, the function returns true. Logically, an empty set is a subset of any other set, meaning there are no items to fail the check.

Is List.ContainsAll case-sensitive?

Yes, for text values. "apple" and "Apple" are treated as different items. To ignore case, convert both lists to lowercase using List.Transform(..., Text.Lower) before running the comparison.