I am trying to replicate a calculation originally made in Excel, using PowerBI and although I have read some documentation about context calculation in measures, I am struggling to get the results.
My table would like as the one below
Date | Weight | Calculated_measure |
---|---|---|
01/01/2020 | 1 | 0 |
01/02/2020 | 3 | 31 |
01/03/2020 | 3 | 147 |
Dates are in dd/mm/yyyy.
The idea is the following for the calculated measure:
- For each line I compare the current “Date” value with all other “Date” values and make a subtraction.
- If the result of the subtraction is greater than zero, I keep the result. Otherwise, I set the result to 0.
- Then I multiply the results by the weight
To illustrate the calculation of the third line: (01/03/2020-01/01/2020)*1 + (01/03/2020-01/02/2020) *3 + (01/03/2020-01/03/2020) *2 =60 * 1 + 29 * 3 + 0 = 147
The formula in Excel for the first line, for example:
= {
SUMPRODUCT(
IF
(
(A2-$A$2:$A$4)>0;
(A2-$A$2:$A$4);
0
)
; $B$2:$B$4
)
}
Thank you. Understanding this code will help me a lot to learn context filters in PowerBI