Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Combine two tables by Column

Hi everyone,   I have two tables with a different amount of rows. I am wondering if it is possible to combine them together without interfering with the data in either? Below are examples of the tw...
  • FrankAT's avatar
    5 years ago

    Hi Anonymous ,

    use Power Query like this:

     

     

    // Table1
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WciwoyElV0lEKcAYShkqxOtFKwYm5xaV56SDBjPw8kKSpUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer Quote" = _t, #"Product Quote" = _t, #"Quantity Quote" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer Quote", type text}, {"Product Quote", type text}, {"Quantity Quote", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type)
    in
        #"Added Index"
    
    // Table2
    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nTyVdJRCnAGEsZKsTrRSm6JyalJ+fnZINGM/LxUIG2oFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer Sales" = _t, #"Sales Product" = _t, #"Quantity Sales" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer Sales", type text}, {"Sales Product", type text}, {"Quantity Sales", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type)
    in
        #"Added Index"
    
    // MergedTables
    let
        Source = Table.NestedJoin(Table1, {"Index"}, Table2, {"Index"}, "Table2", JoinKind.LeftOuter),
        #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"Customer Sales", "Sales Product", "Quantity Sales"}, {"Customer Sales", "Sales Product", "Quantity Sales"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Table2",{"Index"})
    in
        #"Removed Columns"

    With kind regards from the town where the legend of the 'Pied Piper of Hamelin' is at home
    FrankAT (Proud to be a Datanaut)