Forum Discussion
Left Join Between dates
- 6 years ago
Please open "Transform data", then check my queries below:
in "tableb" merge queries with "tablea" in "right outer" way,
then add custom columns and filter rows in "tableb",
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("hc5LCsAgDATQqxTXgvm1Fq8i3v8aNVaRiODCRcLLODk7DBgICK6Y5HZ+zm+f62MAV7y10VpSS7rb2CWXdZZmLUVJ9dqbBY7g+sOPacmlTd8ThZEqR9oKsPKFzrZk2z4dlw8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [b_SD = _t, b_ED = _t, b_Dataset = _t, b_RowsCopied = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"b_SD", type datetime}, {"b_ED", type datetime}, {"b_Dataset", Int64.Type}, {"b_RowsCopied", Int64.Type}}), #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"b_Dataset"}, Tablea, {"a_Dataset"}, "Tablea", JoinKind.RightOuter), #"Expanded Tablea" = Table.ExpandTableColumn(#"Merged Queries", "Tablea", {"a_eventDate", "a_Schedule", "a_Dataset"}, {"Tablea.a_eventDate", "Tablea.a_Schedule", "Tablea.a_Dataset"}), #"Added Custom" = Table.AddColumn(#"Expanded Tablea", "Custom", each if [b_Dataset]<>null and [Tablea.a_eventDate]>=[b_SD] and [Tablea.a_eventDate]<=[b_ED] then 1 else 0), #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Custom] = 1)) in #"Filtered Rows"create a new query1 which uses "tablea" data,
then in new query, merge query with "tableb" based on two columns "datatset" and "eventdate"
let Source = Tablea, #"Merged Queries" = Table.NestedJoin(Source, {"a_Dataset", "a_eventDate"}, Tableb, {"b_Dataset", "Tablea.a_eventDate"}, "Tableb", JoinKind.LeftOuter), #"Expanded Tableb" = Table.ExpandTableColumn(#"Merged Queries", "Tableb", {"b_SD", "b_ED", "b_Dataset", "b_RowsCopied"}, {"Tableb.b_SD", "Tableb.b_ED", "Tableb.b_Dataset", "Tableb.b_RowsCopied"}), #"Sorted Rows" = Table.Sort(#"Expanded Tableb",{{"a_eventDate", Order.Ascending}, {"a_Dataset", Order.Ascending}}) in #"Sorted Rows"Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Since I cannot read SQL out of the box, I'm not sure what you are doing exactly, but it sounds like you want an left-Anti-Join. Do a Merge between your tables and tell it to only return the rows in the first or second table, depending on which way you are going.
If that isn't what you need, provide sample data in a table format for both tables and the expected result using the "how to provide sample data" instructions below.
How to get good help fast. Help us help you.
How to Get Your Question Answered Quickly
How to provide sample data in the Power BI Forum