Power Query: More Fun with (Un)Pivot
18 December 2019
Welcome to our Power Query blog. This week, I look at another use for unpivoting.
John, my imaginary salesperson, has been creative with his accounts again.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image1.png/e774d10cbbb9450fc45efbe51abdf434.jpg)
In this case, I’d like to see a column for months, a column for amounts, a column for expense category and for tips – that way, it’s much easier to add future data, as additional categories can be readily added.
I start by extracting the data into Power Query using the ‘From Table’ option on the ‘Get & Transform’ section of the ‘Data’ tab.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image2.png/f32e5a15e2cf9c3e4d2d058458ce054d.jpg)
As with last week’s blog, I need to remove the ‘Changed Type’ step because it refers to specific month columns:
= Table.TransformColumnTypes(Source,{{"Expense Type", type text}, {"August Amount", Int64.Type}, {"August Tips", Int64.Type}, {"September Amount", Int64.Type}, {"September Tips", Int64.Type}, {"October Amount", Int64.Type}, {"October Tips", Int64.Type}, {"November Amount", Int64.Type}, {"November Tips", Int64.Type}})
I delete this step.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image3.png/f1140ff857fc3b6f5f97a6a24f4a6fc7.jpg)
I need to avoiding referencing month names in the columns; to achieve this, I select the Expense Type column, and opt to unpivot the other columns.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image4.png/72aa864d2854c6fefb1083fba0ab5792.jpg)
The M code generated is:
= Table.UnpivotOtherColumns(Source, {"Expense Type"}, "Attribute", "Value")
This is good because there is no mention of the month columns. The months, amounts and tip titles now appear as data under the Attribute column. I need to perform further transformations to sort out this column. I begin by splitting the Attribute column into the separate titles.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image5.png/36776d1da4d05b45bb5a5d09375f407c.jpg)
I opt to use ‘Split Columns’ on the ‘Transform’ tab, and choose ‘By Delimiter’.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image6.png/23912d3b1671861e02bebcd5183f1607.jpg)
I choose to split at ‘Space’.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image7.png/6f49c288a0d88a66b427eaf4ece923d6.jpg)
I can rename Attribute.1 to Month. Attibute.2 contains my two other column headings, so I need to pivot this column by selecting it and using the ‘Pivot’ option on the ‘Transform’ menu.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image8.png/b9ee28d90e6b5bc92ea4aeafdad51628.jpg)
I need to choose what to put in my new columns; they should contain the associated Value, since that contains the amount.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image9.png/0485ccbc83bdeec1d741bad442a1ea5f.jpg)
Finally, I choose to order by month, since that makes the data easier to read. I need to create a temporary column with month number in order to do this, as I don’t want the months in alphabetical order!
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image10.png/daf8c4f0259ce428269c0d3d4badd32b.jpg)
I create this column and sort by it.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image11.png/22c6daeb82d7d69ac88f878227e04b28.jpg)
I can now delete the Month Number column.
![](http://sumproduct-4634.kxcdn.com/img/containers/main/blog-pictures/2019/power-query/159/image12.png/a1537847463e660a31158c8032525438.jpg)
My data is now ready to append and update if further months are added. Next time, I will look at another example of unpivoting.
Come back next time for more ways to use Power Query!