简体   繁体   English

通过上一个值过滤的累计非重复计数-DAX

[英]Cumulative distinct count filtered by last value - DAX

I have a dataset: 我有一个数据集:

month   name    flag
1       abc     TRUE
2       xyz     TRUE
3       abc     TRUE
4       xyz     TRUE
5       abc     FALSE
6       abc     FALSE

I want to calculate month-cumulative distinct count of 'name' filtered by last 'flag' value (TRUE). 我想计算按“标志”值(真)过滤的“名称”的月份累计唯一计数。 Ie I want to have a result: 即我想要一个结果:

month   count
1       1
2       2
3       2
4       2
5       1
6       1

In months 5 and 6 'abc' should be excluded because the flag switched to 'FALSE' in month 5. I am trying to achieve something using examples given here: 在第5个月和第6个月,不应将'abc'排除在外,因为在第5个月标志已切换为'FALSE'。我正在尝试使用此处给出的示例来实现以下目标:

http://www.daxpatterns.com/cumulative-total/ http://www.daxpatterns.com/cumulative-total/

.

But I am struggling terribly. 但是我在努力挣扎。 I was thinking that maybe function TOPN could be used to filter the table on which DISTINCTCOUNT could be used but I am not getting the desired results. 我在想,也许可以使用功能TOPN来过滤可以在其上使用DISTINCTCOUNT的表,但是我没有得到期望的结果。

MyFilteredCumulativeMeasure =
COUNTROWS(
    FILTER(
        GENERATE(
            ALL( 'MyTable'[name] )
            ,CALCULATETABLE(
                SAMPLE(
                    1
                    ,SUMMARIZE(
                        'MyTable'
                        ,'MyTable'[month]
                        ,'MyTable'[flag]
                    )
                    ,'MyTable'[month]
                    ,DESC
                )
                ,FILTER(
                    ALL( 'MyTable'[month] )
                    ,'MyTable'[month] <= MAX( 'MyTable'[month] )
                )
            )
        )
        ,'MyTable'[flag]
    )
)

This works on your sample. 这适用于您的示例。 Might need some tweaking for the real data. 可能需要对实际数据进行一些调整。 Also likely to slow down significantly as data size increases. 随着数据大小的增加,也有可能显着降低速度。 I'll continue noodling on it, because this doesn't feel quite right, but it's a good naive implementation. 我将继续说明这一点,因为这感觉不太正确,但这是一个很好的幼稚实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM