3.2 Grouping, Aggregating, Pivoting, and Unpivoting
Key Takeaways
- Group By aggregates rows based on one or more columns, applying functions like Sum, Count, Average, Min, Max.
- Pivot Column transforms unique row values into column headers — turning long format into wide format.
- Unpivot Columns converts column headers back into row values — turning wide format into long format.
- Transpose swaps rows and columns entirely, useful for data that is laid out horizontally.
- Unpivot is particularly important for normalizing Excel reports with months or categories as column headers.
Grouping, Aggregating, Pivoting, and Unpivoting
Quick Answer: Group By summarizes data (like SQL GROUP BY). Pivot turns row values into column headers (long → wide). Unpivot turns column headers into row values (wide → long). Transpose flips the entire table. Unpivot is the most commonly tested because it normalizes denormalized Excel data into a format suitable for data modeling.
Group By
Group By aggregates multiple rows into summary rows based on grouping columns.
Transform tab → Group By
Basic Group By
Group by one column with one aggregation:
| Before: Sales Transactions | |
|---|---|
| Region | Amount |
| East | 100 |
| East | 200 |
| West | 150 |
| West | 250 |
Group By Region, Sum of Amount:
| After: Grouped | |
|---|---|
| Region | Total Amount |
| East | 300 |
| West | 400 |
Advanced Group By
Group by multiple columns with multiple aggregations:
Group By → Advanced →
Group By: Region, Product
Aggregations:
- Total Sales: Sum of Amount
- Order Count: Count Rows
- Average Order: Average of Amount
Available aggregation functions:
- Sum, Average, Median
- Min, Max
- Count Rows, Count Distinct Values
- All Rows (returns a table — useful for advanced scenarios)
All Rows Aggregation
The All Rows option creates a nested table for each group. This is powerful for:
- Getting the first or last row of each group
- Applying custom calculations within groups
- Accessing detail rows after aggregation
Pivot Column
Pivot transforms row values into column headers, converting data from long (normalized) to wide (denormalized) format.
Select the column to pivot → Transform tab → Pivot Column
→ Choose the values column → Select aggregation
Example: Pivoting
Before (Long Format):
| Product | Month | Sales |
|---|---|---|
| Widget | Jan | 100 |
| Widget | Feb | 150 |
| Gadget | Jan | 200 |
| Gadget | Feb | 250 |
After Pivoting Month column (Values: Sales):
| Product | Jan | Feb |
|---|---|---|
| Widget | 100 | 150 |
| Gadget | 200 | 250 |
Aggregation options when pivoting:
- Sum (default for duplicates)
- Average, Count, Min, Max
- Don't Aggregate (errors if duplicates exist)
Unpivot Columns
Unpivot is the reverse of Pivot — it converts column headers into row values, transforming from wide to long (normalized) format.
Select columns to unpivot → Transform tab → Unpivot Columns
Three Unpivot Options
| Option | Behavior |
|---|---|
| Unpivot Columns | Unpivots the selected columns |
| Unpivot Other Columns | Keeps selected columns and unpivots everything else |
| Unpivot Only Selected Columns | Same as Unpivot Columns but generates different M code |
Example: Unpivoting
Before (Wide Format — common in Excel reports):
| Product | Jan Sales | Feb Sales | Mar Sales |
|---|---|---|---|
| Widget | 100 | 150 | 200 |
| Gadget | 200 | 250 | 300 |
After Unpivoting the month columns:
| Product | Attribute | Value |
|---|---|---|
| Widget | Jan Sales | 100 |
| Widget | Feb Sales | 150 |
| Widget | Mar Sales | 200 |
| Gadget | Jan Sales | 200 |
| Gadget | Feb Sales | 250 |
| Gadget | Mar Sales | 300 |
Best Practice: Use "Unpivot Other Columns" instead of "Unpivot Columns" when the source may add new columns over time (e.g., new months). This way, new columns are automatically unpivoted.
Why Unpivot Matters
Unpivoting is critical for data modeling because:
- Power BI models work best with normalized (long) data
- Star schema fact tables require one row per measurement
- DAX functions work most effectively with long-format data
- Filters and slicers apply naturally to normalized structures
Transpose
Transpose swaps the entire table's rows and columns:
Transform tab → Transpose
- Row 1 becomes Column 1, Row 2 becomes Column 2, etc.
- Column headers become the first row of data
- The first row of data becomes the new column headers
Common use case: Data sources where data is laid out horizontally (metrics as rows, time periods as columns) that need to be flipped to a standard vertical layout.
Tip: After transposing, use "Use First Row as Headers" to promote the first data row to column headers.
Semi-Structured Data Conversion
Converting semi-structured data (JSON, XML) into tables:
JSON to Table
// Parse JSON
Json.Document(Source)
// Convert record to table
Record.ToTable(jsonContent)
// Expand nested records/lists
Table.ExpandRecordColumn(previousStep, "Value", {"field1", "field2"})
XML to Table
// Parse XML
Xml.Document(Source)
// Navigate to the data nodes
// Expand nested tables
Expanding Records and Lists
| Column Display | Type | Action |
|---|---|---|
| Record | Single nested object | Expand columns (select fields) |
| List | Array of values | Expand to new rows |
| Table | Nested table | Expand columns or rows |
Click the expand icon (two arrows) in the column header to expand records or lists.
On the Exam
The PL-300 frequently tests:
- Choosing between Pivot and Unpivot for a given data transformation
- Using Unpivot Other Columns for dynamic column handling
- Group By with multiple columns and aggregations
- Converting JSON semi-structured data into tabular format
- Understanding when to use Transpose vs. Unpivot
An Excel report has columns: Product, Q1 Sales, Q2 Sales, Q3 Sales, Q4 Sales. You need to create a normalized table with columns: Product, Quarter, Sales. Which operation should you use?
You have a table with Region and Sales columns containing 10,000 rows across 5 regions. You want a summary showing total sales per region. Which Power Query operation should you use?
Why should you use "Unpivot Other Columns" instead of "Unpivot Columns" when the source might add new columns in the future?