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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

2nd last date

I have the following query below calculating the latest date per ID to give me an outcome, what i want to try and do is get the date previous to the max so the 2nd to last date is there an easy way i can amend the top VAR _maxdate to show 2nd last date rather than the max?

VAR _maxdate =
    CALCULATE (
        MAX ( 'Table1'[Date] ),
        FILTER ( ALL ( 'Table1' ), 'Table1'[ID] = MAX ( 'Table1'[ID]) )
    )
RETURN
    CALCULATE (
        MAX ( 'Table1'[outcome] ),
        FILTER (
            ALL ( 'Table1' ),
            'Table1'[ID] = MAX ( 'Table1'[ID])
                && 'Table1'[Date] = _maxdate
        )
    )

 

 

 

 

 

1 ACCEPTED SOLUTION
v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

 

According to your measure, it seems that you want to show the outcome of the 2nd last Date for each ID ,right?

 

I did it in two ways, please check.

 

1. Get the last date firstly and then use MAXX() to get the date which is < the last date, it will be the 2nd last date:

Measure1 = 
VAR _last =
    CALCULATE ( MAX ( 'Table1'[Date] ), ALLEXCEPT ( Table1, Table1[ID] ) )
VAR _2nd =
    MAXX (
        FILTER ( ALL ( 'Table1' ), [ID] = MAX ( 'Table1'[ID] ) && [Date] < _last ),
        [Date]
    )
RETURN
    CALCULATE (
        MAX ( 'Table1'[outcome] ),
        FILTER ( ALL ( 'Table1' ), [ID] = MAX ( 'Table1'[ID] ) && [Date] = _2nd )
    )

2.Use RANK() to sort by [Date] in descending order, then the 2nd last Date is [Rank]=2:

Rank =
RANKX (
    FILTER ( ALL ( 'Table1' ), [ID] = MAX ( 'Table1'[ID] ) ),
    CALCULATE ( MAX ( ( 'Table1'[Date] ) ) ),
    ,
    DESC
)
Measure2 = 
CALCULATE (
    MAX ( 'Table1'[outcome] ),
    FILTER ( ALL ( Table1 ), [ID] = MAX ( 'Table1'[ID] ) && [Rank] = 2 )
)

 Here is the final output:

Eyelyn9_0-1631584071076.png

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Ashish_Mathur
Super User
Super User

Hi,

Share some data and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-eqin-msft
Community Support
Community Support

Hi @Anonymous ,

 

According to your measure, it seems that you want to show the outcome of the 2nd last Date for each ID ,right?

 

I did it in two ways, please check.

 

1. Get the last date firstly and then use MAXX() to get the date which is < the last date, it will be the 2nd last date:

Measure1 = 
VAR _last =
    CALCULATE ( MAX ( 'Table1'[Date] ), ALLEXCEPT ( Table1, Table1[ID] ) )
VAR _2nd =
    MAXX (
        FILTER ( ALL ( 'Table1' ), [ID] = MAX ( 'Table1'[ID] ) && [Date] < _last ),
        [Date]
    )
RETURN
    CALCULATE (
        MAX ( 'Table1'[outcome] ),
        FILTER ( ALL ( 'Table1' ), [ID] = MAX ( 'Table1'[ID] ) && [Date] = _2nd )
    )

2.Use RANK() to sort by [Date] in descending order, then the 2nd last Date is [Rank]=2:

Rank =
RANKX (
    FILTER ( ALL ( 'Table1' ), [ID] = MAX ( 'Table1'[ID] ) ),
    CALCULATE ( MAX ( ( 'Table1'[Date] ) ) ),
    ,
    DESC
)
Measure2 = 
CALCULATE (
    MAX ( 'Table1'[outcome] ),
    FILTER ( ALL ( Table1 ), [ID] = MAX ( 'Table1'[ID] ) && [Rank] = 2 )
)

 Here is the final output:

Eyelyn9_0-1631584071076.png

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

DemoFour
Responsive Resident
Responsive Resident

@Anonymous  When I have tackled something similar, I have used RANKEQ to rank the dates by ID and then found the max -1 that way.

Greg_Deckler
Super User
Super User

@Anonymous Maybe:

VAR _maxdate1 =
    CALCULATE (
        MAX ( 'Table1'[Date] ),
        FILTER ( ALL ( 'Table1' ), 'Table1'[ID] = MAX ( 'Table1'[ID]) )
    )
VAR __maxdate = 
    CALCULATE (
        MAX ( 'Table1'[Date] ),
        FILTER ( ALL ( 'Table1' ), 'Table1'[ID] = MAX ( 'Table1'[ID]) && 'Table1'[Date]=__maxdate1 )
    )
RETURN
    CALCULATE (
        MAX ( 'Table1'[outcome] ),
        FILTER (
            ALL ( 'Table1' ),
            'Table1'[ID] = MAX ( 'Table1'[ID])
                && 'Table1'[Date] = _maxdate
        )
    )

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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