Sub topic 13
Summarizing data with Group By
Group By collapses many rows into one row per group, with an aggregate such as a sum or count. This chapter assumes you can load a table into the Power Query Editor, apply data types, and read the Applied Steps pane. If you can do those, you have the prerequisites.
What Group By does
Suppose a table has one row per sale. A report needs one row per region with the total amount. Group By does this inside the query, so the summary refreshes along with the source data.
The step is a transformation like any other. It appears in Applied Steps and can be edited or deleted.
Group by one column
Select the Region column, then choose Transform > Group By. In the dialog, pick Region as the grouping column. Name the new column TotalAmount, choose the operation Sum, and choose Amount as the column to sum.
The query below shows the M code that Power Query generates for this.
let
Source = Excel.CurrentWorkbook(){[Name="SalesClean"]}[Content],
Grouped = Table.Group(Source, {"Region"}, {{"TotalAmount", each List.Sum([Amount]), type number}})
in
GroupedTable.Group takes three arguments. The first is the table. The second is the list of columns that define a group. The third is a list of aggregations, each with a new column name, a function, and a type.
Rows with the same Region value fall into one group. Text values are compared exactly, so "East" and "East " (with a trailing space) become two groups. Trim and clean text before you group.
Several aggregations at once
In the dialog, select Advanced. You can add more than one aggregation and more than one grouping column. For example, group by Region and Product, then add a Sum of Amount and a Count Rows.
| Operation | Result |
|---|---|
| Sum | Total of a numeric column |
| Average | Mean of a numeric column |
| Min, Max | Smallest or largest value |
| Count Rows | Number of rows in each group |
| Count Distinct Rows | Number of unique rows in each group |
| All Rows | A nested table holding each group's rows |
Count Rows needs no source column, because it counts rows. The other numeric operations need a column with a number type. If Amount is still text, Sum returns an error, so set data types first.
How the data flows
Common errors
- Wrong totals from duplicate groups. Stray spaces or different capitalization split one group into several. Fix the text before grouping.
- Errors in the aggregate column. A value in the source column could not be converted to a number. Fix the type or the offending rows first.
- Missing columns after the step. This is expected, as the warning above explains.
- Blank groups. Null values form their own group. Filter them out or fill them before you group.
Exercise
A table has columns Region, Product, and Amount. You want the number of orders and the average Amount for each Region. Which Group By settings do you use?
Show answer
Group by Region. Open Advanced and add two aggregations: Count Rows (for example named Orders) and Average on Amount (for example named AvgAmount). Product is not a grouping column, so it is dropped from the result.