Power Query: Keep the First Occurrence of Each Row
In this article
To keep only the first occurrence of each value in Power Query, the reliable approach is to let Power Query number the rows within each group and then filter that numbering to 1. In the Power Query Editor, select the column (or columns) that define a duplicate, use Home > Group By to build an all-rows table for each group, add an index column inside a custom column with Table.AddIndexColumn, expand the rows back out, then keep only rows where the index equals 0. That gives you exactly one row per distinct value, in the order the rows originally appeared, with all other columns intact. Menu labels can differ slightly between Power BI Desktop and Excel versions, but the logic is identical in both.
Rows are more often "first" in the sense of "first as loaded" than "first by date." If you want the first by date, price or some other rule, sort that column before you group, because Power Query respects the sort order of the table when it builds each group's nested table. Getting that order wrong is the single most common reason people end up with the wrong row kept.
What You'll Need
- A table loaded into Power Query (Data > From Table/Range in Excel, or Get Data in Power BI Desktop), or loaded from a file or database.
- A clear idea of which column or columns define a duplicate (for example, a Customer ID or an Email address).
- If "first" means something other than load order, a column you can sort by to make that order unambiguous.
- A backup of the source file or a copy of the query, so you can go back if the result is not what you expected.
Step-by-Step
- Load the data and check it. Open Power Query (in Excel: Data > Get Data > From Table/Range; in Power BI: Home > Transform data). Scroll the preview and confirm which columns actually hold the values you want deduplicated. Watch for leading or trailing spaces and inconsistent capitalisation, because "John@example.com" and "john@example.com " will count as two different values.
- Clean the key column first. Right-click the header of the key column and choose Transform > Trim, then Transform > Clean. If case should not matter, use Transform > Format > lowercase on a duplicate helper column so you do not destroy the display text of the original. Doing this before grouping prevents near-duplicates slipping through as separate groups.
- Set the order that means "first." If the order you want is not the order the data arrived in, click the filter arrow on the relevant column (usually a date, a timestamp or a sequence number) and sort ascending or descending. Power Query keeps the sort order of each group's nested table, so this is what decides which row survives. If the rows already arrive in the right order, leave them alone.
- Group the rows. Select the key column (hold Ctrl or Shift to select more than one), then choose Home > Group By. In the dialog, give the new column a name such as AllRows, leave the operation as All Rows and click OK. You now have one row per distinct key, plus a column containing a nested table of every row that belonged to that group.
- Add an index inside each nested table. Choose Add Column > Custom Column. Name it something like RowNum and use a formula of the form
Table.AddIndexColumn([AllRows], "Index", 0, 1). Be careful: in the dialog, select the name of the nested table column from the list of available columns rather than typing it wrongly, so it is inserted with the correct reference. Click OK. Each group now holds its rows numbered starting from 0 in the order they were sorted. - Expand the nested tables back out. Click the double-arrow icon in the header of the new indexed column and choose Expand. Untick the option to use the original column name as a prefix if you want the original column names back, then select the columns you need. You should now see every original row again, with an Index column attached.
- Filter to the first row of each group. Click the filter arrow on the Index column, untick everything, tick only 0 and click OK. Because numbering started at zero, index 0 is the first row of each group. If your formula started at 1, filter for 1 instead.
- Remove the helper columns. Right-click the Index column header and choose Remove, and remove the group-key columns you no longer need. Rename the query and, when the preview matches what you expect, choose Home > Close & Load (Excel) or Close & Apply (Power BI) to write the result out.
- Verify the count. Compare the row count of the new query with the distinct count of the key column. They should match. In Excel, the status bar shows how many rows are selected; in Power BI, the table preview shows the row count at the bottom.
Common Mistakes
- Sorting after grouping. Once the groups exist, sorting the outer table does nothing to the order inside each nested table. Any sort that decides "first" must happen before Group By.
- Grouping on the wrong columns. Grouping only on an ID when the duplicate check should include a date or region will silently merge unrelated rows.
- Forgetting that the index starts at zero. Filtering for 1 when the index starts at 0 keeps the second occurrence of every group and drops the first one.
- Untrimmed text. Invisible spaces, non-breaking spaces copied from web pages, and mixed case all create extra groups. Trim, clean and normalise case first.
- Using Remove Duplicates without checking sort order. The right-click Remove Duplicates command keeps the first row in the current order, but that order may be arbitrary after merges and expansions, so the row that survives is unpredictable. It also cannot deduplicate on one column while keeping another column's values with any particular rule.
- Hard-coding column names in the custom column. If the grouped column is renamed upstream, the formula breaks. Use the column name exactly as it appears and re-check the step after any upstream renaming.
Tips
- To keep the last occurrence instead, sort the deciding column descending before grouping, or keep the maximum index per group.
- If you only need a distinct list of values and no other columns, selecting the column and choosing Home > Remove Rows > Remove Duplicates is simpler and faster.
- Rename the custom column step in the Applied Steps list (for example, "Add index within group") so the query reads clearly months later.
- For repeated use, turn the logic into a reusable query or function rather than rebuilding it per table.
- When the key is a combination (name plus postcode, for example), group on all those columns together rather than concatenating them into a single string.
Troubleshooting
The result still has duplicates. The key column probably contains hidden differences: trailing spaces, non-breaking spaces, or mixed case. Add a temporary column that trims and lowercases the key, group on that instead, then remove it.
The wrong row survived. Check where the sort sits in the Applied Steps. Move the sort step above the Group By step, or change its direction, and refresh the preview.
The Expand step shows errors or missing columns. This usually means the nested table column referenced in the custom column was renamed or the wrong column was picked in the dialog. Delete the custom column step and recreate it, selecting the nested column from the list.
Rows disappeared entirely. If you filtered the index for 0 but your formula began at 1, the filter matches nothing. Also check that no earlier step filtered rows you needed.
The query is slow. Grouping on a very large table with all rows nested is memory-hungry. If you can, fold the work back to the source (for example, a SQL DISTINCT or a window query) or reduce rows and columns before grouping.
If that did not work, reload the query from a saved copy, rebuild the steps one at a time and check the preview after each step. That isolates which step changes the result.
Frequently Asked Questions
Does this keep the first row in the original file order?
Yes, provided you do not sort beforehand and the table has not been reordered by merges or expansions. Power Query preserves the order it received unless a step changes it.
Can I deduplicate on more than one column?
Yes. Select all the key columns before choosing Group By. A row counts as a duplicate only when every selected column matches.
What is the difference between Remove Duplicates and this method?
Remove Duplicates keeps the first row it encounters, but only over the whole row or the selected columns. The group-and-index method lets you choose the surviving row by a rule, such as the latest date, while keeping other columns intact.
Does the index column need to start at zero?
No, but it must match your filter. A formula starting at 0 is filtered for 0; one starting at 1 is filtered for 1.
Will this work in Excel and Power BI?
Yes. The engine is the same; only the menu names differ slightly, for example Close & Load in Excel versus Close & Apply in Power BI.
Can I keep the last occurrence instead?
Yes. Sort the deciding column the other way before grouping, or filter for the maximum index within each group.