Help with Unpivot/PowerQuery

megera716

Board Regular
Joined
Jan 3, 2013
Messages
146
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!
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Here is the Mcode I used to achieve your results.

VBA Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer Name", type text}, {"Customer Type", type text}, {"2013 Member", type text}, {"2013 Sales", Int64.Type}, {"2013 Rep", type text}, {"2014 Member", type text}, {"2014 Sales", Int64.Type}, {"2014 Rep", type text}, {"2015...", type any}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Customer Name", "Customer Type"}, "Attribute", "Value"),
    #"Added Custom" = Table.AddColumn(#"Unpivoted Other Columns", "Member", each if Text.Contains([Attribute],"Member") then [Value] else null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Sales", each if Text.Contains([Attribute],"Sales") then [Value] else null),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Rep", each if Text.Contains([Attribute], "Rep") then[Value] else null),
    #"Filled Up" = Table.FillUp(#"Added Custom2",{"Rep", "Sales"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Up", each ([Member] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Value"}),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Removed Columns", "Attribute", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"Attribute.1", "Attribute.2"}),
    #"Removed Columns1" = Table.RemoveColumns(#"Split Column by Character Transition",{"Attribute.2"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Attribute.1", "Year"}})
in
    #"Renamed Columns"

Book1
ABCDEF
2AppleABC2013Y15000SJ
3AppleABC2014Y25000SJ
4GoogleXYZ2013N0CD
5GoogleXYZ2014Y10000CD
Sheet2
 
Upvote 0
Alan, thanks for writing that code for me! I'm definitely not at all familiar with VBA. Do I just open the editor and paste that in? For the "2015..." column, I put that to indicate that the Member/Sales/Rep columns continue for 2015-2019, but that's not the actual text of the column header. Can I just copy and paste what you wrote for 2013 and 2014 to populate for the remaining years?
 
Upvote 0
This is not a VBA code. You have to go to Advance Editor in Power Query and Just paste the code in advance editor.
 
Upvote 0
MCode
select your range/table
Data - From Table
it will open Power Query Editor
find and open Advanced Editor
replace whole code there with code from the post
be sure the name of the source table is the same as in the code (here: Table1)
Done
Close&Load
 
Upvote 0
here is compatible M with older version of PQ
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    UOC = Table.UnpivotOtherColumns(Source, {"Customer Name", "Customer Type"}, "Attribute", "Value"),
    Members = Table.AddColumn(UOC, "Member", each if Text.Contains([Attribute],"Member") then [Value] else null),
    Sales = Table.AddColumn(Members, "Sales", each if Text.Contains([Attribute],"Sales") then [Value] else null),
    Rep = Table.AddColumn(Sales, "Rep", each if Text.Contains([Attribute], "Rep") then[Value] else null),
    FillU = Table.FillUp(Rep,{"Rep", "Sales"}),
    FilterNull = Table.SelectRows(FillU, each ([Member] <> null)),
    RC = Table.RemoveColumns(FilterNull,{"Value"}),
    SCBD = Table.SplitColumn(RC, "Attribute", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    RC1 = Table.RemoveColumns(SCBD,{"Attribute.2"}),
    Ren = Table.RenameColumns(RC1,{{"Attribute.1", "Year"}})
in
    Ren
 
Upvote 0
here is compatible M with older version of PQ
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    UOC = Table.UnpivotOtherColumns(Source, {"Customer Name", "Customer Type"}, "Attribute", "Value"),
    Members = Table.AddColumn(UOC, "Member", each if Text.Contains([Attribute],"Member") then [Value] else null),
    Sales = Table.AddColumn(Members, "Sales", each if Text.Contains([Attribute],"Sales") then [Value] else null),
    Rep = Table.AddColumn(Sales, "Rep", each if Text.Contains([Attribute], "Rep") then[Value] else null),
    FillU = Table.FillUp(Rep,{"Rep", "Sales"}),
    FilterNull = Table.SelectRows(FillU, each ([Member] <> null)),
    RC = Table.RemoveColumns(FilterNull,{"Value"}),
    SCBD = Table.SplitColumn(RC, "Attribute", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
    RC1 = Table.RemoveColumns(SCBD,{"Attribute.2"}),
    Ren = Table.RenameColumns(RC1,{{"Attribute.1", "Year"}})
in
    Ren

Sandy! This was incredible! It gave me the exact layout I wanted but it didn't quiiite work -- the rep is offset from the sales by one row. I pasted the exact code you provided other than I changed the name to Table3 (which is the actual name). Can you help here?
NameYearMemberSalesRep
Apple2013N
0​
Apple2014N
0​
Apple2015N
0​
Apple2016N
0​
Apple2017N
6000​
Apple2018N
14000​
HK
Apple2019N
15980​
HK
Google2013N
0​
BB
Google2014N
0​
Google2015N
14940​
Google2016N
0​
BB
Google2017N
0​
Google2018N
0​
Google2019N
0​
 
Upvote 0
Ooh, I didn't know you could tag people!

Alan, I used Sandy's code because I wasn't sure how to add the actual column names (regarding my earlier post where I mentioned that "2015..." was not an actual column header) and his didn't specify column names. Can you advise on why the rep is off by one row from the rest of the data for that year?
 
Upvote 0

Forum statistics

Threads
1,223,785
Messages
6,174,535
Members
452,570
Latest member
Ron1970

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