如何将此 COUNTIFS 转换为 Power Query 以获得更好的性能?

How to convert this COUNTIFS into Power Query for better performance purpose?

我在 Excel 的 table 中使用它,但每次计算都需要很长时间 - 毫无疑问。

=IF(COUNTIFS(T:T;T9371;R:R;">="&EOMONTH('Sheet1'!$B;-1)+1;R:R;"<="&EOMONTH('Sheet1'!$B;0))>0;1;0)

我们的想法是找到该客户在当月出现的次数,如果有则标记为 1,如果没有则标记为 0。也找出上个月买过的...

我们如何在 Power Query 中设置它?

这是数据示例:

CustomerID Customer Date Item ThisMonth LastMonth
T45678 ABC 09/15/21 Product A 1 0
T45678 ABC 09/15/21 Product B 1 0
T123645 BGT 08/10/21 Product A 0 1

谢谢!

我将如何在 PQ 中执行此操作的示例。

请阅读代码中的注释并探索应用步骤以了解算法。

M代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"CustomerID", type text}, {"Customer", type text}, {
            "Date", type date}, {"Item", type text}}),

    //Normalize all dates to start of month for grouping
    //date type => datetime so can be used in grouping aggregation below
    #"Added Custom" = Table.AddColumn(#"Changed Type", "BOM", 
        each Date.StartOfMonth([Date]), type datetime),

    //group by Customer and BOM
    //  then return 1 or 0 if in current or previous month
    #"Grouped Rows" = Table.Group(#"Added Custom", {"CustomerID", "BOM"}, {
         {"all", each _, type table 
            [CustomerID=nullable text, 
            Customer=nullable text, 
            Date=nullable date, 
            Item=nullable text,
            BOM=date]},
        {"This Month", each if Date.IsInCurrentMonth([BOM]{0}) then 1 else 0, Int64.Type}, 
        {"Last Month", each if Date.IsInPreviousMonth([BOM]{0}) then 1 else 0, Int64.Type}
        }),
        
    //Expand the table to restore ungrouped listings
    #"Expanded all" = Table.ExpandTableColumn(#"Grouped Rows", "all", 
        {"Customer", "Date", "Item"}, {"Customer", "Date", "Item"}),

    //Don't need BOM column anymore
    #"Removed Columns1" = Table.RemoveColumns(#"Expanded all",{"BOM"})
in
    #"Removed Columns1"

原始数据

结果