Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
EtienneT
New Member

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: 

IDData_1Data_2Date_Field
ABC10015007/23/2024
ABC11016007/24/2024
ABC130N/A07/26/2024

 

After a bunch of PowerQuery steps, I keep only the latest value of the data to make it look like that : 

 

IDData_1Data_2
ABC130160

 

I use some filters, some conditional formulas and also a duplicate removal steps. So at the very last step of my query, I have only one version of each ID in the table, which the latest version of data, which is exactly what I want. 

 

Now comes my second table, that has the same ID on its records, where I need to perform the merge to get the Data_1 and Data_2 fields. 

 

For that I use the "Merge Query" Left Outer function and select both IDs in both table to perform the merge. I have exact matching and I would expect the Table 2 to look like that : 

 

Other Data IDTable1.Data_1Table1.Data_2
XXXXXABC130160

 

Turns out it does not at all and it keeps giving me wrong data from the Table1, like that : 

 

Other Data IDTable1.Data_1Table1.Data_2
XXXXXABC100150

 

I do not understand this behaviour as I would expect the merge to be performed on the latest step of the Table1 query, including the removal of old data. It looks like it is returning me some rows that are deleted in my Table1. 

 

Any hints on how to solve this ? 

 

Thanks a lot in avance! 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

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

vheqmsft_0-1722592431858.png

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

vheqmsft_1-1722592472037.png

 

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

 

View solution in original post

5 REPLIES 5
Anonymous
Not applicable

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

vheqmsft_0-1722592431858.png

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

vheqmsft_1-1722592472037.png

 

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

 

PwerQueryKees
Super User
Super User

#"Merge Table1" = Table.NestedJoin(#"Previous step", {"ID"}, #"Name of the final result",  {"ID"}, "Table 1", JoinKind.LeftOuter)

 

PwerQueryKees
Super User
Super User

Hard to say without seeing your code.

Everything is in one Query? Then you should refer to the name of the step showing the final result you showed in your post.

PwerQueryKees
Super User
Super User

You don't refer to the final query results.

Show your m code...

Hey, thanks for the answer 

 

I cannot post the code here for privacy policy but it looks like that : 

 

 #"Merge Table1" = Table.NestedJoin(#"Previous step", {"ID"}, #"Table1",  {"ID"}, "Table 1", JoinKind.LeftOuter)

 

How can I refer exactly to the final query result ? 

 

Thanks

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.