Power Query: Group Rows but Keep All Columns
In this article
Power Query's Group By command normally collapses your table to just the grouping column and the aggregated result, discarding every other column. To keep all columns while grouping, you add a temporary All Rows column during grouping, then expand the nested tables and remove duplicates or re-expand the specific fields you want. The short version: Group By > choose your key column > add an aggregation of type All Rows > expand the resulting Table column > remove the helper column and duplicates. This guide walks through that exact sequence, explains why it works, and covers the traps that make people think Power Query cannot do it.
What You'll Need
- Power BI Desktop, Excel (Power Query is built into Excel 2016 and later on Windows; Mac Excel has a reduced Power Query), or another host that runs Power Query.
- A table already loaded into the Power Query Editor (Data > From Table/Range in Excel, or Get Data in Power BI).
- A clear idea of which column defines a group (for example Customer ID, Order Number, or Date).
- No blank rows in the key column — blanks form their own group and usually surprise people later.
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 > Get Data and pick your source. Confirm the first row is promoted to headers and each column has the correct data type.
- Sort deliberately before grouping. Grouping keeps the first row it encounters per group, so the sort order decides which row's values survive. Click the column you want to prioritise (for example a date column) and sort ascending or descending. If you skip this, the "kept" row is effectively arbitrary.
- Select the grouping column. Click the header of the column that defines each group. Hold Ctrl to select more than one column if the group is defined by a combination (for example Customer ID plus Month).
- Open Group By. On the Home tab, click Group By. The dialog opens with the selected column already listed under grouping. Labels vary slightly by version and host, but the dialog layout is consistent.
- Add an All Rows aggregation. Click New column name, type a helper name such as AllData, set Operation to All Rows, and click OK. Power Query now returns one row per group, with a Table value in the AllData column containing every original row for that group — all columns intact.
- Expand the nested table. Click the expand icon (two arrows) in the AllData column header. Untick "Use original column name as prefix" if you do not want prefixed names, then tick the columns you want to keep. If you tick every column, you get the full original rows back, one row per original record.
- Remove duplicates if you want one row per group. With the key column selected, go to Home > Remove Rows > Remove Duplicates. Because you sorted earlier, the surviving row per group is the one you chose in step 2. This is the step that actually delivers "grouped, one row per group, all columns kept."
- Clean up. Delete any helper columns you no longer need, rename the remaining columns, and set data types again — expanding a table often resets types to Any. Then Close & Load (or Close & Apply in Power BI).
A worked example: a sales table with Order ID, Customer, Date, Product and Amount. You want one row per customer showing their most recent order with all fields. Sort Date descending, group by Customer with an All Rows column, expand all columns, then remove duplicates on Customer. The result is one full row per customer, taken from their latest order.
Common Mistakes
- Expecting Group By to keep columns automatically. The standard aggregations (Sum, Count, Max) only return the grouped column plus the aggregate. All Rows is the only built-in way to retain the rest.
- Forgetting to sort first. Without a deliberate sort, Remove Duplicates keeps whichever row happens to come first, which can change between refreshes if the source order changes.
- Expanding only some columns and then wondering where the data went. Expansion is a choice; unticked columns are dropped from the output.
- Leaving the AllData column in place. A column of nested tables is not useful downstream and can bloat the model. Remove it after expanding.
- Ignoring data types after expansion. Expanded columns often come back as Any; set them explicitly or later steps and relationships may fail.
- Grouping on a column with trailing spaces or inconsistent casing. "ACME" and "Acme " become separate groups. Trim and clean the key column first.
Tips
- If you only need one extra column per group (for example the latest date), a cheaper alternative is Group By with Operation set to Max on that column — no expansion needed. Use All Rows when you genuinely need several columns.
- To keep the full row for a specific record per group, sort so the desired record is first, then use the All Rows plus Remove Duplicates pattern.
- Rename the AllData column to something meaningful before expanding; the expand dialog uses that name for prefixes.
- Check the step names in Applied Steps. Renaming the Grouped Rows and Expanded steps makes the query readable months later.
- If refresh speed matters, filter rows before grouping rather than after — grouping a smaller table is faster.
Troubleshooting
Error: "Expression.Error: The column 'X' of the table wasn't found." A later step refers to a column that was dropped during expansion. Re-expand that column or fix the step reference.
Duplicate rows still appear after Remove Duplicates. The key column probably has invisible differences — trailing spaces, mixed case, or numbers stored as text. Use Transform > Format > Trim and Clean, and set a consistent type before grouping.
The wrong row survives per group. The sort step is missing or was placed after grouping. Move the sort before Group By. Note that sorting before grouping is what makes the result deterministic.
Expansion produces far more rows than expected. That is normal: expanding All Rows returns every original row. Remove Duplicates on the key column is the step that reduces it back to one row per group.
Query is slow. Grouping with All Rows on a very large table is memory-heavy. Filter early, remove unneeded columns before grouping, and consider whether a Max or Min aggregation on one column would meet the need.
If that did not work, rebuild the query from the source with only the essential steps, confirming after each step that the row count and columns are what you expect. Most failures trace back to a column renamed or removed upstream.
Frequently Asked Questions
Can Group By keep all columns directly?
Not with the standard aggregations. The supported route is an All Rows aggregation, then expand, then remove duplicates on the key column.
Does All Rows lose any data?
No. It nests every original row for each group into a Table value. Data is only lost if you choose not to expand certain columns.
Why does my grouped table have more rows than groups?
Because you expanded all columns and have not yet removed duplicates on the grouping column. Expansion restores the original row count.
Can I group by more than one column?
Yes. Select multiple columns with Ctrl before clicking Group By, or add them in the dialog. The All Rows pattern works the same way.
Does this work in Excel as well as Power BI?
Yes. Both use the same Power Query engine and the same Group By dialog, though the ribbon location and some labels differ slightly by version.
Is there a DAX equivalent?
In Power BI you can achieve similar results with DAX measures or calculated tables, but for shaping data at load time the Power Query approach is simpler and keeps the model smaller.