Stop 04 · Power Query TransformationsTopics · 8/11

Group By in Power Query

Part of the Power BI Roadmap

In short

Sometimes you don't need every single transaction in the model, only daily or monthly totals. Group By in Power Query summarises rows before loading, which can make big models much smaller and faster.

Group By summarises rows into one row per group with sums, counts or other aggregations before the data reaches the model, which can shrink large tables.

The jaadu sits behind three cash drawers, stacking coins and notes into tidy piles, one pile per drawer.

Think of it like this

Counting the day's cash by counter

At closing time a supermarket manager doesn't log every coin. She counts each counter's drawer and writes one line per counter: total cash, number of bills. Group By writes those summary lines for you.

Watch it happen

Grouping 2 lakh order lines

Order lines
Group by City, Date
Sum and count
Daily summary

1/42,00,000 order lines with City, OrderDate and Amount.

The big ideas

What you need to know

Basic and Advanced

Basic groups by one column with one aggregation. Advanced allows several group columns and several aggregations.

Many aggregations

Sum, Average, Median, Min, Max, Count Rows, Count Distinct Rows and All Rows.

Detail is gone

After grouping you can't drill to individual orders. Only group when the report truly never needs detail.

It can fold

On a database source, Group By folds into a SQL GROUP BY, so the server does the work.

See it in code

Walk through the code

m
1let2    Source = Sql.Database("sql-prod-01", "ShopDB"),3    Lines = Source{[Schema = "rpt", Item = "vw_SalesLines"]}[Data],4    Daily = Table.Group(5        Lines,6        {"City", "OrderDate"},7        {8            {"Total Sales", each List.Sum([LineAmount]), type number},9            {"Orders", each List.Count(List.Distinct([OrderID])), Int64.Type},10            {"Lines", each Table.RowCount(_), Int64.Type}11        }12    )13in14    Daily

1/4Group by two columns: City and OrderDate.

Summarise order lines into one row per city per day with total sales, distinct orders and line count.

Save yourself the pain

Common mistakes

Oops

Grouping in Power Query and then trying to show a list of individual orders.

Do this instead

Keep the detail table if anyone needs to drill down. Group only for tables meant purely for summary.

Oops

Using Count Rows to count orders on an order-lines table.

Do this instead

Count distinct OrderID values. One order with three lines would otherwise count as three.

Your mission

Shrink a big table

~20 min

Quick check

In Table.Group, what does the underscore _ refer to inside each aggregation?

Go deeper

  1. 1Group daily sales into one row per store per month.
  2. 2Use the Advanced option to add several aggregations at once.
  3. 3Ask whether you lose detail that a report might need later.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge