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
rajivraina
Helper II
Helper II

Need Help: Show second to last value

Hi all,

 

I was hoping someone could help me figure out a way to show the second to last value for a certain field. 

 

For example, I have country credit ratings which are a text field (D through AAA+) that can change at irregular intervals (so not monthly or yearly). 

 

I have a table that shows the latest rating for countries that have had it changed in the past 3 months: capture3.PNG

Now I want to get another column in this table that shows the what the previous rating was, thus my need to find the 2nd to last value.

 

All the columns in the table are normal raw data columns (no measures). 

 

Anyone know how I can do this?

 

Thanks,

Raj

 

 

1 ACCEPTED SOLUTION
AkhilAshok
Solution Sage
Solution Sage

You could probably create a Calculated column like this:

 

Previous Rating = 
VAR CurrentDate = 'Table'[Date]
VAR CurrentCountry = 'Table'[Country]
VAR AllPreviousRatingsTable =
    FILTER (
        'Table',
        'Table'[Country] = CurrentCountry
            && 'Table'[Date] < CurrentDate
    )
VAR PreviousRatingTable =
    TOPN ( 1, AllPreviousRatingsTable, 'Table'[Date], DESC )
VAR PreviousRating =
    MAXX ( PreviousRatingTable, 'Table'[Rating] )
RETURN
    PreviousRating

View solution in original post

3 REPLIES 3
AkhilAshok
Solution Sage
Solution Sage

You could probably create a Calculated column like this:

 

Previous Rating = 
VAR CurrentDate = 'Table'[Date]
VAR CurrentCountry = 'Table'[Country]
VAR AllPreviousRatingsTable =
    FILTER (
        'Table',
        'Table'[Country] = CurrentCountry
            && 'Table'[Date] < CurrentDate
    )
VAR PreviousRatingTable =
    TOPN ( 1, AllPreviousRatingsTable, 'Table'[Date], DESC )
VAR PreviousRating =
    MAXX ( PreviousRatingTable, 'Table'[Rating] )
RETURN
    PreviousRating


@AkhilAshok wrote:

You could probably create a Calculated column like this:

 

Previous Rating = 
VAR CurrentDate = 'Table'[Date]
VAR CurrentCountry = 'Table'[Country]
VAR AllPreviousRatingsTable =
    FILTER (
        'Table',
        'Table'[Country] = CurrentCountry
            && 'Table'[Date] < CurrentDate
    )
VAR PreviousRatingTable =
    TOPN ( 1, AllPreviousRatingsTable, 'Table'[Date], DESC )
VAR PreviousRating =
    MAXX ( PreviousRatingTable, 'Table'[Rating] )
RETURN
    PreviousRating


Thank you this worked perfectly!!

Ashish_Mathur
Super User
Super User

Hi,

Share the link from where i can download the PBI file or paste the Table here so that i can take it to an Excel file.  More importantly, please show the expected result in another column.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

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.