Stop 04 · Power Query TransformationsTopics · 7/11

Unpivot and Pivot Columns

Part of the Power BI Roadmap

In short

Budget sheets love one column per month. Power BI hates it. Unpivot is the one-click transformation that turns a wide Excel layout into the long, tidy shape every chart and measure expects.

Unpivot turns wide data with one column per month into long data with a Month column and a Value column, which is the shape Power BI works best with. Pivot does the reverse.

The jaadu lifts jars one by one off a long wide shelf and lines them up in a neat single-file row on the floor, each with a small hanging tag.

Think of it like this

Unloading a shelf into a queue

A shop shelf holds twelve jars side by side, one per month. To weigh them, you take each jar down and line them up in a single queue, each with a tag saying which month it is. Unpivot lines the jars up. Pivot puts them back on the shelf.

Watch it happen

Wide vs long

Wide (Excel style)

  • Product | Jan | Feb | Mar ...
  • A new month means a new column
  • Measures need one formula per month
  • Cannot slice by month easily

Long (Power BI style)

  • Product | Month | Sales
  • A new month means new rows
  • One measure: SUM of Sales
  • Month works on axes and slicers

Unpivot wide month columns into Month and Sales. Pivot is for the rare case where you really need one column per value, like separate Target and Actual columns.

The big ideas

What you need to know

Unpivot Other Columns

Select the columns that should stay (like Product) and choose Unpivot Other Columns. New months are handled automatically.

Avoid Unpivot Columns on months

Unpivoting the selected month columns hard-codes their names, so next year's columns are ignored.

Turn labels into dates

After unpivoting, convert 'Jan-26' style text into a real date for time intelligence.

Pivot is the reverse

Pivot Column spreads a column's values into new columns and aggregates the values underneath.

See it in code

Walk through the code

m
1let2    Source = Budget,3    Long = Table.UnpivotOtherColumns(Source, {"Product"}, "Month", "Sales"),4    Typed = Table.TransformColumnTypes(5        Long,6        {{"Month", type date}, {"Sales", Currency.Type}},7        "en-IN"8    ),9 10    // Reverse, just to see it: spread Month back into columns11    BackToWide = Table.Pivot(12        Long,13        List.Distinct(Long[Month]),14        "Month",15        "Sales",16        List.Sum17    )18in19    Typed

1/3Keep Product fixed. Every other column becomes a Month name with its Sales value.

Unpivot every column except Product into Month and Sales. BackToWide shows Pivot undoing it; the query still returns the long Typed table.

Save yourself the pain

Common mistakes

Oops

Selecting Jan to Dec and clicking Unpivot Columns.

Do this instead

Select Product and click Unpivot Other Columns, so next year's new month columns are included automatically.

Oops

Keeping a Total column in the wide table before unpivoting.

Do this instead

Remove Total first, or it becomes a fake month and doubles every sum.

Your mission

Flip a budget sheet

~20 min

Quick check

Next year a new column Jan-27 is added to the budget file. Which choice handles it with no edits?

Go deeper

  1. 1Unpivot a budget sheet that has one column per month.
  2. 2Use 'Unpivot Other Columns' so new months are handled automatically.
  3. 3Pivot a small attribute table and see when the wide shape is actually useful.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge