Power Query List.Distinct: Remove Duplicates Fast
In this article
List.Distinct returns a new list with duplicate values removed, keeping the first occurrence of each value. You use it inside Power Query (the data preparation engine in Excel and Power BI) by writing a small M formula, not by clicking a button. If you just want to deduplicate a column in the Power Query Editor, the ribbon's Remove Duplicates command is faster; List.Distinct is for when you need the unique values as a list for further processing, such as building a parameter list, counting distinct items, or feeding another function.
What You'll Need
- Excel (Microsoft 365, Excel 2021, or Excel 2019) or Power BI Desktop with Power Query available. In Excel, Power Query is on the Data tab; in Power BI Desktop it is the Transform data button.
- A table or list already loaded into Power Query, or the willingness to type a manual list in the formula bar.
- Basic familiarity with the Power Query Editor: the Queries pane on the left, the formula bar (enable it under View if hidden), and the Applied Steps pane on the right.
- Optional: the Power Query M function reference open in a browser for exact syntax. Labels and menu positions can differ slightly by version, device and region.
Step-by-Step
- Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the Power Query Editor, then connect to your source. Confirm the column you want to deduplicate is present and has the expected data type (for example Text or Whole Number).
- Decide whether you need a list or a column. List.Distinct works on a list. A table column is not a list until you convert it. If your goal is simply to remove duplicate rows from the table, stop here and use Home > Remove Rows > Remove Duplicates instead; that is the built-in command and it is the right tool for table-level deduplication.
- Create a query that produces a list. The cleanest way: right-click your query in the Queries pane and choose Reference (or Duplicate). In the new query, select the column you want, then go to Transform > Convert To List (in some versions it appears under the Transform tab's Convert group). The preview now shows a list rather than a table.
- Open the formula bar and wrap the step in List.Distinct. Suppose the step is named Converted to List and its formula reads
= Table.Column(#"Changed Type", "Region"). Edit it to= List.Distinct(Table.Column(#"Changed Type", "Region"))and press Enter. The preview should now show only unique values, in the order they first appeared. - Verify the count. Add a step that counts the result, for example
= List.Count(#"Distinct List"), or use the status bar in the preview. Compare against the original count to confirm duplicates were actually removed. If the counts match, either there were no duplicates or the values differ by case or whitespace (see Common Mistakes). - Use the list. You can reference it from another query by name, convert it back to a table with Table.FromList if you need to load it, or pass it into functions such as List.Contains or Text.Combine. If you only need the count of unique values, you can skip creating a separate query and use
List.Count(List.Distinct(yourList))directly. - Load or close. In Excel, choose Close & Load > Close & Load To and pick where the result goes. In Power BI Desktop, choose Close & Apply. Remember that a bare list cannot be loaded as a table without converting it first.
Common Mistakes
- Passing a table instead of a list. List.Distinct expects a list. If you feed it a table or a column reference that has not been converted, you get an error or unexpected results. Convert the column first.
- Expecting case-insensitive matching. By default List.Distinct is case-sensitive, so "apple" and "Apple" are treated as different values. If you need case-insensitive deduplication, normalise the text first with Text.Lower or Text.Upper, then apply List.Distinct.
- Ignoring trailing spaces. "North " and "North" are distinct to the function. Clean the values with Text.Trim before deduplicating.
- Assuming order is preserved. List.Distinct keeps the first occurrence and the original order, but if you sort the list before deduplicating, the "first" occurrence changes. Sort only after you are happy with which duplicates survive.
- Using it to deduplicate a table. For removing duplicate rows from a table, use the Remove Duplicates command. Wrapping a whole table in List.Distinct is not supported and is not what the function is for.
- Forgetting that null is a value. Nulls are treated as distinct values and one null will remain in the output. If you do not want nulls, filter them out before or after.
Tips
- Name your steps clearly in the Applied Steps pane. A step called "Distinct Regions" is far easier to debug than "Custom1".
- To count unique values without building a separate query, use
List.Count(List.Distinct(Table.Column(Source, "ColumnName")))in a custom step. - For a case-insensitive result, combine cleaning steps:
List.Distinct(List.Transform(myList, Text.Lower))gives lowercased unique values; trim first if whitespace is a concern. - If you need unique values from multiple columns combined, merge the columns into one list first (for example with List.Combine) and then apply List.Distinct.
- Keep a reference query for the original list so you can compare counts and confirm the function did what you expected.
Troubleshooting
- Error: "We cannot convert a value of type Table to type List." You passed a table. Convert the column to a list first, or use Remove Duplicates on the table instead.
- No duplicates removed. Check for case differences, leading or trailing spaces, and non-printing characters. Apply Text.Trim and Text.Lower (or Text.Upper) before List.Distinct and re-check the count.
- Unexpected values missing. Confirm you are deduplicating the correct column and that earlier steps (filters, merges, type changes) have not already altered the data. Step through the Applied Steps one at a time to see where values change.
- Formula bar not visible. Turn it on under the View tab in the Power Query Editor. Without it you cannot edit the M formula directly.
- Result will not load. A list is not a table. Convert it with Table.FromList, or load the original table after using Remove Duplicates.
- Refresh fails after renaming a step. M formulas reference step names in quotes. If you rename a step, update every formula that refers to it, or use the Advanced Editor to check for broken references.
Frequently Asked Questions
Is List.Distinct case-sensitive?
Yes, by default. "Apple" and "apple" are treated as different values. Normalise case first if you need them merged.
Does List.Distinct remove nulls?
No. Null is treated as a value, and one null remains in the output. Filter nulls out before or after if you do not want them.
Can I use List.Distinct on a table column directly?
Not directly. Convert the column to a list first (Transform > Convert To List), or use the Remove Duplicates command for table-level deduplication.
Does it preserve the original order?
It keeps the first occurrence of each value in the order they appear. Sorting before deduplicating changes which duplicate is kept.
How do I count unique values?
Wrap the list in List.Count: List.Count(List.Distinct(myList)). This returns the number of unique items.
Can I deduplicate across multiple columns?
Combine the columns into one list first (for example with List.Combine), then apply List.Distinct to the combined list.