Forum Discussion

gauravg3's avatar
gauravg3
Helper I
11 months ago
Solved

Merge row cells

Can someone pls advise if its possible to merge data from cells in 2 rows. In the below screenshot, I want the data from columns B-E against the name and add the date in a separate column. In the sou...
  • collinsg's avatar
    11 months ago

    Good Day gauravg3,

    There will be more elegant ways to do this but here's an approach which works...

    1. Load the table into Power Query.
    2. Add an index column (starting at 0).
    3. Add a conditional column called "Name" with the logic "if the index in the current row is odd populate "Name" with the value of the previous row of the "Start Date" column (which will be a name), otherwise set it as null.
    4. Tidy up by
      1. Removing the alternate rows you don't need.
      2. Removing the index column.
      3. Extract the date from the "Start Date"
      4. Set the correct type for the "Name" column.
      5. Rename columns as you wish (in my example I used "Name" for the name and "Start Date" for the date.
      6. Reorder columns if you wish.

    The result looks as follows

    The code looks as follows

    let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Start Date", type any}, {"Duration A", type number}, {"Duration B", type number}, {"Duraction C", type number}, {"Duration D", type number}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Name", each if Number.IsOdd([Index]) then #"Changed Type"[Start Date]{[Index]-1} else null),
    #"Removed Alternate Rows" = Table.AlternateRows(#"Added Custom",0,1,1),
    #"Removed Other Columns" = Table.SelectColumns(#"Removed Alternate Rows",{"Start Date", "Duration A", "Duration B", "Duraction C", "Duration D", "Name"}),
    #"Extracted Date" = Table.TransformColumns(#"Removed Other Columns",{{"Start Date", DateTime.Date, type date}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Extracted Date",{{"Name", type text}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Changed Type1",{"Name", "Duration A", "Duration B", "Duraction C", "Duration D", "Start Date"})
    in
    #"Reordered Columns"

    I've attached an example Excel file.

    Hope this helps