【问题标题】:How to convert this COUNTIFS into Power Query for better performance purpose?如何将此 COUNTIFS 转换为 Power Query 以获得更好的性能目的?
【发布时间】:2021-09-10 11:25:06
【问题描述】:

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

=IF(COUNTIFS(T:T;T9371;R:R;">="&EOMONTH('Sheet1'!$B$2;-1)+1;R:R;"<="&EOMONTH('Sheet1'!$B$3;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

谢谢!

【问题讨论】:

  • 编辑您的问题,以提供可复制/粘贴到 Excel 工作表中的数据样本作为文本;以及该数据的预期结果的屏幕截图。 (您也可以通过不使用整列引用来检查您的性能是否有所提高——创建一个表格并改用结构化引用)
  • 是否有必要查看所有 1,048,576 行?只需查看相关范围,您就可以大大加快公式的速度。
  • 不要在 PQ 中复制公式:添加带有月初日期 (BOM) 的自定义列。然后按名称/BOM 分组并与 Count 聚合。使用公式添加自定义列:=if Count>0 then 1 else 0。然后展开表格。

标签: excel-formula powerquery countif


【解决方案1】:

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

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

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"

原始数据

结果

【讨论】:

  • 太棒了!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
相关资源
最近更新 更多