The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
I have a table containing state changes for multiple values
time | primary_key | value | lag_value |
1 | A | 100 | 0 |
2 | B | 200 | 0 |
3 | A | 300 | 100 |
4 | B | 400 | 200 |
6 | A | 0 | 300 |
6 | B | 0 | 400 |
I can easily calculate the value of the last state with the following formula:
WantedVal =
var vDate = MAX('Table'[time])
Return
CALCULATE(SUM('Table'[value]),FILTER(ALL('Table'[time]),'Table'[time]<= vDate))-CALCULATE(SUM('Table'[lag_value]),FILTER(ALL('Table'[time]),'Table'[time]<= vDate))
However, I'd rather not create all the lags as I have multiple value variables.
I've tried to recreate this measure without the lag, but I fail to make it work in a context of primary_key + time, primary_key or time.
For instance, the following measure works in primary_key + time but not in a context only using time:
no_lag_value =
VAR vDate = MAX('Table'[time])
RETURN
CALCULATE(SUM('Table'[value]),FILTER(ALL('Table'[time]),'Table'[time]<= vDate)) - SUMX( FILTER(ALLSELECTED('Table'),'Table'[group] in VALUES('Table'[group])),CALCULATE(SUM('Table'[value]),'Table'[time]<vDate))
Summarising, is there a way to reproduce WantedVal without using lag_value?
@Anonymous ,
new column =
var _1 = maxx(filter(Table1, Table1[time] < earlier(Table1[time]) && Table1[primary_key] =earlier(Table1[primary_key]) ), [time])
return
maxx(filter(Table1, Table1[time] =_1 && Table1[primary_key] =earlier(Table1[primary_key]) ), [value])
Hello @amitchandak ,
I don't want a column with the lag (I could add it in the m query). I have around ten values on change, and more can come making it hard to sustain. For that reason, I'm looking for a measure that I can generalise if needed.
User | Count |
---|---|
10 | |
9 | |
6 | |
6 | |
5 |
User | Count |
---|---|
22 | |
14 | |
14 | |
9 | |
7 |