合并日期匹配的查询,如果找不到匹配项则保留 null

Merge queries matching on dates and leave null if no match found

下面有两个table(例子):

Table SFID

Sales Force ID Type Name Assistant From To
123 ABC Store A Ben 01/04/2020 30/04/2020
123 ABC Store A Jen 01/05/2020 31/05/2020
123 ABC Store A Ben 01/06/2020 21/06/2020
126 DEF Store B Tim 01/04/2020 30/04/2020
126 DEF Store B Tim 01/04/2020 null

Table Activity

Transaction ID Date Sales Force ID
1 03/05/2020 123
2 03/06/2020 200
3 01/01/2021 123
4 02/01/2021 126

我希望我的最终结果是

Transaction ID Date Sales Force ID Type Name Assistant
1 03/05/2020 123 ABC Store A Jen
2 03/06/2020 200 null null null
3 01/01/2021 123 null null null
4 02/01/2021 126 DEF Store B Tim

为此,最好的解决方案是 with some modifications (allow both To and From to be null). However, only the row on transaction ID 2 disappears because that Sales Force had already had assistant entries (they get wiped out on the filtered row step). I also tried the solution presented in 中发布的解决方案,但加载需要很长时间。

我想知道是否有一种方法可以保证所有交易都出现,而不必在没有助手的情况下向 table SFID 引入行,并且不会使查询变得非常慢。

这是我的代码:

    let
    Source = Source,
    #"Merged Queries" = Table.NestedJoin(Source,{"Sales Force ID"},SFID,{"SFID"},"SFID",JoinKind.LeftOuter),
    #"Expanded SFID" = Table.ExpandTableColumn(#"Merged Queries", "SFID", {"Type", "Name", "Assistant", "From", "To"}, {"Type", "Name", "Assistant", "From", "To"}),
    #"Changed Type" = Table.TransformColumnTypes(#"SFID",{{"Date", type date}, {"From", type date}, {"To", type date}}),
    FilteredRows = Table.SelectRows(#"Changed Type", each ([Date] >= [From] and [Date] <= [To]) or ([Date] >= [From] and [To] = null)or ([From] = null and [To] = null)),
    #"Removed Columns" = Table.RemoveColumns(FilteredRows,{"From", "To"})
in
    #"Removed Columns"

所以在将近 2 周没有任何回应和不合理的降级之后,我找到了解决方案!

我创建了一个查询,基本上可以使用上述代码生成此 table。

Transaction ID Date Assistant
1 01/01/2021 Jen
4 02/01/2021 Tim
let
    Source = Source,
    #"Merged Queries" = Table.NestedJoin(Source,{"Sales Force ID"},SFID,{"SFID"},"SFID",JoinKind.LeftOuter),
    #"Expanded SFID" = Table.ExpandTableColumn(#"Merged Queries", "SFID", {"Type", "Name", "Assistant", "From", "To"}, {"Type", "Name", "Assistant", "From", "To"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Expanded SFID",{{"Date", type date}, {"From", type date}, {"To", type date}}),
    FilteredRows = Table.SelectRows(#"Changed Type", each ([Date] >= [From] and [Date] <= [To]) or ([Date] >= [From] and [To] = null)),
    #"Removed Columns" = Table.RemoveColumns(FilteredRows,{"From", "To"})
in
    #"Removed Columns"

并在我的初始版本 Table Activity

中左加入它

我将 Type 和 Name 类型的信息保存在一个单独的查询中(因为它们不会改变),然后在 Table Activity 上再次左连接它.

最终查询如下所示:

let
    Source = Source,
    #"Merged Queries1" = Table.NestedJoin(Source,{"Sales Force Code"},Info,{"SFID"},"Info",JoinKind.LeftOuter),
    #"Expanded Info" = Table.ExpandTableColumn(#"Merged Queries1", "Info", {"Type", "Name"}, {"Type", "Name"}),
    #"Merged Queries" = Table.NestedJoin(#"Expanded Info",{"ID"},IDvsAssistant,{"ID"},"IDvsAssistant",JoinKind.LeftOuter),
    #"Expanded IDvsAssistant" = Table.ExpandTableColumn(#"Merged Queries", "IDvsAssistant", {"Assistant"}, {"Assistant"})
in
    #"Expanded IDvsAssistant"