DAX – 计算表中连续值(组)的数量

huangapple go评论101阅读模式
英文:

DAX - count the number of consecutive values (groups) in a table

问题

以下是您提供的代码的翻译部分:

我有一张表格,有两列,一列列出日期,一列列出0或1。例如:

    日期   | 值
    1月1日 | 0
    1月2日 | 0
    1月3日 | 1
    1月4日 | 1
    1月5日 | 1
    1月6日 | 0
    1月7日 | 0
    1月8日 | 1
    1月9日 | 1
    1月10日 | 1
    1月11日 | 1
    1月12日 | 1
    1月13日 | 0
    1月14日 | 0
    1月15日 | 0
    1月16日 | 1
    1月17日 | 0
    1月18日 | 1
    1月19日 | 0
    1月20日 | 0

我试图创建一个度量,用于计算数据集中1的分组(簇)的数量。在这种情况下,答案应该是四。任何帮助将不胜感激。

编辑。

我设法创建了一个度量,可以勉强算作DAX度量,并且它可以工作,但显然不够优雅,我相信有更好的方法:

    var ClusterStart = ADDCOLUMNS(
        '表格',
        "簇起始",
        var PreviousNumber =  CALCULATE (
            MAX ( '表格'[值] ),
            FILTER (
                ALL ( '表格'),
                '表格'[日期] = EARLIER ( '表格'[日期])-1
            )) return
    
        if(AND('表格'[值]=1,PreviousNumber<>1),1,0)) return

    SUMX(ClusterStart,[簇起始])
英文:

I have a table with two columns, one listing dates, one listing one or zero. For example:

Date |Val
1-Jan	0
2-Jan	0
3-Jan	1
4-Jan	1
5-Jan	1
6-Jan	0
7-Jan	0
8-Jan	1
9-Jan	1
10-Jan	1
11-Jan	1
12-Jan	1
13-Jan	0
14-Jan	0
15-Jan	0
16-Jan	1
17-Jan	0
18-Jan	1
19-Jan	0
20-Jan	0

I am trying to create a measure which calculates the number of groups (clusters) of ones in the dataset. In this case the answer should be four. Any assistance would be appreciated.

Edit.

I have managed to create something that, with a measure of good will, could be considered a DAX measure and it is working but it's certainly not elegant and I'm sure there is a better way of doing it:

var ClusterStart = ADDCOLUMNS(
    &#39;Table&#39;,
    &quot;Cluster Start&quot;,
    var PreviousNumber =  CALCULATE (
        MAX ( &#39;Table&#39;[Val] ),
        FILTER (
            ALL ( &#39;Table&#39;),
            &#39;Table&#39;[Date] = EARLIER ( &#39;Table&#39;[Date])-1
        )) return

    if(AND(&#39;Table&#39;[Val]=1,PreviousNumber&lt;&gt;1),1,0)) return

SUMX(ClusterStart,[Cluster Start])

答案1

得分: 1

使用EARLIER()实际上已经不推荐使用,现在更倾向于使用VAR来使代码更易读。以下部分与您的示例代码返回相同结果。

Clusters = 
VAR ClusterStart = 
    ADDCOLUMNS(
        'Table',
        "Cluster Start",
        VAR d = CALCULATE(MAX('Table'[Date]))
        VAR PreviousNumber =  
        CALCULATE (
            MAX ( 'Table'[Val] ),
            FILTER (
                ALL ( 'Table'),
                'Table'[Date] = d-1
            )
        ) 
        RETURN
        IF(AND('Table'[Val]=1,PreviousNumber<>1),1,0)) 

RETURN
SUMX(
    ClusterStart,
    [Cluster Start]
)
英文:

Using EARLIER() is practically deprecated and it is now preferable to use VAR to make the code easier to read. The following returns the same as your code for example.

Clusters = 
VAR ClusterStart = 
    ADDCOLUMNS(
        &#39;Table&#39;,
        &quot;Cluster Start&quot;,
        VAR d = CALCULATE(MAX(&#39;Table&#39;[Date]))
        VAR PreviousNumber =  
        CALCULATE (
            MAX ( &#39;Table&#39;[Val] ),
            FILTER (
                ALL ( &#39;Table&#39;),
                &#39;Table&#39;[Date] = d-1
            )
        ) 
        RETURN
        IF(AND(&#39;Table&#39;[Val]=1,PreviousNumber&lt;&gt;1),1,0)) 

RETURN
SUMX(
    ClusterStart,
    [Cluster Start]
)

huangapple
  • 本文由 发表于 2023年6月19日 14:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定