Forum Discussion

alagator28's avatar
alagator28
Helper II
1 year ago
Solved

Combine values from multiple rows

Hello, how can I combine values from multiple rows based on conditions? I need to extract values from every row where ID and Group are both the same as the other rows. Here is a simplified view of my...
  • lbendlin's avatar
    1 year ago
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSixOSVPSUTIE4pDM5OzUEhDHyBhI5pXm5MCoWB0UlUGpicX5eQg1SanJiaXFqThUOxelJpakpqAZqaNkZGBkomtoAEQYOnLyi/FpMARrqKisAgoaI7vc1NQUi8sRCtEdXp5RqZCXX4JdMR53G+saGAKRUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Group = _t, Name = _t, Ticket = _t, Reason = _t, DateTime = _t]),
        #"Replaced Value" = Table.ReplaceValue(Source,"null",null,Replacer.ReplaceValue,{"Ticket", "Reason", "DateTime"}),
        trans = (tbl)=>
           let
               #"Removed Other Columns" = Table.SelectColumns(Table.AddColumn(tbl, "Value", each List.RemoveNulls({[Ticket],[Reason],[DateTime]}){0}),{"Name", "Value"})
           in 
               Table.Pivot(#"Removed Other Columns", List.Distinct(#"Removed Other Columns"[Name]), "Name", "Value"),
        #"Grouped Rows" = Table.Group(#"Replaced Value", {"ID", "Group"}, {{"Rows", each trans(_), type table [ Ticket=nullable text, Reason=nullable text, Created=nullable text, Closed=nullable text]}}),
        #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Ticket", "Reason", "Created", "Closed"}, {"Ticket", "Reason", "Created", "Closed"})
    in
        #"Expanded Rows"

    How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

  • Omid_Motamedise's avatar
    1 year ago

    Consider the next data in power query

     

     

    Select the columns Ticket, Reason, and Date time then right click on one of them and pick unpivot columns to reach next image.

     

     

     

    then on the value column filter non null values and also remove column Attribute to reach the next image

     

     

    now select Name column and from transform tab pick pivot column and make the next setting to solve the problem.

     

     

     

    the result would be like the next image

     

     

     

    here you can find the whole code

     

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSixOSVPSUTIE4pDM5OzUEhDHyBhI5pXm5MCoWB0UlUGpicX5eQg1SanJiaXFqThUOxelJpakpqAZqaNkZGBkomtoAEQYOnLyi/FpMARrqKisAgoaI7vc1NQUi8sRCtEdXp5RqZCXX4JdMR53G+saGAKRUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Group = _t, Name = _t, Ticket = _t, Reason = _t, DateTime = _t]),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {"ID", "Group", "Name"}, "Attribute", "Value"),
        #"Filtered Rows" = Table.SelectRows(#"Unpivoted Columns", each ([Value] <> "null")),
        #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Attribute"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Name]), "Name", "Value")
    in
        #"Pivoted Column"

     

    If this answer helped resolve your issue, please consider marking it as the accepted answer. And if you found my response helpful, I'd appreciate it if you could give me kudos. 

    Thank you!

  • lbendlin's avatar
    lbendlin
    1 year ago

    Looks like my original code produces that result

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSixOSVPSUTIE4pDM5OzUEhDHyBhI5pXm5MCoWB0UlUGpicX5eQg1SanJiaXFqThUOxelJpakpqAZqaNkZGBkomtoAEQKBoZWBgZAhKE1J7+YdJ1GyJ4xNTXF7RkjLJ4pz6hUyMsvwaGaTM8YEeEZI1SdFZVV6BGD3S8IhUR4BaEYj0+MdQ0MgQibe4gIW4RCot1DTsgi6cQZsMYYARsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Group = _t, Name = _t, Ticket = _t, Reason = _t, DateTime = _t]),
        #"Replaced Value" = Table.ReplaceValue(Source,"null",null,Replacer.ReplaceValue,{"Ticket", "Reason", "DateTime"}),
        trans = (tbl)=>
           let
               #"Removed Other Columns" = Table.SelectColumns(Table.AddColumn(tbl, "Value", each List.RemoveNulls({[Ticket],[Reason],[DateTime]}){0}),{"Name", "Value"})
           in 
               Table.Pivot(#"Removed Other Columns", List.Distinct(#"Removed Other Columns"[Name]), "Name", "Value"),
        #"Grouped Rows" = Table.Group(#"Replaced Value", {"ID", "Group"}, {{"Rows", each trans(_), type table [ Ticket=nullable text, Reason=nullable text, Created=nullable text, Closed=nullable text]}}),
        #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Ticket", "Reason", "Created", "Closed"}, {"Ticket", "Reason", "Created", "Closed"})
    in
        #"Expanded Rows"