Advanced Excel for Data AnalystsLesson 44 of 49
Lesson 44 of 49

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.

RegionMonth (Unpivoted Attribute)Budget (Unpivoted Value)Transformation Applied
NorthJanuary1,50,000Unpivot Other Columns
NorthFebruary1,75,000Unpivot Other Columns
SouthJanuary2,10,000Unpivot 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:

Function 1

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.

Syntax:
Select key columns -> Transform Tab -> Unpivot Columns -> Unpivot Other Columns
Parameters Breakdown:
  • 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.
Real-World Example (Flatten 12 Monthly Columns):
Problem / Scenario: How do you turn a 12-month cross-tab sheet into a format ready for Pivot Tables?
Formula: Right-click Region column -> Click "Unpivot Other Columns"
Result: Clean 3-column table (Region, Month, Sales)
Why it works: Power Query converts wide matrix into normalized tall format.
Function 2

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).

Syntax:
Home Tab -> Merge Queries -> Select Table 1 & Table 2 -> Highlight Key Column -> Select Join Kind
Parameters Breakdown:
  • 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.
Real-World Example (Customer Details Lookup):
Problem / Scenario: Add Customer City and Email to 50,000 sales transactions using CustomerID.
Formula: Merge Fact_Sales on CustomerID with Dim_Customer -> Expand City & Email
Result: Relational join populated without a single VLOOKUP formula
Why it works: Executes in the background at database speed without formula lag.
Function 3

Append Queries (Vertical Stacking)

Definition: Stacks two or more tables with identical column structures vertically on top of each other (equivalent to SQL UNION ALL).

Syntax:
Home Tab -> Append Queries -> Select Two Tables or Three or More Tables -> Click OK
Parameters Breakdown:
  • 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.
Real-World Example (Combine Quarterly Reports):
Problem / Scenario: Stack Q1 and Q2 sales sheets into a single Master Annual table.
Formula: Append Q1_Sales and Q2_Sales into Combined_Sales
Result: Unified dataset containing all rows from both periods
Why it works: Eliminates tedious manual copy-pasting across tabs.

Common 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

ActionWindowsMac
Open Applied Steps SettingsDouble-click Gear Icon next to StepDouble-click step
View Advanced M Code ScriptHome -> Advanced EditorHome -> 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.