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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
heba_q
Regular Visitor

Transform a matrix into a table from Power Query Editor

Hello everyone,

 

Below are tables 1 and 2. Table 1 is the original format that I currently have in my data source Excel file, while table 2 is how I want the table to look like in the Power Query editor. So basically I want to remove row #2 (which includes Act and FCST) and Total FY columns from table 1. Then, I want to transpose the Cases, Tons, and months' names and values columns, while ungrouping the other columns (Category, Type, ..., Weight). Can you please help me? This is a OneDrive link for the excel file (table 1 is in sheet1 and table 2 is in sheet2) --> https://1drv.ms/x/s!AvoXXWI9anTxhC4M_yqKFY-XC8Ty?e=RaRiqV

 

Table 1:

 

          CasesCasesCasesCasesCasesCasesCasesCasesCasesCasesCasesCasesCasesTONSTONSTONSTONSTONSTONSTONSTONSTONSTONSTONSTONSTONS
          ActActActActActActFCSTFCSTFCSTFCSTFCSTFCST              
CategoryTypeNew CodesDescriptionBrandPricepointFlavorNOTESRatWeightJan-22Feb-22Mar-22Apr-22May-22Jun-22Jul-22Aug-22Sep-22Oct-22Nov-22Dec-22Total FYJan-22Feb-22Mar-22Apr-22May-22Jun-22Jul-22Aug-22Sep-22Oct-22Nov-22Dec-22Total FY
ABABC123ABCDEFGOE 5ABCD 33.1804794474113724251468013 78.8481437.179833.4066611.7314815.4769710.6812730.6728727.691494.11070529.0046355.823443.355371 

 

Table 2 (the desired result):

 

CategoryTypeNew CodesDescriptionBrandPricepointFlavorNOTESRatWeightMonthCasesTONS
A1ABC123ABCDEFGOE 5ABCD 33.1Jan-228078.84814
A1ABC123ABCDEFGOE 5ABCD 33.1Feb-224737.1798
A1ABC123ABCDEFGOE 5ABCD 33.1Mar-229433.40666
A1ABC123ABCDEFGOE 5ABCD 33.1Apr-224711.73148
A1ABC123ABCDEFGOE 5ABCD 33.1May-224115.47697
A1ABC123ABCDEFGOE 5ABCD 33.1Jun-221310.68127
A1ABC123ABCDEFGOE 5ABCD 33.1Jul-227230.67287
A1ABC123ABCDEFGOE 5ABCD 33.1Aug-224227.69149
A1ABC123ABCDEFGOE 5ABCD 33.1Sep-22514.110705
A1ABC123ABCDEFGOE 5ABCD 33.1Oct-224629.00463
A1ABC123ABCDEFGOE 5ABCD 33.1Nov-228055.82344
A1ABC123ABCDEFGOE 5ABCD 33.1Dec-22133.355371

 

Thank you!

2 ACCEPTED SOLUTIONS
v-yalanwu-msft
Community Support
Community Support

Hi, @heba_q ;

You could use unpivot it. here is the full M statement.

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\Administrator\Downloads\Matrix to table.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Cases_12", "TONS_24"}),
    #"Promoted Headers1" = Table.PromoteHeaders(#"Removed Columns", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers1",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type any}, {"Column10", type any}, {"Act", type any}, {"Act_1", type any}, {"Act_2", type any}, {"Act_3", type any}, {"Act_4", type any}, {"Act_5", type any}, {"FCST", type any}, {"FCST_6", type any}, {"FCST_7", type any}, {"FCST_8", type any}, {"FCST_9", type any}, {"FCST_10", type any}, {"Column23", type any}, {"Column24", type any}, {"Column25", type any}, {"Column26", type any}, {"Column27", type any}, {"Column28", type any}, {"Column29", type any}, {"Column30", type any}, {"Column31", type any}, {"Column32", type any}, {"Column33", type any}, {"Column34", type any}}),
    #"Promoted Headers2" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Promoted Headers2", {"Category", "Type", "New Codes", "Description", "Brand", "Pricepoint", "Flavor", "NOTES", "Rat", "Weight", "1/1/2022_1", "2/2/2022_2", "3/6/2022_3", "4/7/2022_4", "5/9/2022_5", "6/10/2022_6", "7/12/2022_7", "8/13/2022_8", "9/14/2022_9", "10/16/2022_10", "11/17/2022_11", "12/19/2022_12"}, "Attribute", "Value"),
    #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Unpivoted Columns", {"Category", "Type", "New Codes", "Description", "Brand", "Pricepoint", "Flavor", "NOTES", "Rat", "Weight", "Attribute", "Value"}, "Attribute.1", "Value.1"),
    #"Extracted Text Before Delimiter" = Table.TransformColumns(#"Unpivoted Columns1", {{"Attribute.1", each Text.BeforeDelimiter(_, "_"), type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Extracted Text Before Delimiter", "Custom", each if [Attribute.1] = [Attribute] then 1 else null),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = 1)),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Attribute.1", "Custom"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Attribute", "Month"}, {"Value", "Cases"}, {"Value.1", "TONS"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Month", type date}})
