Power Query Table.Distinct: Step-by-Step Guide
In this article
Table.Distinct removes duplicate rows from a Power Query table, keeping the first occurrence of each unique row (or each unique combination of the columns you specify). It is the function behind the Remove Duplicates button and is available in Excel Power Query, Power BI, and any tool that hosts the Power Query engine.
What You'll Need
- Power Query Editor open in Excel or Power BI Desktop with a table loaded.
- Basic familiarity with the Power Query formula bar and the Advanced Editor.
- For column-specific deduplication, know which columns define a duplicate.
Step-by-Step
- Load your data into Power Query: in Excel, select your table and go to Data > From Table/Range; in Power BI, use Home > Transform data to open the editor.
- Choose the table you want to deduplicate in the Queries pane. The steps below assume a single table; if you have multiple, repeat for each.
- To remove duplicates across all columns (exact duplicate rows), right-click the table in the Queries pane and choose Remove Duplicates, or use the Remove Duplicates button on the Home tab. This inserts a Table.Distinct step.
- To deduplicate based on specific columns, first select those columns (Ctrl+click for multiple), then use Remove Duplicates. Power Query will keep the first row for each unique combination of the selected columns.
- To write the function manually, open the Advanced Editor (Home > Advanced Editor) and add a step like
= Table.Distinct(PreviousStep, {"Column1", "Column2"}). Replace PreviousStep with your step name and list the columns that define uniqueness. - If you want to remove duplicates across all columns manually, use
= Table.Distinct(PreviousStep)without a column list. - Click Done in the Advanced Editor, then check the data preview. The step name appears in the Applied Steps list; you can rename it for clarity.
- Close & Load (Excel) or Close & Apply (Power BI) to apply the change to your destination table.
Menu labels can differ slightly between Excel versions, Power BI versions, and regions, but the Remove Duplicates command and the Table.Distinct function are consistent.
Common Mistakes
- Assuming Table.Distinct keeps the last row. It keeps the first occurrence in the current sort order. If you need the latest record, sort descending by a date column before deduplicating.
- Forgetting that the column list is case-sensitive by default. Table.Distinct treats "Apple" and "apple" as different unless you set the optional EquationCriteria argument to ignore case (see Tips).
- Using Remove Duplicates on the wrong columns. If you select only one column, you will lose entire rows that share that value, even if other columns differ. Always confirm which columns define a duplicate.
- Expecting Table.Distinct to remove duplicates across multiple tables. It works on a single table; combine tables first if needed.
- Ignoring nulls. Null values are treated as equal to each other, so rows with nulls in the key columns may be deduplicated unexpectedly.
Tips
- To make deduplication case-insensitive, use the optional third argument:
Table.Distinct(PreviousStep, {"Name"}, Comparer.OrdinalIgnoreCase). This treats "Apple" and "apple" as duplicates. - Sort your table before deduplicating to control which row is kept. Use Table.Sort as a prior step.
- If you need to keep all unique rows but also want to know which were removed, add an index column before deduplicating and compare counts.
- For large tables, deduplicate early in the query to improve performance.
- Rename the step to something meaningful like "Removed Duplicate Customers" for easier auditing.
Troubleshooting
If Table.Distinct does not remove the duplicates you expect, check the following:
- Data types: A number stored as text (e.g., "123" vs 123) will not be treated as equal. Ensure columns have consistent types.
- Trailing spaces: "Apple " and "Apple" are different. Use Text.Trim on text columns before deduplicating.
- Case sensitivity: As noted, use Comparer.OrdinalIgnoreCase if needed.
- Step order: If you deduplicate before cleaning data, duplicates may persist. Move the step after cleaning steps.
- Error rows: Rows with errors in key columns may not deduplicate as expected. Remove errors first.
- Preview vs. load: The preview may show limited rows; the actual load applies to all rows. Verify with a row count.
If the function returns an error, check that the column names in the list exactly match the table's column names (including spaces and capitalization).
Frequently Asked Questions
Does Table.Distinct remove all duplicate rows or just adjacent ones?
It removes all duplicate rows across the entire table, not just adjacent ones. The first occurrence in the current order is kept.
Can I use Table.Distinct on multiple columns?
Yes. Pass a list of column names as the second argument, e.g., {"Column1", "Column2"}. Rows are considered duplicates only if all listed columns match.
What is the difference between Table.Distinct and Remove Duplicates button?
The button inserts a Table.Distinct step using the columns you have selected. They are functionally the same.
Does Table.Distinct preserve the original row order?
It preserves the order of the first occurrences. If you need a specific order, sort the table before deduplicating.
Can I make Table.Distinct case-insensitive?
Yes, by supplying a comparer such as Comparer.OrdinalIgnoreCase as the third argument.
Will Table.Distinct remove rows with null values?
Nulls are treated as equal, so rows with nulls in the key columns may be deduplicated. If you need to keep nulls, handle them before deduplicating.