Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
ngct1112
Post Patron
Post Patron

How to unpivot the wide dataset

Hi All,

is it possible I could unpivot the table to vertical format? Great Thanks

IDDescriptionFromToPriceFromToPriceFrom ToPrice
1AA1002000.3842003007.1643004000.316
2BB1002000.6472003001.2623004000.585
3CC1002001.9522003001.2223004000.474
1DD1002000.2822003000.4763004000.641
2EE1002000.5032003000.4563004001.302
3FF1002000.7052003000.2433004004.778

 

IDDescriptionFromToPrice
1AA1002000.384
1AA2003007.164
1AA3004000.316
1DD1002000.282
1DD2003000.476
1DD3004000.641
2BB1002000.647
2BB2003001.262
2BB3004000.585
2EE1002000.503
2EE2003000.456
2EE3004001.302
3CC1002001.952
3CC2003001.222
3CC3004000.474
3FF1002000.705
3FF2003000.243
3FF3004004.778

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Description", type text}, {"From", Int64.Type}, {"To", Int64.Type}, {"Price", type number}, {"From2", Int64.Type}, {"To3", Int64.Type}, {"Price4", type number}, {"From ", Int64.Type}, {"To5", Int64.Type}, {"Price6", type number}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID", "Description"}, "Attribute", "Value"),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Attribute.1", "Attribute.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Character Transition",{"Attribute.2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Removed Columns",{{"Attribute.1", Text.Trim, type text}}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Trimmed Text", {{"ID", type text}}, "en-IN"),{"ID", "Attribute.1"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
    #"Added Index" = Table.AddIndexColumn(#"Merged Columns", "Index1", 1, 1),
    Partition = Table.Group(#"Added Index", {"Merged"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Description", "Value", "Index1", "Index"}, {"Description", "Value", "Index1", "Index"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Partition",{{"Index1", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Index1"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns1", "Merged", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Merged.1", "Merged.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged.1", Int64.Type}, {"Merged.2", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Merged.1", "ID"}}),
    #"Pivoted Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[Merged.2]), "Merged.2", "Value"),
    #"Removed Columns2" = Table.RemoveColumns(#"Pivoted Column",{"Index"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns2",{{"From", type number}, {"To", type number}, {"Price", type number}})
in
    #"Changed Type2"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Description", type text}, {"From", Int64.Type}, {"To", Int64.Type}, {"Price", type number}, {"From2", Int64.Type}, {"To3", Int64.Type}, {"Price4", type number}, {"From ", Int64.Type}, {"To5", Int64.Type}, {"Price6", type number}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"ID", "Description"}, "Attribute", "Value"),
    #"Split Column by Character Transition" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Attribute.1", "Attribute.2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Split Column by Character Transition",{"Attribute.2"}),
    #"Trimmed Text" = Table.TransformColumns(#"Removed Columns",{{"Attribute.1", Text.Trim, type text}}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Trimmed Text", {{"ID", type text}}, "en-IN"),{"ID", "Attribute.1"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
    #"Added Index" = Table.AddIndexColumn(#"Merged Columns", "Index1", 1, 1),
    Partition = Table.Group(#"Added Index", {"Merged"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}}),
    #"Expanded Partition" = Table.ExpandTableColumn(Partition, "Partition", {"Description", "Value", "Index1", "Index"}, {"Description", "Value", "Index1", "Index"}),
    #"Sorted Rows" = Table.Sort(#"Expanded Partition",{{"Index1", Order.Ascending}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Sorted Rows",{"Index1"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns1", "Merged", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Merged.1", "Merged.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged.1", Int64.Type}, {"Merged.2", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Merged.1", "ID"}}),
    #"Pivoted Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[Merged.2]), "Merged.2", "Value"),
    #"Removed Columns2" = Table.RemoveColumns(#"Pivoted Column",{"Index"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Removed Columns2",{{"From", type number}, {"To", type number}, {"Price", type number}})
in
    #"Changed Type2"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur , the solution looks brilliant and work for me.

However, is it possible you explain a little bit on this? it is a group function without using the group button?

= Table.Group(#"Merged Columns", {"Merged"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}})

You are welcome.  That step is basically restarting the index number column on every change in entry in the "Merged columns" column.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
amitchandak
Super User
Super User

@ngct1112 , Refer if this can help : https://kohera.be/blog/power-bi/how-to-unpivot-twice/

 

Detailed steps has been provided by Author.

 

Please Watch/Like/Share My webinar on Time Intelligence: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-Y...
My Youtube Tips at: https://www.youtube.com/playlist?list=PLPaNVDMhUXGYrm5rm6ME6rjzKGSvT9Jmy
Appreciate your Kudos.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Greg_Deckler
Community Champion
Community Champion

@ngct1112 I did it like the attached PBIX file.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors