Forum Discussion
Calculations by Rank
- 7 years ago
Anonymous -
Would the below work for you?
Var = VAR nextRank = Table1[Rank] + 1 VAR differnce = CALCULATE( VALUES(Table1[Value]), FILTER(ALLEXCEPT(Table1,Table1[Type]),Table1[Rank] = nextRank) ) - Table1[Value] RETURN IF( differnce >= 0, differnce, BLANK() ) - 7 years ago
Anonymous -
The entire code block is your calculated column. Documentation on using VAR can be found @ https://docs.microsoft.com/en-us/dax/var-dax and a nice example article can be found @ https://www.sqlbi.com/articles/variables-in-dax/
- 7 years ago
Anonymous -
You're missing closing ' ) ' in a couple places.
Copy -> Paste my code and replace 'Table1' with 'Primary_ID' or try formatting multi-line by clicking ALT + ENTER so you can see your opening and closing Parentheses ' ( ' & ' )' respectively a little easier.
It almost works Chris.
However this is the result I'd really like;
| Primary ID | Type | Date | Value | Rank | Var |
| Type A20170101 | Type A | 01/01/17 | 100 | 1 | 0 |
| Type A20170201 | Type A | 01/02/17 | 110 | 2 | 10 |
| Type A20170301 | Type A | 01/03/17 | 125 | 3 | 15 |
| Type B20180101 | Type B | 01/01/18 | 90 | 1 | 0 |
| Type B20180201 | Type B | 01/02/18 | 95 | 2 | 5 |
| Type B20180301 | Type B | 01/03/18 | 105 | 3 | 10 |
| Type B20180401 | Type B | 01/04/18 | 120 | 4 | 15 |
Where the Var for Rank 1 is alway 0 as there is no lower rank. The var for rank 2 onwards is the value of rank 2 less rank 1, value of rank 3 less rank 2 etc.
Sorry to be a nusiance!
Anonymous -
Var =
VAR prevRank = Table1[Rank] - 1
VAR differnce =
Table1[Value] -
CALCULATE(
VALUES(Table1[Value]),
FILTER(ALLEXCEPT(Table1,Table1[Type]),Table1[Rank] = prevRank )
)
RETURN
IF(
differnce < Table1[Value],
differnce,
0
)
- ChrisMendoza7 years agoResident Rockstar
Anonymous -
You're missing closing ' ) ' in a couple places.
Copy -> Paste my code and replace 'Table1' with 'Primary_ID' or try formatting multi-line by clicking ALT + ENTER so you can see your opening and closing Parentheses ' ( ' & ' )' respectively a little easier.
- Anonymous7 years agoNot applicable
Here's my code...
= VAR prevRank = Primary_ID[Rank] - 1 VAR differnce = Primary_ID[Value] - CALCULATE( VALUES(Primary_ID[Value], FILTER(ALLEXCEPT(Primary_ID,Primary_ID[Type],Primary_ID[Rank] = prevRank ) ) RETURN IF( differnce < Primary_ID[Value], differnce, 0 )
and here's the error I get...
- Anonymous7 years agoNot applicable
Perfect! Many thanks for perservering with me Chris.
Much appreciated.
I learnt a lot.
Thanks again.
Peter