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

时间:2019-12-22 06:37:28

标签: sum powerbi dax measure

我试图给出每个组的等级列,该等级列在原始表的组内的每一行中重复,但不提供求和后的形状。

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

表1

+-----------+------------+-------+

| 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" )

在确定总和法之前

1 个答案:

答案 0 :(得分:1)

该脚本是为计算列而不是度量而设计的。如果输入它作为度量,则EARLIER没有要引用的“上一个”行上下文,并且会给您错误。

创建度量:

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参数以更改排名模式。