Forum Discussion
Dynamically subtract two values in same column between two dates
- 3 years ago
Hi,
I did as below :
1- Add the Index column to the table in Power Query.
2- Then use below code for create the new column :
DiffVal Col =Var __CurrIndex = Sheet57[Index]Var __CurVal = (Sheet57[Value])Var __BeforeVal = CALCULATE(sum(Sheet57[Value]), filter(sheet57, Sheet57[Index] = __CurrIndex - 1 ))return __CurVal - __BeforeValAppreciate your Kudos and please mark it as solution if it helps you
Hi,
I did as below :
1- Add the Index column to the table in Power Query.
2- Then use below code for create the new column :
Appreciate your Kudos and please mark it as solution if it helps you
- daXtreme3 years agoSolution Sage
THe problem with your solution is that it makes the model unnecessarily bloated (as the index column is not really needed). Second, calculated columns are not compressed the way they are when created in Power Query - this results in even more bloat. In big models this may be unacceptable. Therefore, please use Power Query to do such calculations. Unless, of course, you don't care about how big the model is and how fast DAX could be. But I think you should since you pay money for storage. You also want to have fast DAX and columns optimized for access and calculations.
- dirkkoch3 years agoHelper III
MahyarTF
Thanks! Yes, that would work if there is a value in each row of the column. Unfortunately there might be some blanks, meaning that if there is no value, it should substract the actual with the last availabe value in the column- MahyarTF3 years agoMemorable Member
Hi dirkkoch
Please try the below code in the new column I described earlier :
DiffVal Col =Var __CurrIndex = Sheet57[Index]Var __PreviousIndex = CALCULATE(max(Sheet57[Index]), all(Sheet57), Sheet57[Index] < __CurrIndex, not(ISBLANK(Sheet57[Value])))Var __BeforePreviousIndex = CALCULATE(max(Sheet57[Index]), all(Sheet57), Sheet57[Index] < __PreviousIndex, not(ISBLANK(Sheet57[Value])))Var __PreviousVal = CALCULATE(sum(Sheet57[Value]), filter(sheet57, Sheet57[Index] = __PreviousIndex))Var __BeforePreviousVal = CALCULATE(sum(Sheet57[Value]), filter(sheet57, Sheet57[Index] = __BeforePreviousIndex))Var __NextVal = CALCULATE(sum(Sheet57[Value]), filter(sheet57, Sheet57[Index] = __CurrIndex+1))Var __CurVal = if(ISBLANK(Sheet57[Value]), __NextVal,Sheet57[Value])return if(ISBLANK(Sheet57[Value]), __PreviousVal - __BeforePreviousVal, __CurVal - __PreviousVal)Appreciate your Kudos and please mark it as a solution if it helps you