Forum Discussion
Calculate the difference between two columns (without hard coding in formula) in matrix
- Anonymous7 years ago
Hi dancarr22,
Index column is a custom column I created in query edit.
Since power bi data model not contains column index and row index, you need one index to let formula calculation dynamic based on current index.(such current -1 or current +1)
Date or numeric column can be used as index, text character current not support compare with math symbols.
If you only need to calculate on specific source, you can try to use switch function to define them as specific numeric as calculation index.
Regards,
Xiaoxin Sheng
HI dancarr22,
I think you need to add index column based on 'pricing source' column to let formula dynamic.
Sample formula:
Diff =
VAR currIndex =
MAX ( Table3[Index] )
VAR currPrice =
CALCULATE (
SUM ( Table3[Price] ),
FILTER ( ALLSELECTED ( Table3 ), Table3[Index] = currIndex ),
VALUES ( Table3[Ticker] ),
VALUES ( Table3[Date] )
)
VAR prevPrice =
CALCULATE (
SUM ( Table3[Price] ),
FILTER ( ALLSELECTED ( Table3 ), Table3[Index] = currIndex - 1 ),
VALUES ( Table3[Ticker] ),
VALUES ( Table3[Date] )
)
RETURN
IF ( prevPrice <> BLANK (), currPrice - prevPrice, currPrice )
Regards,
Xiaoxin Sheng
Hi Xiaoxin,
Thanks for your assistance with this.
Unfortunately, the solution you provided did not work.
Issue appears to be in the index logic. while we do have indexes associated with the price source they are not in strict numerical order. Instead of using "currIndex - 1" is there some other way to define the selected indexes?
If not, can you just provide an example where we do price source 'Bloomberg' vs price source 'Reuters'? Not the optimal dynamic solution but can go with this for now.
Thanks,
Dan