Help with Unpivot/PowerQuery

megera716

Board Regular
Joined
Jan 3, 2013
Messages
140
Office Version
  1. 365
Platform
  1. Windows
I'm pretty savvy to Unpivot, Split Column and Pivot Columns within Power Query but I'm really struggling with this one. Hoping I'm missing something obvious or just doing my steps in the wrong order

I have a table like so with data from 2013-2019 (3 columns for every year with Member/Sales/Rep):
Customer NameCustomer Type2013 Member2013 Sales2013 Rep2014 Member2014 Sales2014 Rep2015...
AppleABCY15000SJY25000SJ
GoogleXYZN0Y10000CD


I am trying to get it into a table format that will look like this (thinking I would split the Member/Sales/Rep columns with a year in it on 4 characters to isolate the year) so that I can pivot by rep or year or whatever I fancy:
Customer NameCustomer TypeYearMemberSalesRep
AppleABC2013Y15000SJ
AppleABC2014Y25000SJ
GoogleXYZ2013N0
GoogleXYZ2014Y10000CD


I have tried seemingly every iteration of selecting or not selecting columns, unpivoting selected or unpivoting other. I think my last try, I selected just the sales and rep columns for each year, Unpivot Selected and ended up with this:
Customer NameCustomer TypeAttributeValue
AppleABC2013 Sales15000
AppleABC2013 RepSJ
AppleABC2014 Sales25000
AppleABC2014 RepSJ
GoogleXYZ2013 Sales0
GoogleXYZ2013 Rep
GoogleXYZ2014 Sales10000
GoogleXYZ2014 RepCD


Not what I'm going for :(. Can someone please advise as to what columns I should select/not select and what steps? Thank you in advance!
 
I'm not understanding your question. Please explain using an example of what is in my example and what should be in the example. Based upon your original post, my results look exactly like your expected results. Please clarify. If your sample is not representative of your actual data, please identify the differences or show us a modified sample.
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Book1
ABCDEF
1Customer NameCustomer TypeYearMemberSalesRep
2AppleABC2013Y15000SJ
3AppleABC2014Y25000SJ
4GoogleXYZ2013N0CD
5GoogleXYZ2014Y10000CD
Sheet3
 
Upvote 0
@alansidman I know this is a couple months old but I'm hoping you might be willing to help out again! When I run the code you provided, something is off as I mentioned earlier. I thought I could fix it by just deleting one cell from the column to shift the data up but it's still flip-flopped.

I start with this:
Name2013 Sales2013 Member2013 Rep2014 Sales2014 Member2014 Rep2015 Sales2015 Member2015 Rep2016 Sales2016 Member2016 Rep2017 Sales2017 Member2017 Rep2018 Sales2018 Member2018 Rep2019 Sales2019 Member2019 Rep
GooberCo
0​
N
0​
N
0​
N
0​
N
0​
N
0​
N
69,000​
YCA
Google
17,400​
YAM
17,600​
YHP
34,880​
YHP
32,000​
YHP
24,000​
YFG
13,000​
YHK
13,000​
NHK

And after I run the code you wrote, it gives me this. At first glance, it looks like the rep is just off by one row so that's why I tried just shifting it up one.
TypeNameYearMemberSalesRep
VendorGooberCo2013N
0​
VendorGooberCo2014N
0​
VendorGooberCo2015N
0​
VendorGooberCo2016N
0​
VendorGooberCo2017N
0​
VendorGooberCo2018N
69000​
VendorGooberCo2019Y
17400​
CA
VendorGoogle2013Y
17600​
AM
VendorGoogle2014Y
34880​
HP
VendorGoogle2015Y
32000​
HP
VendorGoogle2016Y
24000​
HP
VendorGoogle2017Y
13000​
FG
VendorGoogle2018Y
13000​
HK
VendorGoogle2019N
11200​
HK

But you can also see that if I shift it up one, it would put rep "AM" on 2019 for GooberCo but really, "AM" is the rep on Google for 2013 (which is correct).. Any ideas how I can fix this? I don't know enough about m-code to even start to figure out where it's going wrong :(.
 
Upvote 0
This looks like it is correct now

Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"2013 Sales", type text}, {"2013 Member", type text}, {"2013 Rep", type text}, {"2014 Sales", type text}, {"2014 Member", type text}, {"2014 Rep", type text}, {"2015 Sales", type text}, {"2015 Member", type text}, {"2015 Rep", type text}, {"2016 Sales", type text}, {"2016 Member", type text}, {"2016 Rep", type text}, {"2017 Sales", type text}, {"2017 Member", type text}, {"2017 Rep", type text}, {"2018 Sales", type text}, {"2018 Member", type text}, {"2018 Rep", type text}, {"2019 Sales", type text}, {"2019 Member", type text}, {"2019 Rep", type text}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", Int64.Type}, {"Attribute.2", type text}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type1",{{"Attribute.1", Order.Ascending}}),
    #"Pivoted Column" = Table.Pivot(#"Sorted Rows", List.Distinct(#"Sorted Rows"[Attribute.2]), "Attribute.2", "Value")
in
    #"Pivoted Column"

Book3
ABCDE
7NameAttribute.1SalesMemberRep
8GooberCo20130N
9GooberCo20140N
10GooberCo20150N
11GooberCo20160N
12GooberCo20170N
13GooberCo20180N
14GooberCo201969,000YCA
15Google201317,400YAM
16Google201417,600YHP
17Google201534,880YHP
18Google201632,000YHP
19Google201724,000YFG
20Google201813,000YHK
21Google201913,000NHK
Sheet1
 
Upvote 0
@alansidman thank you very much! Both for the corrected code and being willing to return to my lowly thread months later :). You have saved me DAYS of work (as I'm running the code on a table with 1200 rows of customers!)
 
Upvote 0

Forum statistics

Threads
1,215,734
Messages
6,126,542
Members
449,316
Latest member
sravya

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top