DAX:运行 选择范围内的不同计数

DAX: Running count distinct within selection

我的 Power Query 模型中有一个数据 table,如下所示:

date customer_id category1 category2
1 1 1 1
2 1 1 2
3 4 2 3
1 2 2 2
2 2 1 3
3 4 1 1
1 2 2 2
2 3 2 1
3 5 1 3
1 1 1 1
2 3 2 3
3 6 2 2
1 2 1 3
2 2 1 2
3 5 1 1

我有一个计算出的度量值不同 customer_id:

Distinct_cust:=DISTINCTCOUNT(Table1[customer_id]))

这种测量在数据透视表中非常有效 table - 它可以准确地计算我添加到数据透视表和所有小计的任何和所有字段的所有插入。

现在我想要一个在给定交叉点范围内工作但考虑到以前日期值的措施 - 它应该 return 运行 计算 custeomer_id,但仅适用于当前枢轴 table.

的天数值

现在我有这样的东西,几乎可以正常工作:

Cumulative_Distinct_cust:=CALCULATE (
    [Distinct_cust];
    FILTER (
        ALL ( 'Table1'[date] );
        'Table1'[date] <= MAX ('Table1'[date]) 
    )
)

这 return 确实 运行 计数不同,但前提是我的数据透视表 table 中没有过滤掉所有日期。一旦我从数据透视表中删除第 1 天,它仍然显示 运行 第 2 天和第 3 天的计数不同,如果第 1 天也被考虑在内的话:

我想出的蛮力解决方案,我正在寻找替代方法是在 FILTER 语句中手动添加日期过滤器,这会将日期限制为仅第 2 天和第 3 天:

Cumulative_Distinct_cust:=CALCULATE (
    [Distinct_cust];
    FILTER (
        ALL ( 'Table1'[date] );
        AND(
              'Table1'[date] >= 2; <-- added this to filter
              'Table1'[date] <= MAX ( 'Table1'[date]) 
    )
))

(您可以在标记为 1 的列中看到差异)

但显然,每次我想更改日期范围时,更改起来都很丑陋且麻烦。有什么方法可以传递日期字段的当前可用值以进行此计算吗?

你可以这样做:

Cumulative_Distinct_cust:=VAR _date = MAX(Table1[date])
VAR _dates = CALCULATETABLE(VALUES(Table1[date]); ALLSELECTED())
VAR _rt_dates = FILTER(_dates; Table1[date]<= _date)
RETURN
CALCULATE([Distinct_cust];_rt_dates)