问题
I've looked everywhere for this but any other example is not exactly what I want to achieve. I have data that looks like that:
Day Group Sales
14 1 15
13 0 22
14 1 17
and so on. Now I create calculated field of Running Total using following formula:
CALCULATE (
SUM(Table[Sales]),
FILTER (
ALL (table),
Table[Date] <= MAX(Table[Date])
)
)
I create Pivot Table and put Date and Group as rows, then Running Total as Values. Why running total is not aggregated separately for each group? I would like Group 1 to have its calculation, and Group 0 too.
回答1:
The reason that running total is not aggregated separately for each group is because your formula is incomplete. You have just used the ALL function for FILTER. The ALL will mean that you will ignore the existing filter contexts. So on a row with Group 0 you will ignore the Group 0 filter.
What I think you want is to retain the Group filter. You can achieve this by replacing ALL (table),
with either ALLEXCEPT(table,table[Group]),
or ALL(table[Date]),
. Both of these will give a running total just for the Group row so you will have a running total for Group 0 and one for Group 1.
If what you are looking for is a running total where the totals accumulate by both Date and Group you can keep ALL and add an extra expression for FILTER:
CALCULATE (
SUM ( table[Sales] ),
FILTER (
ALL ( table ),
table[Date] <= MAX ( table[Date] )
&& table[Group] <= MAX ( table[Group] )
)
)
来源:https://stackoverflow.com/questions/30072341/dax-running-total-multiple-critiera-grouping