Excel 如果日期在电源查询中介于两个日期之间,返回值?

Excel 如果日期在电源查询中介于两个日期之间,返回值?,excel,powerquery,Excel,Powerquery,员工有一定的时间来完成一项行动并获得结果。时间框架可能从一天过渡到下一天。他们可能必须每天完成一次以上的操作。我需要将action datetime与时间框架相匹配,并保留源表中的所有行 源表: 员工ID |开始日期时间|结束日期时间 123 | 9/30晚上10:00 | 10/1上午1:00 456 | 10/1上午7:00 | 10/1上午10:00 456 | 10/1下午5:00 | 10/1下午10:00 查找表: 员工ID |操作日期时间|结果 123 | 9/30晚上11:00

员工有一定的时间来完成一项行动并获得结果。时间框架可能从一天过渡到下一天。他们可能必须每天完成一次以上的操作。我需要将action datetime与时间框架相匹配,并保留源表中的所有行

源表:

员工ID |开始日期时间|结束日期时间

123 | 9/30晚上10:00 | 10/1上午1:00

456 | 10/1上午7:00 | 10/1上午10:00

456 | 10/1下午5:00 | 10/1下午10:00

查找表:

员工ID |操作日期时间|结果

123 | 9/30晚上11:00 | A

456 | 10/1上午9:30 | B

456 | 10/11:15下午| B

最终结果:

员工ID |开始日期时间|结束日期时间|操作日期时间|结果

123 | 9/30晚上10:00 | 10/1上午1:00 | 9/30晚上11:00 | A

456 | 10/1上午7:00 | 10/1上午10:00 | 10/1上午9:30 | B

456 | 10/1下午5:00 | 10/1下午10:00 |零|零

我尝试了以下代码,但速度非常慢:

let
    Source = #"SourceTable",
    LBuffered = Table.Buffer(LookupTable),
    #"Added Custom" = Table.AddColumn(Source, "LookupTable", (#"SourceTable") => Table.SelectRows(LBuffered, each #"SourceTable"[Empl ID] = [Empl ID] and #"SourceTable"[Compliance StartDateTime] <= [Action DateTime] and #"SourceTable"[End DateTime] >= [Action DateTime])),
    #"Expanded LookupTable" = Table.ExpandTableColumn(#"Added Custom", "LookupTable", {"Action DateTime",  "Result"}, {"Action DateTime", "Result"})

(other code)
let
Source=#“SourceTable”,
LBuffered=表缓冲区(可查找),
#“添加的自定义”=Table.AddColumn(Source,“LookupTable”(#“SourceTable”)=>Table.SelectRows(LBuffered,每个#“SourceTable”[emp ID]=[emp ID]和#“SourceTable”[Compliance StartDateTime]=[Action DateTime]),
#“Expanded LookupTable”=Table.ExpandTableColumn(#“添加了自定义”、“LookupTable”、“Action DateTime”、“Result”}、{“Action DateTime”、“Result”})
(其他守则)

有没有其他我没有想到的方法来加速这个过程?

我想加入可能会更快。在我的示例中,DateTime列是文本,因此我将它们转换为DateTime。如果您的源有DateTime列,则不必执行此步骤以节省时间

我每15分钟根据“员工ID”为所有相关日期创建一个列表。 (在查找表示例中,时间为15分钟。如果需要,您可以将M代码更改为每分钟一次)

查找:

/*Lookup Table*/

let
    Quelle = Excel.CurrentWorkbook(){[Name="Lookup_Table"]}[Content],
    #"Geänderter Typ" = Table.TransformColumnTypes(Quelle,{{"Empl ID ", Int64.Type}, {"Action DateTime", type text}, {"Result", type text}}),
    Custom_ActionDateTimeNew = Table.AddColumn(#"Geänderter Typ", "Action DateTimeNew", each 
        #datetime(Date.Year(DateTime.LocalNow()),
                    Number.From(Text.BeforeDelimiter([#"Action DateTime"], "/")), 
                    Number.From(Text.BetweenDelimiters([#"Action DateTime"], "/", " ")), 
                    Time.Hour(Time.From(Text.AfterDelimiter([#"Action DateTime"], " "))), 
                    Time.Minute(Time.From(Text.AfterDelimiter([#"Action DateTime"], " "))), 0))
in
    Custom_ActionDateTimeNew
结果

/*Result Table*/
let
    Quelle = Table.NestedJoin(Source_Table, {"Empl ID ", "List DateTime"}, Lookup_Table, {"Empl ID ", "Action DateTimeNew"}, "Lookup_Table", JoinKind.LeftOuter),
    Expand_Lookup_Table = Table.ExpandTableColumn(Quelle, "Lookup_Table", {"Action DateTime", "Result"}, {"Action DateTime", "Result"}),
    RemoveOtherColumns = Table.SelectColumns(Expand_Lookup_Table,{"Empl ID ", "Start DateTime ", "End DateTime", "Action DateTime", "Result"}),
    #"Gruppierte Zeilen" = Table.Group(RemoveOtherColumns, {"Empl ID ", "Start DateTime ", "End DateTime"}, {{"Action DateTime", each List.Max([Action DateTime]), type nullable text}, {"Result", each List.Max([Result]), type nullable text}})
in
    #"Gruppierte Zeilen"

如果没有完整的图片(例如有多少条记录,表存储在哪里,源表和查找表发生了什么转换),很难说清楚@ricardiodiaz“源表”和“查找表”都在各自的查询中,我认为它们可以组合在“最终结果”查询中。每天可能有数千条记录。我正在对27000条源表记录的一周进行测试。我通过一个简单的自定义公式(轮班时间+或-2小时)在源表上创建开始和结束时间。它们来自哪里?sql、csv、excel?您很可能还有其他转换。就我而言,将查询应用到45k条记录只需不到1秒的时间。也许最好回顾每一步,看看瓶颈在哪里。@RicardoDiaz他们来自excel和csv。我将每天的CSV文件合并到一个查询中,如果这有区别的话。我所有的能力查询知识都是自学的,所以有时候很难知道我不知道什么。
/*Result Table*/
let
    Quelle = Table.NestedJoin(Source_Table, {"Empl ID ", "List DateTime"}, Lookup_Table, {"Empl ID ", "Action DateTimeNew"}, "Lookup_Table", JoinKind.LeftOuter),
    Expand_Lookup_Table = Table.ExpandTableColumn(Quelle, "Lookup_Table", {"Action DateTime", "Result"}, {"Action DateTime", "Result"}),
    RemoveOtherColumns = Table.SelectColumns(Expand_Lookup_Table,{"Empl ID ", "Start DateTime ", "End DateTime", "Action DateTime", "Result"}),
    #"Gruppierte Zeilen" = Table.Group(RemoveOtherColumns, {"Empl ID ", "Start DateTime ", "End DateTime"}, {{"Action DateTime", each List.Max([Action DateTime]), type nullable text}, {"Result", each List.Max([Result]), type nullable text}})
in
    #"Gruppierte Zeilen"