Power Query List.Intersect Function: A Complete Guide
In this article
The List.Intersect function in Power Query is used to return a list containing only the items that appear in every single list provided to the function. Essentially, it performs a mathematical intersection of multiple sets, filtering out any value that is not present in all specified lists. To use it, you must provide a list of lists as the input argument.
What You'll Need
- Microsoft Excel (with Power Query/Get & Transform) or Power BI Desktop.
- Two or more columns of data that you wish to compare, or existing lists created within the Power Query editor.
- Basic familiarity with the Power Query Formula Bar and the Advanced Editor.
Step-by-Step
- Load Your Data: Open Power Query by going to the Data tab in Excel and selecting Get Data, or by opening Power BI Desktop and clicking Transform Data. Ensure you have the tables or lists you want to compare loaded into the editor.
- Convert Columns to Lists:
List.Intersectrequires lists, not tables. If your data is in table columns, you must reference the column specifically. For example, if you have a table named "SalesTable" and a column named "CustomerIDs", the list reference isSalesTable[CustomerIDs]. - Create a Custom Step: In the Power Query editor, go to the Add Column tab and select Custom Column, or click the
fxicon next to the formula bar to create a new step. - Write the Function Syntax: Enter the function using the following structure:
List.Intersect({List1, List2, List3}). Note the curly brackets{}inside the parentheses; these are critical because they wrap your individual lists into a single list of lists. - Example Implementation: If you are comparing a list of "Current Customers" and "Past Customers", your formula would look like this:
List.Intersect({"Current Customers"[ID], "Past Customers"[ID]}). - Convert the Result Back to a Table: The output of
List.Intersectis a List object. To use this as a table for further reporting, click the "To Table" button in the Transform ribbon. - Define the Column Type: Once converted to a table, the column will likely be labeled "Column1" with a "Any" data type. Click the data type icon in the column header and select the appropriate type (e.g., Text or Whole Number) to ensure data integrity.
Common Mistakes
The most frequent error when using List.Intersect is forgetting the curly brackets. The function expects a single argument: a list of lists. If you write List.Intersect(ListA, ListB), Power Query will throw an error because it thinks you are trying to pass two separate arguments to a function that only accepts one. You must write List.Intersect({ListA, ListB}).
Another common mistake is ignoring case sensitivity. Power Query is case-sensitive. If List A contains "Apple" and List B contains "apple", List.Intersect will treat these as different values and will not include them in the final result. This often leads users to believe the function is broken when it is actually working exactly as designed.
Users also frequently attempt to use this function on entire tables rather than specific columns. List.Intersect cannot compare two tables to find matching rows; it compares lists of values. To find matching rows across tables, a Merge operation (Inner Join) is the correct tool, not this function.
Tips
To handle the case-sensitivity issue mentioned above, transform your lists to a consistent case before intersecting them. You can do this by using List.Transform with Text.Lower or Text.Upper. For example: List.Intersect({List.Transform(ListA, each Text.Lower(_)), List.Transform(ListB, each Text.Lower(_))}). This ensures that "Apple" and "apple" are both treated as "apple" and successfully intersected.
If you have a dynamic number of lists (for example, lists stored in a table of lists), you can pass the column containing those lists directly into List.Intersect without the curly brackets, as the column itself is already a list of lists. This allows your intersection logic to scale automatically as you add more lists to your source data.
For performance optimization on very large datasets, ensure that your lists are sorted or filtered for nulls before performing the intersection. While List.Intersect is efficient, reducing the volume of data passed into the function can speed up the refresh time of your query, especially when dealing with hundreds of thousands of rows.
Troubleshooting
If you receive a "Expression.Error: We cannot convert the value... to type List" error, check your brackets. Ensure you have the { } enclosing your lists. If the error persists, verify that the items you are referencing are actually lists and not tables or records. You can check this by clicking on the step in the Applied Steps pane and looking at the icon next to the result in the preview window; a list icon looks like a small bulleted list, whereas a table icon looks like a grid.
If the function returns an empty list when you know there should be matches, first check for leading or trailing spaces in your text data. A value like "ID123 " (with a space) will not match "ID123". Use the "Trim" transformation on your source columns before applying the intersection to remove invisible whitespace.
If your Power Query editor becomes unresponsive or extremely slow during the intersection of very large lists, try to perform a "Remove Duplicates" operation on each individual list first. List.Intersect processes every element; by removing duplicates from the input lists, you reduce the number of comparisons the engine has to perform, which can significantly lower memory consumption.
Frequently Asked Questions
Does List.Intersect remove duplicates from the result?
Yes, the resulting list contains only the unique values that are present in all the input lists. Even if a value appears multiple times in the source lists, it will only appear once in the intersection result.
Can I use List.Intersect with more than two lists?
Yes. You can include as many lists as needed within the curly brackets, such as List.Intersect({List1, List2, List3, List4, List5}). The function will only return values that exist in every single one of those lists.
What happens if one of the lists is empty?
If any one of the lists provided to the function is empty, the result will always be an empty list. This is because there are no values that can possibly exist in an empty list, meaning no value can be common to all lists.
Is List.Intersect better than using a Merge Join?
It depends on the goal. Use List.Intersect when you need a simple list of common values for a calculation or a filter. Use a Merge Join (Inner Join) when you need to combine multiple columns from different tables based on a matching key.
Can List.Intersect compare numbers and text?
The function can compare them, but it will not treat them as the same. A number 100 and a text "100" are different data types and will not be considered a match. Ensure all lists have the same data type before intersecting.