Group By: summarize rows

Grouping combines many rows into one per group, with a sum, count or average. In Power Query this already happens while loading. Here you learn how it works, and why Power BI needs it less often than Excel.

In short: select the group column, Group By, then choose a new column name, the operation (Sum, Count Rows …) and the column. The result has one row per group; the individual rows are gone afterwards.

Prerequisites: Unpivot.

Try it now

Task An overview only needs sales per region. Group by Region and sum Amount into a new column Sales.

ABC
1 Region Product Amount
2 North Laptop 899
3 South Mouse 19.9
4 North Monitor 229
5 West Laptop 1798
6 South Laptop 899
7 North Keyboard 39.9
8 West Mouse 59.7

Group in Power Query or sum in the report?

In Power BI the matrix and measures sum per group anyway, recalculated for every slicer selection. If you group in Power Query already, the individual rows are no longer in the model: filtering by product or drilling into a month is then impossible. Grouping is therefore mainly worth it in three cases:

  • The raw data is very large and the report only needs totals (for example millions of till rows per day and store).
  • A second, small query for an export or a helper table.
  • Intermediate steps such as "sales per customer", to classify customers with a conditional column afterwards.

For everything else, a measure as in the lesson Your first measure is the more flexible route.

One more exercise: count rows

Task How many orders does each category have? Group by Category and count the rows into a new column Count.

ABC
1 OrderID Category Status
2 202 Hardware Delivered
3 203 Accessories Delivered
4 204 Hardware Open
5 205 Software Delivered
6 206 Accessories Delivered
7 207 Hardware Delivered
8 208 Accessories Open

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

In real Power BI

  1. Group By: Home or Transform, Group By.
  2. Basic: one group column, one aggregation. Choose the new column name, operation and column.
  3. Advanced: several group columns and, with "Add aggregation", several results, such as sum and count at once.

Common mistakes

Sum over text

If the column is still text, Sum returns an error. Change the data type first, then group.

Grouped too early

After grouping, every column that is neither a group nor a result is gone. If you later want to filter by product after all, you have to delete the step again.

Frequently asked questions

What is the difference between Count Rows and Count Distinct Rows?

Count Rows counts every row of the group. Count Distinct Rows only counts different rows; duplicates count once.

Is this the same as a PivotTable?

Yes, with one group in the rows and one value. The result just stays a table that Power Query keeps working with.

Learn this next: conditional column

Classify rows by a rule, the IF of Power Query.

Conditional column

Write to us

Choose language

Select your preferred language