Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
Nickb167
Frequent Visitor

Getting value for second last date in related table.

Hi,

 

I have a data set that appears as below:

IDDateScore
11/01/20203
31/01/20202
41/01/20205
61/01/20204
12/02/20202
32/02/20203
42/02/20205
62/02/20201
13/03/20205
33/03/20203
43/03/20204
63/03/20201
14/04/20202
34/04/20205
44/04/20202
64/04/20204

 

The expected result is:

 

IDFirst ScoreLatest ScoreSecond Latest Score
1325
3253
4524
6441

 

I have worked out the First and Latest Scores with:

Latest Score = SUMX(
VALUES(cnsw_carersstar[cnsw_ProgramParticipant]),
CALCULATE(MAX(cnsw_carersstar[CarersStarScore]), LASTDATE(cnsw_carersstar[DateOnly]))
)
 
But am having difficulity getting the second latest. I have tried adding filters to the above but cannot seem to get the expected result. Any help would be greatly appreciated.
 
 
Thanks,
 
Nick
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Nickb167 ,

According to my understand, you want to display Score based on the latest date ,the second latest date and the first date, right?

You could use the following formula:

rank =
RANKX (
    FILTER (
        ALL ( LastDateTable ),
        'LastDateTable'[ID] = MAX ( 'LastDateTable'[ID] )
    ),
    CALCULATE ( MAX ( ( LastDateTable[Date] ) ) ),
    ,
    ASC
)
First Score =
CALCULATE ( MAX ( LastDateTable[Score] ), FILTER ( LastDateTable, [rank] = 1 ) )
Last Score =
CALCULATE (
    MAX ( 'LastDateTable'[Score] ),
    FILTER ( 'LastDateTable', [rank] = MAXX ( ALL ( LastDateTable ), [rank] ) )
)
Second Last Score =
CALCULATE (
    MAX ( 'LastDateTable'[Score] ),
    FILTER ( 'LastDateTable', [rank] = MAXX ( ALL ( LastDateTable ), [rank] - 1 ) )
)

My visualization looks like this:

9.15.1.1.png

Did I answer your question ? Please mark my reply as solution. Thank you very much.

If not, please upload some insensitive data samples and expected output.

 

Best Regards,

Eyelyn Qin

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @Nickb167 ,

According to my understand, you want to display Score based on the latest date ,the second latest date and the first date, right?

You could use the following formula:

rank =
RANKX (
    FILTER (
        ALL ( LastDateTable ),
        'LastDateTable'[ID] = MAX ( 'LastDateTable'[ID] )
    ),
    CALCULATE ( MAX ( ( LastDateTable[Date] ) ) ),
    ,
    ASC
)
First Score =
CALCULATE ( MAX ( LastDateTable[Score] ), FILTER ( LastDateTable, [rank] = 1 ) )
Last Score =
CALCULATE (
    MAX ( 'LastDateTable'[Score] ),
    FILTER ( 'LastDateTable', [rank] = MAXX ( ALL ( LastDateTable ), [rank] ) )
)
Second Last Score =
CALCULATE (
    MAX ( 'LastDateTable'[Score] ),
    FILTER ( 'LastDateTable', [rank] = MAXX ( ALL ( LastDateTable ), [rank] - 1 ) )
)

My visualization looks like this:

9.15.1.1.png

Did I answer your question ? Please mark my reply as solution. Thank you very much.

If not, please upload some insensitive data samples and expected output.

 

Best Regards,

Eyelyn Qin

Anonymous
Not applicable

Hi @Nickb167 ,

 

I think the below table is what you want:

 

Getting value for second last date in related table.PNG

 

I add an index column in the table, and the measures are:

 

First Score = CALCULATE(SUM('Table (2)'[Score]), FILTER(ALL('Table (2)'[ID],'Table (2)'[Index]),'Table (2)'[Index] = MIN('Table (2)'[Index])))
Last Score = CALCULATE(SUM('Table (2)'[Score]), FILTER(ALL('Table (2)'[ID],'Table (2)'[Index]),'Table (2)'[Index] = MAX('Table (2)'[Index])))
Second Last Score = CALCULATE(SUM('Table (2)'[Score]), FILTER(ALL('Table (2)'[ID],'Table (2)'[Index]),'Table (2)'[Index] = MAXX(FILTER('Table (2)','Table (2)'[Index] < MAX('Table (2)'[Index])),'Table (2)'[Index])))

 

Table:

Getting value for second last date in related table 2.PNGGetting value for second last date in related table 3.PNG

 

 

 

Please try.

Aiolos Zhao

amitchandak
Super User
Super User

@Nickb167 ,

Try

 

last = lastnonblankvalue(Table[Date], sum(Table[Score]))
Second last =
var _max = maxx(allselected(Date[Date]), Date[Date])
return
calculate(lastnonblankvalue(Table[Date], sum(Table[Score])), filter(allselected(Table), Table[Date] <_max))


First = Firstnonblankvalue(Table[Date], sum(Table[Score]))
Second  =
var _max = maxx(allselected(Date[Date]), Date[Date])
return
calculate(Firstnonblankvalue(Table[Date], sum(Table[Score])), filter(allselected(Table), Table[Date] <_max))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.