Power Query Transformations: Unpivoting, Merging & Appending
Overview & Learning Objectives
ERP reports often export data in "wide" cross-tab formats (with months as columns) that Pivot Tables cannot analyze. Power Query transforms wide data into normalized "tall" tables with Unpivot, joins tables with Merge, and stacks files with Append.
- Unpivot multi-column month headers into clean Attribute and Value pairs.
- Perform relational joins between Fact Sales and Dim Customer using Merge Queries.
- Stack multiple monthly worksheets vertically using Append Queries.
Industry Standard
Standard: Data warehouse modeling requires raw cross-tab spreadsheets to be unpivoted into normalized 3NF relational schemas before feeding BI semantic layers.
Real-World Example: Regional Sales Matrix Unpivoting & Relational Joins
At QuickMart Retail, the finance department provides a 12-column budget matrix (Jan to Dec across the page). The analyst unpivots it into 2 columns ("Month" and "Budget Amount") in 2 clicks, allowing instant year-over-year slicing.
| Region | Month (Unpivoted Attribute) | Budget (Unpivoted Value) | Transformation Applied |
|---|---|---|---|
| North | January | 1,50,000 | Unpivot Other Columns |
| North | February | 1,75,000 | Unpivot Other Columns |
| South | January | 2,10,000 | Unpivot Other Columns |
Functions Covered in this Chapter
Here is the definition, exact syntax, parameter breakdown, and real-world business example for each function covered in this chapter:
Unpivot Columns Transformation
Definition: Converts repeated column headers (such as Jan, Feb, Mar, Apr...) into a single vertical "Attribute" column and their corresponding numbers into a "Value" column.
Select key columns -> Transform Tab -> Unpivot Columns -> Unpivot Other Columns
Unpivot Other Columns: Selects the fixed identifier columns (e.g. Region, Store) and flattens all other metric columns dynamically.Attribute: The new column holding the former column headers (Month names).Value: The new column holding the metrics.
Right-click Region column -> Click "Unpivot Other Columns"Merge Queries (Relational Join)
Definition: Joins two tables side-by-side based on a shared matching key column (equivalent to a SQL LEFT OUTER JOIN or VLOOKUP).
Home Tab -> Merge Queries -> Select Table 1 & Table 2 -> Highlight Key Column -> Select Join Kind
Left Table: Primary transactional table (Fact_Sales).Right Table: Dimension lookup table (Dim_Customer).Join Kind: Left Outer (all from first, matches from second), Inner (only matching rows), Full Outer.
Merge Fact_Sales on CustomerID with Dim_Customer -> Expand City & EmailAppend Queries (Vertical Stacking)
Definition: Stacks two or more tables with identical column structures vertically on top of each other (equivalent to SQL UNION ALL).
Home Tab -> Append Queries -> Select Two Tables or Three or More Tables -> Click OK
Tables to Append: The tables to combine (e.g. Q1_Sales, Q2_Sales, Q3_Sales).Column Header Alignment: Power Query automatically aligns matching column names even if their order varies.
Append Q1_Sales and Q2_Sales into Combined_SalesCommon Mistakes & Limitations
- Clicking "Unpivot Columns" directly instead of "Unpivot Other Columns". "Unpivot Other Columns" ensures that when next month's column (e.g. "April") is added, it unpivots automatically!
- Case sensitivity in Merge: Power Query treats "DELHI" and "Delhi" as different keys. Always apply Text -> Format -> Capitalize Each Word before merging.
Keyboard Shortcuts
| Action | Windows | Mac |
|---|---|---|
| Open Applied Steps Settings | Double-click Gear Icon next to Step | Double-click step |
| View Advanced M Code Script | Home -> Advanced Editor | Home -> Advanced Editor |
Pro Tips & Modern Alternatives
- In modern Excel 365, `=VSTACK(Table1, Table2)` provides an in-grid alternative to Append Queries.
- Use `=HSTACK()` for in-grid horizontal table binding.