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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
parry2k
Super User
Super User

need help with M query to unpivot

Hi there,

 

I have data in excel sheet whcih I want unpivot, see attached link for sample file. anyhelp will be appreciated.

 

https://drive.google.com/file/d/0B4dPgH9_BPBrU0p3OVFCMkxpYVk/view?usp=sharing

thanks in advance,

 

p



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

1 ACCEPTED SOLUTION

This is one way to do it.

Remark: lately I have been exploring conversions between lists, records and tables in Power Query.

This may have influenced the solution Smiley Happy

 

let
    Source = Excel.CurrentWorkbook(){[Name="RawData"]}[Content],

    // 1. Transpose and pivot right part of the table
    #"Removed Columns" = Table.RemoveColumns(Source,{"Column1", "Column2"}),
    #"Transposed Table" = Table.Transpose(#"Removed Columns"),
    #"Removed Columns1" = Table.RemoveColumns(#"Transposed Table",{"Column1"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns1", "Index", 0, 1),
    #"Integer Divided" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 4), type number}}),
    #"Merged Columns" = Table.CombineColumns(#"Integer Divided",{"Column3", "Column4", "Column5", "Column6"}, each _, "Merged"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Column2]), "Column2", "Merged"),
    Zipped = Table.CombineColumns(#"Pivoted Column",{"Accounts", "Invoices", "Monthly Billing", "Balance"}, each List.Zip(_),"Zipped"),

    // 2. Fill down first 2 columns:
    #"Removed Top Rows" = Table.Skip(Source,2),
    #"Filled Down1" = Table.FillDown(#"Removed Top Rows",{"Column1"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Filled Down1",{"Column1", "Column2"}),
    #"Merged Columns1" = Table.CombineColumns(#"Removed Other Columns",{"Column1", "Column2"},each Record.FromList(_,{"Type", "Category"}),"TypeCat"),

    // 3. List with clubs:
    Clubs = List.Select(Record.ToList(Source{0}), each _ <> null),

    // 4. Combine Clubs with part1 and expand:
    Custom2 = Table.FromColumns({Clubs,Zipped[Zipped]}),
    #"Expanded Column2" = Table.ExpandListColumn(Custom2, "Column2"),
    
    // 5. Repeat first 2 columns:
    Custom3 = Table.Repeat(#"Merged Columns1",Table.RowCount(#"Expanded Column2")/Table.RowCount(#"Merged Columns1")),

    // 6. Combine all parts and expand:
    Custom4 = Table.FromColumns({Custom3[TypeCat],Table.ToRecords(#"Expanded Column2")}),
    #"Expanded Column1" = Table.ExpandRecordColumn(Custom4, "Column1", {"Type", "Category"}, {"Type", "Category"}),
    #"Expanded Column3" = Table.ExpandRecordColumn(#"Expanded Column1", "Column2", {"Column1", "Column2"}, {"Column1", "Column2.1"}),

    // 7. Create columns from nested lists:
    RecordFromList = Table.TransformColumns(#"Expanded Column3",{{"Column2.1", each Record.FromList(_,{"Accounts", "Invoices", "Monthly Billing", "Balance"})}}),
    #"Expanded Column2.1" = Table.ExpandRecordColumn(RecordFromList, "Column2.1", {"Accounts", "Invoices", "Monthly Billing", "Balance"}, {"Accounts", "Invoices", "Monthly Billing", "Balance"}),

    // 8. Finishing touches.
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Column2.1",{{"Column1", "Club"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Type", type text}, {"Category", type text}, {"Club", type text}, {"Accounts", type any}, {"Invoices", Int64.Type}, {"Monthly Billing", Currency.Type}, {"Balance", Currency.Type}})
in
    #"Changed Type"
Specializing in Power Query Formula Language (M)

View solution in original post

4 REPLIES 4
GilbertQ
Super User
Super User

Hi there, that can possibly be done, but there would be quite a few steps involved where you would need to possibly transpose, then fill data down and up, transpose it back and then unpivot.

 

Or in some instances you would need to know which data is in which section and then create new columns based on the area's.

 

The great thing is that in the query editor you could try different ways, until you get it into the format you require.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!







Power BI Blog

This is one way to do it.

Remark: lately I have been exploring conversions between lists, records and tables in Power Query.

This may have influenced the solution Smiley Happy

 

let
    Source = Excel.CurrentWorkbook(){[Name="RawData"]}[Content],

    // 1. Transpose and pivot right part of the table
    #"Removed Columns" = Table.RemoveColumns(Source,{"Column1", "Column2"}),
    #"Transposed Table" = Table.Transpose(#"Removed Columns"),
    #"Removed Columns1" = Table.RemoveColumns(#"Transposed Table",{"Column1"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns1", "Index", 0, 1),
    #"Integer Divided" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 4), type number}}),
    #"Merged Columns" = Table.CombineColumns(#"Integer Divided",{"Column3", "Column4", "Column5", "Column6"}, each _, "Merged"),
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Column2]), "Column2", "Merged"),
    Zipped = Table.CombineColumns(#"Pivoted Column",{"Accounts", "Invoices", "Monthly Billing", "Balance"}, each List.Zip(_),"Zipped"),

    // 2. Fill down first 2 columns:
    #"Removed Top Rows" = Table.Skip(Source,2),
    #"Filled Down1" = Table.FillDown(#"Removed Top Rows",{"Column1"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Filled Down1",{"Column1", "Column2"}),
    #"Merged Columns1" = Table.CombineColumns(#"Removed Other Columns",{"Column1", "Column2"},each Record.FromList(_,{"Type", "Category"}),"TypeCat"),

    // 3. List with clubs:
    Clubs = List.Select(Record.ToList(Source{0}), each _ <> null),

    // 4. Combine Clubs with part1 and expand:
    Custom2 = Table.FromColumns({Clubs,Zipped[Zipped]}),
    #"Expanded Column2" = Table.ExpandListColumn(Custom2, "Column2"),
    
    // 5. Repeat first 2 columns:
    Custom3 = Table.Repeat(#"Merged Columns1",Table.RowCount(#"Expanded Column2")/Table.RowCount(#"Merged Columns1")),

    // 6. Combine all parts and expand:
    Custom4 = Table.FromColumns({Custom3[TypeCat],Table.ToRecords(#"Expanded Column2")}),
    #"Expanded Column1" = Table.ExpandRecordColumn(Custom4, "Column1", {"Type", "Category"}, {"Type", "Category"}),
    #"Expanded Column3" = Table.ExpandRecordColumn(#"Expanded Column1", "Column2", {"Column1", "Column2"}, {"Column1", "Column2.1"}),

    // 7. Create columns from nested lists:
    RecordFromList = Table.TransformColumns(#"Expanded Column3",{{"Column2.1", each Record.FromList(_,{"Accounts", "Invoices", "Monthly Billing", "Balance"})}}),
    #"Expanded Column2.1" = Table.ExpandRecordColumn(RecordFromList, "Column2.1", {"Accounts", "Invoices", "Monthly Billing", "Balance"}, {"Accounts", "Invoices", "Monthly Billing", "Balance"}),

    // 8. Finishing touches.
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Column2.1",{{"Column1", "Club"}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Type", type text}, {"Category", type text}, {"Club", type text}, {"Accounts", type any}, {"Invoices", Int64.Type}, {"Monthly Billing", Currency.Type}, {"Balance", Currency.Type}})
in
    #"Changed Type"
Specializing in Power Query Formula Language (M)

Previous post corrected.

Specializing in Power Query Formula Language (M)

Thanks @MarcelBeug

 

I'm going to try this, I was also doing this on my end using lists/records and able to develop something but running bit slow. Going to look at your solution.

 

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.