代码之家  ›  专栏  ›  技术社区  ›  rane

如何使用DAX Power BI添加组内每行的总和

  •  0
  • rane  · 技术社区  · 6 年前

    我试图给出每个组的秩列,这些列在原始表的组内的每一行中重复,而不是after sum的形状。

    我在另一个网站上找到的公式,但它显示了一个错误: https://intellipaat.com/community/9734/rank-categories-by-sum-power-bi

    +-----------+------------+-------+
    
    | product   | date       | sales |
    
    +-----------+------------+-------+
    
    | coffee    | 11/03/2019 | 15    |
    
    | coffee    | 12/03/2019 | 10    |
    
    | coffee    | 13/03/2019 | 28    |
    
    | coffee    | 14/03/2019 | 1     |
    
    | tea       | 11/03/2019 | 5     |
    
    | tea       | 12/03/2019 | 2     |
    
    | tea       | 13/03/2019 | 6     |
    
    | tea       | 14/03/2019 | 7     |
    
    | Chocolate | 11/03/2019 | 30    |
    
    | Chocolate | 11/03/2019 | 4     |
    
    | Chocolate | 11/03/2019 | 15    |
    
    | Chocolate | 11/03/2019 | 10    |
    
    +-----------+------------+-------+
    

    目标

    +-----------+------------+-------+-----+------+
    
    | product   | date       | sales | sum | rank |
    
    +-----------+------------+-------+-----+------+
    
    | coffee    | 11/03/2019 | 15    | 54  | 5    |
    
    | coffee    | 12/03/2019 | 10    | 54  | 5    |
    
    | coffee    | 13/03/2019 | 28    | 54  | 5    |
    
    | coffee    | 14/03/2019 | 1     | 54  | 5    |
    
    | tea       | 11/03/2019 | 5     | 20  | 9    |
    
    | tea       | 12/03/2019 | 2     | 20  | 9    |
    
    | tea       | 13/03/2019 | 6     | 20  | 9    |
    
    | tea       | 14/03/2019 | 7     | 20  | 9    |
    
    | Chocolate | 11/03/2019 | 30    | 59  | 1    |
    
    | Chocolate | 11/03/2019 | 4     | 59  | 1    |
    
    | Chocolate | 11/03/2019 | 15    | 59  | 1    |
    
    | Chocolate | 11/03/2019 | 10    | 59  | 1    |
    
    +-----------+------------+-------+-----+------+
    

    剧本

    sum =
    
    SUMX(
    
        FILTER(
    
             Table1;
    
             Table1[product] = EARLIER(Table1[product])
    
        );
    
        Table1[sales]
    
    ) 
    

    错误:

    EARLIER(Table1[product]) # Parameter is not correct type cannot find name 'product' 
    

    上面的剧本怎么了?

    rank = RANKX( ALL(Table1); Table1[sum]; ;; "Dense" )
    

    在确定和方法之前

    0 回复  |  直到 6 年前
        1
  •  1
  •   RADO    6 年前

    脚本是为计算列而不是度量值设计的。如果将其作为度量值输入,则早期没有可引用的“previous”行上下文,并给出错误。

    创建度量值:

    Total Sales = SUM(Table1[sales])
    

    创建另一个度量值:

    Sales by Product =
    SUMX(
      VALUES(Table1[product]);
      CALCULATE([Total Sales]; ALL(Table1[date]))
    )
    

    此度量将按产品显示销售额,忽略日期。

    Sale Rank = 
      RANKX(
         ALL(Table1[product]; Table1[date]); 
         [Sales by Product];;DESC;Dense)
    

    创建一个以产品和日期为中心的报表,并将所有3个度量值放入其中。结果:

    enter image description here

    如有必要,调整RANKX参数以更改排名模式。

    推荐文章