Unpivot some columns but retain others (variable data types)

crackednut

New Member
Joined
Feb 12, 2015
Messages
5
hi all,



I'm a power BI newbie and trying to get a handle on unpivoting multi-header columns. I followed the tips mentioned on the well detailed page (Unpivot Columns in Power BI - New Tech Dojo) . However, it slightly more complicated than the one shown on the page.



Link to the data set on G Drive--> unpivot test data_v02 sample.xlsx



To summarise the problem statement. My current dataset sits something like this (there are around 25 markets)



market01 market 02
channeldatetitleprogrammedatatype1...datatype6 datatype1...datatype6


the required output would need to look this:



channeldatetitleprogramemarketdatatype1datatype2datatype3...dataype6
market01
market01
market02
market02


the normal unpivot solutions all are pointing in the direction where the 6 dataypes on the right hand side get merged into a single column. only problem with that approach is they are all different datatypes (numeric, time etc)



I believe custom coding in M holds some magic to unlock this mystery. I have very little knowledge of M and am knocking on this forum's doors. How does one go about doing this?

I must also mention that I am dealing with an extremely large excel sheets here. These are massive data dumps around 600MB of excel files each. Hence I am wary of not grinding my machine to a halt with excessive & undesirable unpivot queries. I tried it doing unpivotting one bunch of column at a time but the number of rows increasing exponentially. This is what leads me to believe that M coding might do the trick.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!


Cross posted at: Unpivot some columns but retain others (variable data types)
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
is that what you want?

part of data
#Channel#date#Weekday#Week#Start time#end time#Programme#genre#Programme Language#Dur (sec)#Dur (HH:MM:SS)#Program themeMarketdata_type01data_type02data_type03data_type04data_type05data_type06
channel0243571Tuesday160.4999074070.633078704program18genre01lang01115060.133171296theme01market01651529.316507.96128078.464.310.029490741
channel0143646Sunday270.823379630.936597222program16genre01lang0197820.113217593theme01market01649847.546502132609.285.020.022256944
channel0243563Monday150.8333564810.962511574program02genre01lang01111590.129155093theme01market01650009.5813330.642.0541297.756.350.040532407
channel0143669Tuesday300.5207175930.627083333program07genre01lang0191900.106365741theme01market01651176.449894.71.5232705.145.020.031053241
channel0243710Monday360.5000810190.622337963program04genre01lang01105630.122256944theme01market01651972.8212634.391.9434135.025.240.043506944
channel0243643Thursday260.8336574070.968229167program11genre01lang01116270.134571759theme01market01648693.67277.971.1230723.754.740.03119213
channel0243613Tuesday220.8333333330.955706019program12genre01lang01105730.122372685theme01market01650527.077276.671.1234771.085.350.025150463
channel0243518Friday80.5004398150.636099537program08genre01lang01117210.135659722theme01market01650948.628790.31.3530794.384.730.037303241
channel0143806Saturday490.8331134260.951712963program16genre01lang01102470.118599537theme01market01651774.576423.450.9932888.095.050.022546296
channel0143769Thursday440.829942130.956377315program06genre01lang01109240.126435185theme01market01650147.539888.651.5234626.565.330.034953704
channel0143691Wednesday330.5417245370.671990741program13genre01lang01112550.130266204theme01market01650345.397049.111.0828671.914.410.031331019
channel0143824Wednesday520.5207060190.613171296program09genre01lang0179890.092465278theme01market01651786.198732.151.3429422.944.510.026539352
channel0243479Monday30.4998842590.621273148program14genre01lang01104880.121388889theme01market01651200.447032.691.0827865.14.280.029363426
channel0143849Sunday40.5209143520.638993056program03genre01lang01102020.118078704theme01market01651376.512940.141.9937675.765.780.039386574
channel0143846Thursday30.5000694440.627430556program01genre01lang01110040.127361111theme01market01649301.5116436.922.5342452.546.540.04744213
channel0243776Thursday450.5001736110.630300926program05genre01lang01112430.130127315theme01market01650543.4812448.491.9142268.86.50.037071759
channel0343619Monday230.7557407410.837962963program17genre01lang0271040.082222222theme01market016494776548.981.0127490.534.230.019513889
channel0443727Thursday380.8303819440.987210648program19genre01lang01135500.156828704theme01market01649888.16449.30.9927303.044.20.035150463
channel0343783Thursday460.7902083330.883252315program10genre01lang0280390.093043981theme01market01649490.247791.081.232520.235.010.022141204
channel0443537Wednesday110.8301620370.988217593program20genre01lang01136560.158055556theme01market01650224.366407.490.9940346.356.20.024444444
channel0343733Wednesday390.7751273150.876585648program21genre01lang0287660.101458333theme01market01649801.526394.640.9831761.044.890.020335648
channel0343790Thursday470.7508564810.8446875program22genre01lang0281070.093831019theme01market01649406.546167.290.9529945.994.610.019212963
channel0343521Monday90.7958333330.893368056program15genre01lang0284270.097534722theme01market01650579.46906.741.0627911.84.290.023888889
channel0143518Friday80.4633101850.579016204program16genre01lang0199970.115706019theme01market01650948.626634.651.0228919.094.440.025243056
channel0143824Wednesday520.5207060190.613171296program09genre01lang0179890.092465278theme01market02445383.188561.441.9228728.066.450.026631944
channel0143846Thursday30.5000694440.627430556program01genre01lang01110040.127361111theme01market02443311.9815866.093.5841115.889.270.047303241
channel0343790Thursday470.7508564810.8446875program22genre01lang0281070.093831019theme01market02443263.416067.171.3729346.766.620.01931713
channel0243571Tuesday160.4999074070.633078704program18genre01lang01115060.133171296theme01market02444857.856392.991.4427395.846.160.029675926
channel0143691Wednesday330.5417245370.671990741program13genre01lang01112550.130266204theme01market02443854.826811.121.5327740.266.250.031273148
channel0143669Tuesday300.5207175930.627083333program07genre01lang0191900.106365741theme01market024444989573.192.1531786.737.150.030925926
channel0143769Thursday440.829942130.956377315program06genre01lang01109240.126435185theme01market02443777.99629.862.1733584.567.570.035104167
channel0143806Saturday490.8331134260.951712963program16genre01lang01102470.118599537theme01market02445096.166196.581.3931742.687.130.022534722
channel0443537Wednesday110.8301620370.988217593program20genre01lang01136560.158055556theme01market02443618.086126.81.3837987.458.560.024803241
channel0243613Tuesday220.8333333330.955706019program12genre01lang01105730.122372685theme01market02443942.137040.261.5933686.527.590.025104167
channel0343521Monday90.7958333330.893368056program15genre01lang0284270.097534722theme01market02444235.076766.491.5227403.886.170.023854167
channel0343619Monday230.7557407410.837962963program17genre01lang0271040.082222222theme01market02443327.36427.171.4526928.626.070.019548611
channel0243710Monday360.5000810190.622337963program04genre01lang01105630.122256944theme01market02444755.1512188.182.7432955.757.410.0434375
channel0243776Thursday450.5001736110.630300926program05genre01lang01112430.130127315theme01market02444475.6112164.732.7441114.849.250.037222222
channel0243643Thursday260.8336574070.968229167program11genre01lang01116270.134571759theme01market02442956.157039.141.5929635.826.690.031284722
channel0443727Thursday380.8303819440.987210648program19genre01lang01135500.156828704theme01market02443114.025979.061.3525803.255.820.034409722
channel0243479Monday30.4998842590.621273148program14genre01lang01104880.121388889theme01market02444050.826697.921.5126786.076.030.0290625
channel0343783Thursday460.7902083330.883252315program10genre01lang0280390.093043981theme01market02443435.47593.951.7131718.257.150.02212963
channel0143849Sunday40.5209143520.638993056program03genre01lang01102020.118078704theme01market02444495.0512598.962.8336761.68.270.039282407
channel0243563Monday150.8333564810.962511574program02genre01lang01111590.129155093theme01market02443644.919827.662.2233983.647.660.03630787
channel0343733Wednesday390.7751273150.876585648program21genre01lang0287660.101458333theme01market02443579.196305.21.4231115.987.010.020462963
channel0243518Friday80.5004398150.636099537program08genre01lang01117210.135659722theme01market02444116.498597.011.9429854.746.720.037627315
channel0143518Friday80.4633101850.579016204program16genre01lang0199970.115706019theme01market02444116.496385.931.4428085.286.320.025023148
channel0143646Sunday270.823379630.936597222program16genre01lang0197820.113217593theme01market02443214.656300.21.4231702.447.150.0221875
channel0343790Thursday470.7508564810.8446875program22genre01lang0281070.093831019theme01market0370372.93230.980.331112.171.580.019490741
channel0143849Sunday40.5209143520.638993056program03genre01lang01102020.118078704theme01market0370505.222368.183.366888.979.770.040162037
 
