Power Query List.Difference Function: How to Use It
In this article
The List.Difference function in Power Query returns items from the first list that are not present in the second list. It’s useful for comparing datasets, removing duplicates across tables, or isolating unique records. No special setup is required beyond having Power Query enabled in Excel or Power BI.
What You'll Need
- Microsoft Excel 2016 or later, or Power BI Desktop
- Two lists of data (columns, tables, or manually entered values)
- Basic familiarity with Power Query Editor (M language is not required but helpful)
Step-by-Step
- Open Excel or Power BI Desktop and load the data you want to compare into Power Query. Go to Data > Get Data > From Table/Range (in Excel) or Get Data > From Table/Range (in Power BI) to import your source tables.
- In Power Query Editor, identify the two columns or lists you want to compare. For example, one might be a list of all customers, and another might be a list of customers who made a purchase this month. Ensure both lists contain comparable data types (e.g., both are text or both are numbers).
- Create a new blank query by going to Home > New Source > Blank Query. This will open a new query window with no data.
- Open the Advanced Editor by going to View > Advanced Editor. Delete any default code and enter:
List.Difference({"Apple", "Banana", "Cherry"}, {"Banana", "Date"}). This example returns {"Apple", "Cherry"}. - Click Done to close the Advanced Editor. The result will appear as a single-column table with the difference. Rename the column if needed by right-clicking the header and selecting Rename.
- To compare actual columns from existing queries, replace the hard-coded lists with references. For example, if your first list is named "AllCustomers" and the second is "ActiveCustomers", enter:
List.Difference(AllCustomers[CustomerID], ActiveCustomers[CustomerID]). Ensure both column references return a list type — if they don’t, use the "Extract Values" option from the column’s dropdown menu. - Apply the function to a custom column if you want to compare row-by-row within a table. Right-click the column you want to modify, select Add Column > Custom Column, and enter:
List.Difference({[Column1]}, {[Column2]}). Be cautious: this only works if each row contains a single value, and the result will be a list per row. - To convert the result back into a table with individual rows, click the double-arrow icon next to the column header and select "Expand to New Rows". This turns a list of differences into separate rows for easier analysis or export.
- Click Close & Load to return the result to your worksheet or report. The output will show only the items from the first list that don’t appear in the second.
Common Mistakes
- Using different data types: Comparing text to numbers (e.g., "123" vs 123) will return the full first list because Power Query treats them as unequal. Always ensure consistent types.
- Referencing non-list columns: If you reference a column directly without converting it to a list, you’ll get an error. Use
Table.Column(YourTable, "ColumnName")to extract a list from a table column. - Expecting case-insensitive matching: List.Difference is case-sensitive. "Apple" and "apple" are treated as different items. Use Text.Lower() or Text.Upper() on both lists before comparison if case doesn’t matter.
- Trying to compare empty lists: If either list is null or empty, the function returns the first list unchanged or null. Always validate input lists with
if List.IsEmpty(...) then ... else ...if needed. - Assuming order matters: List.Difference does not preserve order from the original list. The result order is arbitrary. If order is critical, sort the result afterward using List.Sort.
Tips
- Use List.Difference to clean up master lists before merging. For example, remove all customers already invoiced from your prospect list before sending new outreach.
- Combine with List.Intersect or List.Union for more complex comparisons. List.Difference can be nested:
List.Difference(List.Difference(List1, List2), List3)finds items unique to List1 only. - Use the "Show Row Numbers" option in Power Query to track which original row each difference came from, especially useful for debugging.
- For large datasets, avoid using List.Difference in custom columns row-by-row — it’s slow. Instead, extract both columns as lists, apply the function once, then merge the result back.
- Save your query steps with descriptive names like "Find New Customers" so others can follow your logic later.
- Test with small datasets first. Use just 5–10 sample rows to verify your function works before applying it to thousands of records.
Troubleshooting
- Error: "Expression.Error: We cannot apply operator - to types List and List" — This means you’re using subtraction instead of List.Difference. Always use the full function name.
- Result is empty when it shouldn’t be — Check for leading/trailing spaces. Use Text.Trim() on both lists before comparing. Also verify data types — a number stored as text won’t match a number.
- Function returns null — One of your input lists might be null. Wrap your function in an if statement:
if List.IsEmpty(List1) or List.IsEmpty(List2) then {} else List.Difference(List1, List2). - Cannot find the Advanced Editor — Make sure you’re in Power Query Editor (not Excel). Go to View > Advanced Editor. If missing, restart Excel or Power BI.
- Expanded results show blanks — If you expanded a list column and see empty rows, some list items may have been null. Filter out nulls before expanding using the filter dropdown.
- Performance is slow — If you’re comparing large tables, avoid using List.Difference in custom columns. Instead, extract both lists using Table.Column, apply the function, then merge the result using a join or merge operation.