Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Move row data to existing column

Perhaps a basic question, but I can't find a way to achieve this.

I have the following data table:

 2019       2020 
 1 2 3 4 1 
LocationSalesCOSSalesCOSSalesCOSSalesCOSSalesCOS
X10987109871311
Y1210121011912101412
Z16121613161215101411

 

I would like to adjust the table in such a way that it shown as follows:

LocationYearPeriodSalesCOS
X201901109
X20190287
X201903109
X20190487
X2020011311
Y2019011210
Y2019021210
Y201903119
y2019041210
Y2020011412
Z2019011612
Z2019021613
Z2019031612
Z2019041510
Z2020011411

 

How can I do this in PowerBI?

 

  • Hi Anonymous ,

     

    Try this new code:

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nU/BDoIwDP0VwpkDHTDgqJh4MdGEi7pwIOiBxDiD8P+2o5sjUQ8ma1772tfXKRUGYRTiEzGUCMGXMBMipqyJlKOBUTAmjOmiT4qd7tqx13ckDoO+TN0YbAc9PbCu29v1iVjt678rsjhiviJTOpM+U2DkHwm6E8Cp1ny64NMtJl4990lxsj7C7vYyYCuPSufSSisMyYvNAF2VsUvBjdy5nVkC0m2V7gtvLlvagdNufJsfSWlcm+YF", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t, Column10 = _t, Column11 = _t, Column12 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"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 text}, {"Column10", type text}, {"Column11", type text}, {"Column12", type text}}),
    #"Transposed Table" = Table.Transpose(#"Changed Type"),
    #"Trimmed Text" = Table.TransformColumns(#"Transposed Table",{{"Column1", Text.Trim, type text}, {"Column2", Text.Trim, type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Trimmed Text","",null,Replacer.ReplaceValue,{"Column1", "Column2"}),
    #"Filled Down" = Table.FillDown(#"Replaced Value",{"Column1", "Column2"}),
    Table1 = Table.RemoveColumns(Table.FirstN(#"Filled Down", 2), {"Column1", "Column2"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Column2] <> null)),
    #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Column1", "Column2"}, {{"Rows", each Table.PromoteHeaders(Table.Transpose(Table.Combine({
    Table.RemoveColumns(_, {"Column1", "Column2"}),
    Table1 }))), type table [Sales=text, COS=text, Location=text, #"Product Group"=text]}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Sales", "COS", "Location", "Product Group"}, {"Sales", "COS", "Location", "Product Group"}),
    #"Renamed Columns" = Table.RenameColumns(#"Expanded Rows",{{"Column1", "Year"}, {"Column2", "Period"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Year", Int64.Type}, {"Period", Int64.Type}, {"Sales", Int64.Type}, {"COS", Int64.Type}, {"Location", type text}, {"Product Group", type text}})
    in
    #"Changed Type1"

     

5 Replies

  • camargos88's avatar
    camargos88
    Icon for Community Champion rankCommunity Champion

    Hi Anonymous ,

     

    Go to Query Editor and create a blank query, paste this m code on the Advanced Editor:

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtFTSUVLAg4HIyMDIAMyL1YkGixlC5YygtDGUNoHShnDVPvnJiSWZ+XlAgeDEnNRiIO3sH0w2D2RkBMgCkHtALrcAYnOsAiA3GRqCdUSCmEYwVUgsQ6gmJCETCBekLQrENINLm8FNRYiZouoDWhcLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t, Column10 = _t, Column11 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"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 text}, {"Column10", type text}, {"Column11", type text}}),
    #"Transposed Table" = Table.Transpose(#"Changed Type"),
    #"Trimmed Text" = Table.TransformColumns(#"Transposed Table",{{"Column1", Text.Trim, type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Trimmed Text","",null,Replacer.ReplaceValue,{"Column1"}),
    #"Filled Down" = Table.FillDown(#"Replaced Value",{"Column1", "Column2"}),
    #"Trimmed Text1" = Table.TransformColumns(#"Filled Down",{{"Column2", Text.Trim, type text}}),
    #"Replaced Value1" = Table.ReplaceValue(#"Trimmed Text1","",null,Replacer.ReplaceValue,{"Column2"}),
    #"Filled Down1" = Table.FillDown(#"Replaced Value1",{"Column2"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Filled Down1", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"2019", Int64.Type}, {"Column2", Int64.Type}, {"Location", type text}, {"X", Int64.Type}, {"Y", Int64.Type}, {"Z", Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"2019", "Year"}, {"Column2", "Period"}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Renamed Columns", {"Year", "Period", "Location"}, "Attribute", "Value"),
    #"Pivoted Column" = Table.Pivot(#"Unpivoted Other Columns", List.Distinct(#"Unpivoted Other Columns"[Location]), "Location", "Value"),
    #"Renamed Columns1" = Table.RenameColumns(#"Pivoted Column",{{"Attribute", "Location"}})
    in
    #"Renamed Columns1"

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks, that helped a lot! My data table does however has a bit more data. Is the same possible with the following table?

       

        2019       2020 
        1 2 3 4 1 
      LocationProduct GroupSalesCOSSalesCOSSalesCOSSalesCOSSalesCOS
      XA10987109871311
      XB3242434232
      YA1210121011912101412
      YC64108528472
      ZC16121613161215101411
      ZD10810810810897

       

      LocationProduct GroupYearPeriodSalesCOS
      XA20191109
      XA2019287
      XA20193109
      XA2019487
      XA202011311
      XB2019132
      XB2019242
      etc.     
      • camargos88's avatar
        camargos88
        Icon for Community Champion rankCommunity Champion

        Hi Anonymous ,

         

        Try this one:

         

        let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nU/BDoIwDP0VwpkDHTDgqJh4MdGEi7pwIOiBxDiD8P+2o5sjUQ8ma1772tfXKRUGYRTiEzGUCMGXMBMipqyJlKOBUTAmjOmiT4qd7tqx13ckDoO+TN0YbAc9PbCu29v1iVjt678rsjhiviJTOpM+U2DkHwm6E8Cp1ny64NMtJl4990lxsj7C7vYyYCuPSufSSisMyYvNAF2VsUvBjdy5nVkC0m2V7gtvLlvagdNufJsfSWlcm+YF", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t, Column10 = _t, Column11 = _t, Column12 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"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 text}, {"Column10", type text}, {"Column11", type text}, {"Column12", type text}}),
        #"Transposed Table" = Table.Transpose(#"Changed Type"),
        #"Trimmed Text" = Table.TransformColumns(#"Transposed Table",{{"Column1", Text.Trim, type text}}),
        #"Replaced Value" = Table.ReplaceValue(#"Trimmed Text","",null,Replacer.ReplaceValue,{"Column1"}),
        #"Filled Down" = Table.FillDown(#"Replaced Value",{"Column1", "Column2"}),
        #"Trimmed Text1" = Table.TransformColumns(#"Filled Down",{{"Column2", Text.Trim, type text}}),
        #"Replaced Value1" = Table.ReplaceValue(#"Trimmed Text1","",null,Replacer.ReplaceValue,{"Column2"}),
        #"Filled Down1" = Table.FillDown(#"Replaced Value1",{"Column2"}),
        #"Added Custom" = Table.AddColumn(#"Filled Down1", "Custom", each let _year = [Column1], _month = [Column2] in
        Table.PromoteHeaders(
        Table.Transpose(
        Table.RemoveColumns(Table.SelectRows(#"Filled Down1", each
        ([Column1] = _year and [Column2] = _month) or
        [Column2] = null), {"Column1", "Column2"})))),
        #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Column2] <> null)),
        #"Removed Other Columns" = Table.SelectColumns(#"Filtered Rows",{"Column1", "Column2", "Custom"}),
        #"Removed Duplicates" = Table.Distinct(#"Removed Other Columns", {"Column1", "Column2"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Duplicates", "Custom", {"Location", "Product Group", "Sales", "COS"}, {"Location", "Product Group", "Sales", "COS"}),
        #"Renamed Columns" = Table.RenameColumns(#"Expanded Custom",{{"Column1", "Year"}, {"Column2", "Period"}}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Year", Int64.Type}, {"Period", Int64.Type}, {"Location", type text}, {"Product Group", type text}, {"Sales", Int64.Type}, {"COS", Int64.Type}}),
        #"Filtered Rows1" = Table.SelectRows(#"Changed Type1", each ([Location] = "X"))
        in
        #"Filtered Rows1"