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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

How to transpose data twice

Can you please help me with transposing the following data in PowerBI

 

CURRENT STRUCTURE

 

CompanySalesSalesSalesProfitProfitProfit
 31/12/201031/12/201131/12/201231/12/201031/12/201131/12/2012
A100125150255075
B2002252506080120
C300350400140160180

 

DESIRED STRUCTURE

 

CompanyDateAttributeAmount
A31/12/2010Sales100
A31/12/2011Sales125
A31/12/2012Sales150
A31/12/2010Profit25
A31/12/2011Profit50
A31/12/2012Profit75
B31/12/2010Sales200
B31/12/2011Sales225
B31/12/2012Sales250
B31/12/2010Profit60
B31/12/2011Profit80
B31/12/2012Profit120
1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@Anonymous  you can try this

 

let
    Source = Web.BrowserContents("https://community.powerbi.com/t5/Desktop/How-to-transpose-data-twice/m-p/949463#M454935"),
    #"Extracted Table From Html" = Html.Table(Source, {{"Column1", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(1), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(1)"}, {"Column2", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(2), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(2)"}, {"Column3", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(3), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(3)"}, {"Column4", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(4), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(4)"}, {"Column5", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(5), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(5)"}, {"Column6", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(6), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(6)"}, {"Column7", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(7), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(7)"}}, [RowSelector="DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR, DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR"]),
    #"Changed Type" = Table.TransformColumnTypes(#"Extracted Table From Html",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    not0not1 = Table.SelectRows(#"Added Index", each ([Index] <> 0 and [Index] <> 1)),
    #"0" = Table.SelectRows(#"Added Index", each ([Index] =0)),
    #"1" = Table.SelectRows(#"Added Index", each ([Index] =1)),
    Custom1 = #"1"&not0not1,
    #"Promoted Headers" = Table.PromoteHeaders(Custom1, [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"", type text}, {"31/12/2010", Int64.Type}, {"31/12/2011", Int64.Type}, {"31/12/2012", Int64.Type}, {"31/12/2010_1", Int64.Type}, {"31/12/2011_2", Int64.Type}, {"31/12/2012_3", Int64.Type}, {"1", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"1"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {""}, "Attribute", "Value"),
    #"Added Index1" = Table.AddIndexColumn(#"Unpivoted Other Columns", "Index", 1, 1),
    Custom2 = #"0"&not0not1,
    #"Promoted Headers1" = Table.PromoteHeaders(Custom2, [PromoteAllScalars=true]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers1",{{"Company", type text}, {"Sales", Int64.Type}, {"Sales_1", Int64.Type}, {"Sales_2", Int64.Type}, {"Profit", Int64.Type}, {"Profit_3", Int64.Type}, {"Profit_4", Int64.Type}, {"0", Int64.Type}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Changed Type2",{"0"}),
    #"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(#"Removed Columns1", {"Company"}, "Attribute", "Value"),
    #"Added Index2" = Table.AddIndexColumn(#"Unpivoted Other Columns1", "Index", 1, 1),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index"}, #"Added Index2", {"Index"}, "Added Index2", JoinKind.LeftOuter),
    #"Expanded Added Index2" = Table.ExpandTableColumn(#"Merged Queries", "Added Index2", {"Attribute", "Value"}, {"Attribute.1", "Value.1"}),
    #"Removed Columns2" = Table.RemoveColumns(#"Expanded Added Index2",{"Index"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns2", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1.1", "Attribute.2"}),
    #"Changed Type3" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1.1", type text}, {"Attribute.2", Int64.Type}}),
    #"Removed Columns3" = Table.RemoveColumns(#"Changed Type3",{"Attribute.2"}),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Removed Columns3", "Attribute.1", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1.1.1", "Attribute.1.2"}),
    #"Changed Type4" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Attribute.1.1.1", type text}, {"Attribute.1.2", Int64.Type}}),
    #"Removed Columns4" = Table.RemoveColumns(#"Changed Type4",{"Attribute.1.2"})
in
    #"Removed Columns4"
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

5 REPLIES 5
smpa01
Super User
Super User

@Anonymous  you can try this

 

