Sub topic 10
Expanding or aggregating the merged column
This chapter assumes you can run a merge in the Power Query Editor, you know the join kinds, and you can read basic M. A merge does not add the matched columns to your table. It adds one column whose cells each hold a nested table. You then decide how to turn that column into ordinary values.
What the merged column contains
After a merge, the new column shows the word Table in every cell. Each nested table holds the rows from the second query that matched that row's key. With a left outer join, a row with no match gets an empty table.
The header of the column has an icon with two arrows. Clicking it opens a choice:
- Expand adds the columns you tick as new columns in the main table.
- Aggregate collapses each nested table into one value per row, such as a sum or a count.
Expanding the column
Expanding is the right choice when each row in the first table matches at most one row in the second, such as orders looking up a customer. This is the M code for it:
let
Source = Table.NestedJoin(Orders, {"CustomerID"}, Customers, {"CustomerID"}, "Customers", JoinKind.LeftOuter),
Expanded = Table.ExpandTableColumn(Source, "Customers", {"Name", "Region"}, {"Name", "Region"})
in
ExpandedTable.ExpandTableColumn takes four arguments: the table, the nested column, the columns to pull out, and the names for the new columns. Rows with no match get null in Name and Region.
Two options in the expand dialog are worth knowing:
- Use original column name as prefix is on by default. It produces names such as
Customers.Name, which avoids a clash when both tables have a column calledName. - You can untick columns you do not need. Expanding only what you use keeps the table smaller.
Expanding can multiply rows
If one customer has five orders and you merge Customers with Orders, the nested table for that customer holds five rows. Expanding turns that single customer row into five rows. The customer's own columns repeat on each of them.
This is correct when you want one row per order. It is a problem when you wanted one row per customer, because totals taken from the customer table are now counted several times.
Aggregating the column
Use aggregate when you want a summary per original row. The row count stays the same.
let
Source = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Orders", JoinKind.LeftOuter),
Totals = Table.AggregateTableColumn(Source, "Orders", {{"Amount", List.Sum, "Total Amount"}})
in
TotalsEach entry in the list has three parts: the column to summarize, the function, and the new column name. The dialog offers Sum, Average, Min, Max, Count, Count (distinct), and a few statistical choices. A customer with no orders gets null for the sum, not 0. If you need a zero, add a step that replaces null with 0.
Choosing between them
| Situation | Choice | Rows in result |
|---|---|---|
| Look up one attribute per row, such as region | Expand | Same as the first table, if keys are unique in the second |
| Need every detail row, such as each order line | Expand | One per matching row |
| Need one figure per row, such as total spent | Aggregate | Same as the first table |
Exercise
You merge Customers (200 rows) with Orders using a left outer join, then expand the Orders column. The result has 1,350 rows. You need one row per customer with their total order amount. What should you do instead of expanding?
Show answer
Use aggregate on the nested Orders column (Table.AggregateTableColumn with List.Sum on Amount). Each customer has several orders, so expanding repeats the customer row once per order. Aggregating collapses the nested table to one value per customer, so the result stays at 200 rows.