如何使用 DAX Power BI 添加组内每一行的总和
How to add total sum of each rows within group with DAX Power BI
我试图给出每个组的排名列,这些列在原始 table 组内的每一行中重复,但不是汇总后的形状。
我在另一个网站找到的公式,但显示错误:
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" )
之前固定总和方法
脚本是为计算列设计的,而不是度量。如果您将其作为度量输入,则 EARLIER 没有 "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 个度量放入其中。结果:
如有必要,调整 RANKX 参数以更改排名模式。
我试图给出每个组的排名列,这些列在原始 table 组内的每一行中重复,但不是汇总后的形状。
我在另一个网站找到的公式,但显示错误: 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" )
之前固定总和方法
脚本是为计算列设计的,而不是度量。如果您将其作为度量输入,则 EARLIER 没有 "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 个度量放入其中。结果:
如有必要,调整 RANKX 参数以更改排名模式。