如何使用DAX Power BI将组内每行的总和相加 [英] How to add total sum of each rows within group with DAX Power BI

查看:201
本文介绍了如何使用DAX Power BI将组内每行的总和相加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在另一个网站上找到的公式,但显示错误:

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

I am trying to give rank column of every group which repeating in every rows within the group of the original table but not the shape of after sum-up.

The formula i found in another site but it show an error : https://intellipaat.com/community/9734/rank-categories-by-sum-power-bi

Table1

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

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

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

The Goal

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

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

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

The script

sum =

SUMX(

    FILTER(

         Table1;

         Table1[product] = EARLIER(Table1[product])

    );

    Table1[sales]

) 

The Error :

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

What's wrong with the script above ? * not able to test this script:

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

before fixed the sum approach

解决方案

The script is designed for a calculated column, not a measure. If you enter it as a measure, EARLIER has no "previous" row context to refer to, and gives you the error.

Create a measure:

Total Sales = SUM(Table1[sales])

This measure will be used to show sales.

Create another measure:

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

This measure will show sales by product ignoring dates.

Third measure:

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

Create a report with product and dates on a pivot, and drop all 3 measures into it. Result:

Tweak RANKX parameters to change the ranking mode, if necessary.

这篇关于如何使用DAX Power BI将组内每行的总和相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