Upvote 0
Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!


Cross posted at: Unpivot some columns but retain others (variable data types)
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
apologies since I didn't not read the forums rules in detail before posting.
 
Upvote 0
here is M-code (Power Query) for this
Rich (BB code):
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    TRC = Table.RemoveColumns(Table.FillDown(Table.AddColumn(Table.Transpose(Table.DemoteHeaders(Source)), "Custom", each if Text.Contains([Column1], "Column") then null else [Column1]),{"Custom"}),{"Column1"}),
    Reorder = Table.ReorderColumns(TRC,{"Custom", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23", "Column24", "Column25", "Column26"}),
    Promote = Table.PromoteHeaders(Table.Transpose(Table.CombineColumns(Reorder,{"Custom", "Column2"},Combiner.CombineTextByDelimiter("#", QuoteStyle.None),"Merged")), [PromoteAllScalars=true]),
    UOC = Table.UnpivotOtherColumns(Promote, {"#Channel", "#date", "#Weekday", "#Week", "#Start time", "#end time", "#Programme", "#genre", "#Programme Language", "#Dur (sec)", "#Dur (HH:MM:SS)", "#Program theme"}, "Attribute", "Value"),
    Split = Table.SplitColumn(UOC, "Attribute", Splitter.SplitTextByAnyDelimiter({"#"}, QuoteStyle.Csv)),
    Promote1 = Table.PromoteHeaders(Table.Transpose(Table.ReplaceValue(Table.Transpose(Table.DemoteHeaders(Table.RenameColumns(Table.Sort(Table.Pivot(Split, List.Distinct(Split[Attribute.2]), "Attribute.2", "Value"),{{"Attribute.1", Order.Ascending}}),{{"Attribute.1", "Market"}}))),"#","",Replacer.ReplaceText,{"Column1"})), [PromoteAllScalars=true]),
    Type = Table.TransformColumnTypes(Promote1,{{"Channel", type text}, {"date", type date}, {"Weekday", type text}, {"Week", Int64.Type}, {"Start time", type time}, {"end time", type time}, {"Programme", type text}, {"genre", type text}, {"Programme Language", type text}, {"Dur (sec)", Int64.Type}, {"Dur (HH:MM:SS)", type time}, {"Program theme", type text}, {"Market", type text}, {"data_type01", type number}, {"data_type02", type number}, {"data_type03", type number}, {"data_type04", type number}, {"data_type05", type number}, {"data_type06", type number}})
in
    Type
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,796
Messages
6,174,658
Members
452,575
Latest member
Fstick546

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