Forum Discussion

skmaddur's avatar
skmaddur
Copper Contributor
Feb 13, 2020

sheet header data on multiple rows - how to merge the 2 header rows

a1b1c1d1e1f1    
100patx3/22/2019medicine3/23/2019ins    
g1h1j1       
authroizednana       

 

The above is the structure of my excel data export from a 3rd party application. All bold a1-j1 are header columns with data on the 2nd rows. How do I merge these 2 header rows

2 Replies

  • macrordinary's avatar
    macrordinary
    Brass Contributor

    skmaddur Using Power Query:

     

    First, the data needs to be in a table. I've named the table Table1.

    What you need to do is create 1 copy of the data for the header rows (create an index and filter on odd numbers), and 1 copy for data (create an index and filter on even numbers).

    Then, once you have 2 tables with the values you need, create indexes for them - this will provide the criteria for the data merge. Merge the 2 tables on the index, and you'll have a list from A1 to J1 with your data!

     

    Query 1 - The header rows

    let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Demoted Headers" = Table.DemoteHeaders(Source),
    #"Changed Type" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type any}, {"Column2", type text}, {"Column3", type any}, {"Column4", type text}, {"Column5", type any}, {"Column6", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Index] = 1 or [Index] = 3)),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Filtered Rows", {"Index"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Index", "Attribute"}),
    #"Added Index1" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1)
    in
    #"Added Index1"

     

    Query 2 - The data rows

    let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Demoted Headers" = Table.DemoteHeaders(Source),
    #"Changed Type" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type any}, {"Column2", type text}, {"Column3", type any}, {"Column4", type text}, {"Column5", type any}, {"Column6", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1),
    #"Filtered Rows" = Table.SelectRows(#"Added Index", each ([Index] = 2 or [Index] = 4)),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Filtered Rows", {"Index"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Index", "Attribute"}),
    #"Added Index1" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1)
    in
    #"Added Index1"

     

    Query 3 - The data merge

    let
    Source = Table.NestedJoin(Headers, {"Index"}, Values, {"Index"}, "Values", JoinKind.Inner),
    #"Expanded Values" = Table.ExpandTableColumn(Source, "Values", {"Value", "Index"}, {"Value.1", "Index.1"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Values",{"Index", "Index.1"})
    in
    #"Removed Columns"

    • skmaddur's avatar
      skmaddur
      Copper Contributor

      macrordinary 

       

      a1b1c1d1e1f1
      100patx3/22/2019medicine3/23/2019ins1
      g1h1j1   
      authroizednana   
      100paty4/22/2019eyecare4/23/2019ins2
      g1h1j1   
      not authnana   
      100patz3/25/2019legcare3/26/2019ins3
      100patc3/26/2019handcare3/27/2019ins4

      Thank you for your solution. Even though i don't have the powerquery add-in, i understood your solution from your description. I could filter like you said to separate the odd and even rows of header and data. Upon doing so, i found the above variation in the data. Please note the last 2 lines in the table example above without the 2nd header row (g1,h1,j1).

       

       

Resources