Forum Discussion
problem with a calculation
- 4 years ago
thanks!
Pragati11, I need to have the sum of all the values that match with A and substract all the values that match with B, I attached more simple data, thanks
| type | value |
| A | 10 |
| B | 3 |
| A | 5 |
| B | 4 |
| A | 7 |
| B | 3 |
| A | 9 |
| B | 2 |
HI Giada90 ,
The solution that you have accepted, will only work when you have only 2 categories in your data - A and B.
If you are interested in a dynamic solution for your problem, say where you have more number of categories than just A and B, then try the follwing solution I have created.
The sample data I am considering is:
First thing I did was created a Rank column at the type column level in the data using the following DAX expression:
Rank =
RANKX(
TypeTable,
TypeTable[total value], ,
DESC, Dense
)
When you move this to table visual you will see basically a Rank assigned for every type value in the data.
Now the next thing is we need to get the sum of value column at a Type level.
This we can get using following DAX:
total value at Type level =
CALCULATE(
SUM(TypeTable[value]),
ALLEXCEPT(TypeTable,TypeTable[type])
)
Now we need a difference between these values which should be 31 - 12 = 19 in this case as we just have 2 categories in TYPE column. The following DAX will give the required answer:
Difference at Type level =
var minRank = CALCULATE(MIN(TypeTable[Rank]), ALL(TypeTable))
var totalValue = CALCULATE(
SUM(TypeTable[value]),
ALL(TypeTable),
TypeTable[Rank] = minRank
)
var nextTotal =
CALCULATE(
SUM(TypeTable[value]),
//ALLEXCEPT(TypeTable, TypeTable[type]),
ALL(TypeTable),
TypeTable[Rank] = minRank + 1
)
var diffVal = totalValue - nextTotal
RETURN
diffVal
The result is as follows:
As part of best practice, I always try to use the dynamic solutions as data can change over time.
Hope this solution helps.