Sub topic 10
Expanding or aggregating the merged column
A merge in Power Query adds a single column that holds a nested table in every row. This chapter covers how to turn that column into usable data. You can expand it into ordinary columns, or you can aggregate it into one value per row. The choice decides whether your row count stays the same or grows.
This chapter assumes you can already create a merge, you know the join kinds, and you have prepared your key columns. If not, review the earlier chapters on merging two queries, join kinds, and key columns first.
What the merged column contains
After a merge, the new column shows the word Table in each cell. Each cell holds the rows of the second query that matched the key in that row of the first query. The number of rows in each nested table depends on the data:
- A nested table with no rows means no match was found.
- A nested table with one row means exactly one match.
- A nested table with several rows means the key repeats in the second query.
The last case matters most. Expanding a nested table with several rows creates one output row per nested row, so your first query's rows get duplicated.
Expanding the column
To expand, select the arrow icon in the header of the merged column. A panel opens with these elements:
- A list of checkboxes, one per column in the second query.
- A choice between Expand and Aggregate.
- A checkbox named Use original column name as prefix.
- A search box for long column lists.
With Expand selected, tick the columns you want and select OK. The nested column is replaced by the chosen columns.
The M code for this step looks like the following. It matches the earlier merge example and keeps every order, adding the customer name and region.
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 in this form: the table, the nested column name, the list of columns to pull out, and the list of names to give them in the result. The last two lists must have the same length. The second list is where you rename columns to avoid collisions.
Name collisions and prefixes
If the first query already has a column called Name and you expand a Name column from the second query, Power Query cannot keep both names. When Use original column name as prefix is ticked, the new columns become Customers.Name and Customers.Region. If you untick it, the expand step fails or gives a renamed column, depending on whether the name clashes. Check the result either way.
The safest habit is to set the new names yourself in the fourth argument of Table.ExpandTableColumn, for example {"Customer Name", "Customer Region"}.
Unmatched rows after expanding
With a left outer join, a row with an empty nested table is kept. After expanding, the new columns are null for that row. This is how unmatched rows appear, and it is useful: filtering the expanded column for null lists the rows that found no partner.
With an inner join, unmatched rows are already gone before you expand, so no nulls appear from missing matches.
Expanding and row multiplication
Consider these two tables.
| Query | Rows | Key column |
|---|---|---|
| Customers | 100 | CustomerID, unique |
| Orders | 450 | CustomerID, repeats |
If you merge Customers (first) with Orders (second) using a left outer join and expand the Orders columns, the result has one row per order for customers with orders. Customers without orders keep one row each, with null order columns. The total is at least 450 rows, not 100.
This is correct behavior for a join, but it surprises people who expected one row per customer. The row count grows because the relationship is one-to-many. Reversing the order of the merge, with Orders first and Customers second, keeps 450 rows, because each order matches one customer.
Aggregating the column instead
When you only need a summary, such as a total, a count, or a maximum, choose Aggregate in the expand panel. Power Query shows each column of the nested table with a list of functions. Typical choices are Sum, Average, Minimum, Maximum, Count, and Count Distinct. The available functions depend on the column's data type. A text column offers fewer options than a number column.
Aggregation keeps one row per row of the first query, so the row count does not change.
let
Source = Table.NestedJoin(Customers, {"CustomerID"}, Orders, {"CustomerID"}, "Orders", JoinKind.LeftOuter),
Totals = Table.AggregateTableColumn(Source, "Orders", {{"Amount", List.Sum, "Total Amount"}})
in
TotalsTable.AggregateTableColumn takes the table, the nested column name, and a list of triples. Each triple has the source column in the nested table, the function to apply to the list of values, and the name of the new column. You can supply several triples to build several summaries in one step, for example one for List.Sum and another for List.Max.
Aggregating customers with no orders
For a customer whose nested table is empty, List.Sum returns null in this setup, not 0. Reports that show blanks may need a follow-up step to replace nulls. A Table.ReplaceValue step, or the Replace Values command applied to the new column, converts nulls to zero. Count behaves differently: counting an empty nested table gives 0.
Expand or aggregate: a decision guide
| Situation | Better choice | Why |
|---|---|---|
| You need the order detail rows in the result | Expand | Each matched row becomes its own row |
| You need one row per customer with a total | Aggregate | Row count stays the same |
| The second query has unique keys | Either | No multiplication occurs |
| You need a count of matches only | Aggregate with Count | Avoids loading detail you will discard |
| You need the first or latest match only | Sort and remove duplicates before the merge | Aggregation functions do not pick a whole row |
Handling the nested column yourself
You do not have to use the panel. Because the nested column is an ordinary table column, you can transform it with Table.TransformColumns or add a custom column that works on it. For example, a custom column with the formula Table.RowCount([Orders]) gives the number of matches per row without expanding anything. This is a handy check, covered further in the chapter on verifying a merge.
Common errors
- Forgetting to rename. Expanded columns that clash with existing names get a prefix or an error. Set the new names explicitly.
- Expanding after a data type change upstream. If you rename or remove a column in the second query, an old expand step can fail with a "column not found" message. Open the step and update the column list.
- Expanding all columns by default. The panel lets you pick Select All Columns. This loads columns you do not need and makes later refreshes slower. Choose only the columns you will use.
- Trusting the preview. The editor previews only the top rows of data. A one-to-many expansion may not show in those rows, so look at the row count in the status bar or load the result and count it.
- Aggregating a text column with Sum. Sum is not offered for text. Convert the type first.
- Expecting Count to ignore nulls. The plain Count function counts rows in the nested table. Use Count Distinct or filter first if you need something else.
Putting it together
A common sequence is to merge, check the nested table row counts, and then decide how to expand. The steps are:
- Merge the two queries with the join kind you need.
- Add a temporary custom column with
Table.RowCount([NestedColumn])and check for values above 1. - If values above 1 exist and you want one row per key, aggregate. If you want the detail, expand and accept more rows.
- Remove the temporary column.
- Confirm the final row count matches your expectation.
Exercise
Customers has 100 unique CustomerID values. Orders has 450 rows, and 80 of the customers have at least one order. You merge Customers with Orders using a left outer join. How many rows does the result have after you expand the Orders columns, and how many after you aggregate Orders with a Sum of Amount?
Show answer
After expanding, the result has 450 rows for the 80 customers who have orders, plus 20 rows for the customers with no orders (with null order columns), so 470 rows in total. After aggregating, the result has 100 rows, one per customer. The 20 customers without orders have null in the Total Amount column, because the sum of an empty nested table is null in this setup.
Exercise
You expanded a nested column and the Name column in your first query now shows the values from the second query's Name column. What caused this, and what should you do?
Show answer
This does not match how Power Query handles clashes. When both queries have a Name column, the expand step either adds a prefixed column such as Customers.Name or raises a name conflict, rather than overwriting the first column. Check the step and the column list: if a value looks overwritten, the step may have renamed columns deliberately. Fix it by giving the expanded columns explicit new names in the fourth argument of Table.ExpandTableColumn, for example {"Customer Name"}.