Forum Discussion
Hoszi
6 years agoFrequent Visitor
Calculate row diffence within a table with DAX
Hi,
I'd like to write a DAX code for the column Difference (see the method in calculate) within a table, using row filtering. I try with DAX function EARLIER, FILTER, DATEADD, but I couldn't find the right solution.
| (1) | Count (A) | Date | Difference | Calculate |
| (2) | 0 | 2020.03.03 | ||
| (3) | 2 | 2020.03.04 | 2 | =A3-A2 |
| (4) | 2 | 2020.03.05 | 0 | =A4-A3 |
| (5) | 2 | 2020.03.06 | 0 | =A5-A4 |
| 4 | 2020.03.07 | 2 | ... | |
| 7 | 2020.03.08 | 3 | ||
| 9 | 2020.03.09 | 2 | ||
| 9 | 2020.03.10 | 0 | ||
| 13 | 2020.03.11 | 4 | ||
| 13 | 2020.03.12 | 0 | ||
| 19 | 2020.03.13 | 6 |
Thanks,
András
Finally, I find the good calculation.
DailyChange = VAR Yesterday = DATEADD(Table[Date]; -1; DAY) RETURN Table[Count (A)] - CALCULATE( MAXX(Table;Table[Count(A)]); Table[Date] <= Yesterday )
5 Replies
- az38Community Champion
Hi Hoszi
if you have one row per day try a measure
Difference = var _prevDate = CALCULATE(MAX('Table'[Date]),FILTER(ALL('Table'),'Table'[Date] < SELECTEDVALUE('Table'[Date]))) return IF(ISBLANK(_prevDate), BLANK(), SELECTEDVALUE('Table'[Count (A)]) - LOOKUPVALUE('Table'[Count (A)],'Table'[Date],_prevDate))- AnonymousNot applicableCreate one index column and then using lookuovalue function you can the previous or next row value by adding or subtracting 1 from index.
Prev=
Var current=table[value]
Var previous=lookupvalue (table[value],table[index],table[index]-1)
Return
Current-previous
Thanks
Pravin
- AnonymousNot applicableThis is a calculation for Power Query, not for DAX.
Best
D - HosziFrequent Visitor
Finally, I find the good calculation.
DailyChange = VAR Yesterday = DATEADD(Table[Date]; -1; DAY) RETURN Table[Count (A)] - CALCULATE( MAXX(Table;Table[Count(A)]); Table[Date] <= Yesterday )