let
    Source = Web.BrowserContents("https://community.powerbi.com/t5/Desktop/How-to-transpose-data-twice/m-p/949463#M454935"),
    #"Extracted Table From Html" = Html.Table(Source, {{"Column1", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(1), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(1)"}, {"Column2", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(2), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(2)"}, {"Column3", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(3), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(3)"}, {"Column4", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(4), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(4)"}, {"Column5", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(5), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(5)"}, {"Column6", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(6), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(6)"}, {"Column7", "DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR > :nth-child(7), DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR > :nth-child(7)"}}, [RowSelector="DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > TR, DIV[id='bodyDisplay'] > DIV.lia-message-body-content:nth-child(1) > TABLE:nth-child(5) > * > TR"]),
    #"Changed Type" = Table.TransformColumnTypes(#"Extracted Table From Html",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    not0not1 = Table.SelectRows(#"Added Index", each ([Index] <> 0 and [Index] <> 1)),
    #"0" = Table.SelectRows(#"Added Index", each ([Index] =0)),
    #"1" = Table.SelectRows(#"Added Index", each ([Index] =1)),
    Custom1 = #"1"&not0not1,
    #"Promoted Headers" = Table.PromoteHeaders(Custom1, [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"", type text}, {"31/12/2010", Int64.Type}, {"31/12/2011", Int64.Type}, {"31/12/2012", Int64.Type}, {"31/12/2010_1", Int64.Type}, {"31/12/2011_2", Int64.Type}, {"31/12/2012_3", Int64.Type}, {"1", Int64.Type}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"1"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {""}, "Attribute", "Value"),
    #"Added Index1" = Table.AddIndexColumn(#"Unpivoted Other Columns", "Index", 1, 1),
    Custom2 = #"0"&not0not1,
    #"Promoted Headers1" = Table.PromoteHeaders(Custom2, [PromoteAllScalars=true]),
    #"Changed Type2" = Table.TransformColumnTypes(#"Promoted Headers1",{{"Company", type text}, {"Sales", Int64.Type}, {"Sales_1", Int64.Type}, {"Sales_2", Int64.Type}, {"Profit", Int64.Type}, {"Profit_3", Int64.Type}, {"Profit_4", Int64.Type}, {"0", Int64.Type}}),
    #"Removed Columns1" = Table.RemoveColumns(#"Changed Type2",{"0"}),
    #"Unpivoted Other Columns1" = Table.UnpivotOtherColumns(#"Removed Columns1", {"Company"}, "Attribute", "Value"),
    #"Added Index2" = Table.AddIndexColumn(#"Unpivoted Other Columns1", "Index", 1, 1),
    #"Merged Queries" = Table.NestedJoin(#"Added Index1", {"Index"}, #"Added Index2", {"Index"}, "Added Index2", JoinKind.LeftOuter),
    #"Expanded Added Index2" = Table.ExpandTableColumn(#"Merged Queries", "Added Index2", {"Attribute", "Value"}, {"Attribute.1", "Value.1"}),
    #"Removed Columns2" = Table.RemoveColumns(#"Expanded Added Index2",{"Index"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns2", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1.1", "Attribute.2"}),
    #"Changed Type3" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1.1", type text}, {"Attribute.2", Int64.Type}}),
    #"Removed Columns3" = Table.RemoveColumns(#"Changed Type3",{"Attribute.2"}),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Removed Columns3", "Attribute.1", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Attribute.1.1.1", "Attribute.1.2"}),
    #"Changed Type4" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Attribute.1.1.1", type text}, {"Attribute.1.2", Int64.Type}}),
    #"Removed Columns4" = Table.RemoveColumns(#"Changed Type4",{"Attribute.1.2"})
in
    #"Removed Columns4"
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Anonymous
Not applicable

smpa's solution is essentially what I was going to suggest. To enter the M code, click the view tab in the power query editor and click "Advanced Editor". This is really the only way I can think of doing this without modifying the data pre pbi.

Anonymous
Not applicable

@Anonymous 

 

My solution to this issue is selecting the Company column then "unpivot other columns". This will give you something like this:

exm.PNG

Then you split the "Value" column before the first sales amount and duplicate the dates in a new column. 

Anonymous
Not applicable

Thanks for the response, hreid.

 

Could you please explain what is meant by splitting the column and duplicating the dates. 

I am new to powerbi and this might sound silly. 🙂

Anonymous
Not applicable

Well, the dates in the same column as prices are what causes most of the complexity. Is it possible to create a new column with just the dates in your dataset (not in pbi)? If it is, then you can follow the same steps and be done. If your dataset cannot be altered pre-powerbi its a little more challenging. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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