in
    #"Changed Type1"

 

The final show:

vyalanwumsft_0-1660804294937.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1660808943300.png

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    Custom1 = let a=Table.ToRows(Source)
              in #table(
                        List.FirstN(a{2},10)&{"Month","Cases","TONS"},
                        List.TransformMany(
                                           List.Skip(a,3),
                                           each List.RemoveLastN(List.Zip({List.Range(a{2},10,13)}&List.Split(List.Skip(_,10),13))),
                                           (x,y)=>List.FirstN(x,10)&y
                                          )
                       )
in
    Custom1

source is imported from raw data without headers

View solution in original post

2 REPLIES 2
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1660808943300.png

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    Custom1 = let a=Table.ToRows(Source)
              in #table(
                        List.FirstN(a{2},10)&{"Month","Cases","TONS"},
                        List.TransformMany(
                                           List.Skip(a,3),
                                           each List.RemoveLastN(List.Zip({List.Range(a{2},10,13)}&List.Split(List.Skip(_,10),13))),
                                           (x,y)=>List.FirstN(x,10)&y
                                          )
                       )
in
    Custom1

source is imported from raw data without headers

v-yalanwu-msft
Community Support
Community Support

Hi, @heba_q ;

You could use unpivot it. here is the full M statement.

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\Administrator\Downloads\Matrix to table.xlsx"), null, true),
    Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
    #"Removed Columns" = Table.RemoveColumns(#"Promoted Headers",{"Cases_12", "TONS_24"}),
    #"Promoted Headers1" = Table.PromoteHeaders(#"Removed Columns", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers1",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type any}, {"Column10", type any}, {"Act", type any}, {"Act_1", type any}, {"Act_2", type any}, {"Act_3", type any}, {"Act_4", type any}, {"Act_5", type any}, {"FCST", type any}, {"FCST_6", type any}, {"FCST_7", type any}, {"FCST_8", type any}, {"FCST_9", type any}, {"FCST_10", type any}, {"Column23", type any}, {"Column24", type any}, {"Column25", type any}, {"Column26", type any}, {"Column27", type any}, {"Column28", type any}, {"Column29", type any}, {"Column30", type any}, {"Column31", type any}, {"Column32", type any}, {"Column33", type any}, {"Column34", type any}}),
    #"Promoted Headers2" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Promoted Headers2", {"Category", "Type", "New Codes", "Description", "Brand", "Pricepoint", "Flavor", "NOTES", "Rat", "Weight", "1/1/2022_1", "2/2/2022_2", "3/6/2022_3", "4/7/2022_4", "5/9/2022_5", "6/10/2022_6", "7/12/2022_7", "8/13/2022_8", "9/14/2022_9", "10/16/2022_10", "11/17/2022_11", "12/19/2022_12"}, "Attribute", "Value"),
    #"Unpivoted Columns1" = Table.UnpivotOtherColumns(#"Unpivoted Columns", {"Category", "Type", "New Codes", "Description", "Brand", "Pricepoint", "Flavor", "NOTES", "Rat", "Weight", "Attribute", "Value"}, "Attribute.1", "Value.1"),
    #"Extracted Text Before Delimiter" = Table.TransformColumns(#"Unpivoted Columns1", {{"Attribute.1", each Text.BeforeDelimiter(_, "_"), type text}}),
    #"Added Conditional Column" = Table.AddColumn(#"Extracted Text Before Delimiter", "Custom", each if [Attribute.1] = [Attribute] then 1 else null),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each ([Custom] = 1)),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows",{"Attribute.1", "Custom"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Attribute", "Month"}, {"Value", "Cases"}, {"Value.1", "TONS"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Month", type date}})
in
    #"Changed Type1"

 

The final show:

vyalanwumsft_0-1660804294937.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.