Forum Discussion
MarkCBB
Helper V
8 years ago2nd last date per item
Hi there, I need to create a measure that filters another measure to the 2nd last date. for example, For BP Charles I would like the measure to return the 41.2% score as it is the 2nd last score...
- 8 years ago
Hi Mark MarkCBB
Please try this MEASURE
Measure = VAR SecondLastDate = MINX ( TOPN ( 2, CALCULATETABLE ( VALUES ( TableName[DATE] ), ALLEXCEPT ( TableName, TableName[STORE] ) ), TableName[DATE], DESC ), TableName[DATE] ) RETURN CALCULATE ( [TotalAverageScore], FILTER ( ALLEXCEPT ( TableName, TableName[STORE] ), TableName[DATE] = SecondLastDate ) )
Zubair_Muhammad
Community Champion
8 years agoMarkCBB
Helper V
8 years agoHello Zubair_Muhammad,
I was able to do this after messing around a bit. This is the approach I took, I would love to hear your take on it.
First I created a new measure to get a Distinct Count:
Dist Count = COUNTROWS(SUMMARIZE(DATA,DATA[STORE],DATA[DATE]))
Then I edited your measure by adding a logical condition: (The IF)
2nd Last Visit =
Var Occurrence = 2
VAR SecondLastDate =
MINX (
TOPN (
Occurrence,
CALCULATETABLE (
VALUES ( 'DATA'[DATE] ),
ALLEXCEPT ( DATA, DATA[STORE] )
),
'DATA'[DATE], DESC
),
'DATA'[DATE]
)
RETURN
IF( [Dist Count] >=Occurrence ,
CALCULATE (
[TOTAL AVERAGE SCORE],
FILTER (
ALLEXCEPT ( DATA, DATA[STORE] ),
DATA[DATE] = SecondLastDate
)
),BLANK())- Zubair_Muhammad8 years ago
Community Champion
Great work Mark.
I wrote a similar revised MEASURE
Measure = VAR SecondLastDate = MINX ( TOPN ( 2, CALCULATETABLE ( VALUES ( TableName[DATE] ), ALLEXCEPT ( TableName, TableName[STORE] ) ), TableName[DATE], DESC ), TableName[DATE] ) VAR countdates = COUNTROWS ( CALCULATETABLE ( VALUES ( TableName[DATE] ), ALLEXCEPT ( TableName, TableName[STORE] ) ) ) RETURN IF ( [countdates] > 1, CALCULATE ( [TotalAverageScore], FILTER ( ALLEXCEPT ( TableName, TableName[STORE] ), TableName[DATE] = SecondLastDate ) ) )- Zubair_Muhammad8 years ago
Community Champion
Difference is that you had used a MEASURE while I used a VARIABLE
VAR countdates = COUNTROWS ( CALCULATETABLE ( VALUES ( TableName[DATE] ), ALLEXCEPT ( TableName, TableName[STORE] ) ) )- MarkCBB8 years ago
Helper V
Zubair_Muhammad, Thank you so much for your help, I have learnt something new.
Quick question, would there be any performance difference between the 2 approaches?