Forum Discussion

EtienneT's avatar
EtienneT
New Member
2 years ago
Solved

Merged queries with deleted duplicates and filtered rows

Hi everyone,    I'm having issues with merging two queries together.    My Table 1 looks like that:  ID Data_1 Data_2 Date_Field ABC 100 150 07/23/2024 ABC 110 160 07/24/2024 ...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi EtienneT ,
    Thanks for PwerQueryKees reply.
    Not sure about the structure of your second table based on your description, here's my test data
    Table 2

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRyVtJRMjQwAJGmINLAXN/IWN/IwMhEKVYHrsAQrMAMpsAEQ4ExSMpP3xGqwAyqIBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Data_1 = _t, Data_2 = _t, Date_Field = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Data_1", Int64.Type}, {"Data_2", type text}, {"Date_Field", type date}}),
        SortedTable = Table.Sort(Source,{{"ID", Order.Ascending}, {"Date_Field", Order.Descending}}),
        AddIndex = Table.AddIndexColumn(SortedTable, "Index", 1, 1, Int64.Type),
        GroupedTable = Table.Group(AddIndex, {"ID"}, {
            {"LatestData", each 
                let
                    LatestData1 = List.First([Data_1]),
                    LatestData2 = if List.First([Data_2]) <> "N/A" then List.First([Data_2]) else List.First(List.Skip([Data_2], 1))
                in
                    [Data_1 = LatestData1, Data_2 = LatestData2]
            }
        }),
        ExpandedTable = Table.ExpandRecordColumn(GroupedTable, "LatestData", {"Data_1", "Data_2"}),
        #"Merged Queries" = Table.NestedJoin(ExpandedTable, {"ID"}, #"Table (2)", {"ID"}, "Table (2)", JoinKind.LeftOuter),
        #"Expanded Table (2)" = Table.ExpandTableColumn(#"Merged Queries", "Table (2)", {"Other data"}, {"Table (2).Other data"})
    in
        #"Expanded Table (2)"

    Final output

     

    Best regards,
    Albert He

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly