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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Fazal
New Member

Finding two most recent dates from one table

Hi there,

 

Need urgent assistance regarding date calculation. I have table where there are multiple dates per assetid. I would like to extract most two recent dates and difference in days between those 2 dates.

 

Sample Data:

AssetidInspection Date
B00024814/09/2020
B000248

 19/09/2019

B000248

10/09/2018

 

Sample Output:

 

AssetidMost Recent Date_1Most Recent Date_2Date Difference(Days)
B00024814/09/202019/09/2019361

 

Hope it gives an idea of what I am trying to achieve. Really appreciate any help.

 

Thanks

1 ACCEPTED SOLUTION
Samarth_18
Community Champion
Community Champion

Hi @Fazal 

 

You can update your code as below

 

_max_date =
CALCULATE (
    MAX ( 'Previous RMP Inspection (1)'[Date Inspected] ),
    FILTER (
        ALL ( 'Previous RMP Inspection (1)' ),
        'Previous RMP Inspection (1)'[Asset Id]
            = MAX ( 'Previous RMP Inspection (1)'[Asset Id] )
    )
)
_second_max_date =
CALCULATE (
    MAX ( 'Previous RMP Inspection (1)'[Date Inspected] ),
    FILTER (
        ALL ( 'Previous RMP Inspection (1)' ),
        'Previous RMP Inspection (1)'[Date Inspected] < [_max_date]
            && 'Previous RMP Inspection (1)'[Asset Id]
                = MAX ( 'Previous RMP Inspection (1)'[Asset Id] )
    )
)
_date_diff =
DATEDIFF ( [_second_max_date], [_max_date], DAY )

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

View solution in original post

4 REPLIES 4
Fazal
New Member

Hi Samarth,

 

Thanks for your reply. It works excellently when I am doing it on the sample data I provided earlier however when I apply the same measures on real large dataset it produces some static dates. 

Would you please be able to run this on my cut down large dataset is in the link?

https://www.dropbox.com/s/w3v3bi4nvn9s8rr/Previous%20RMP%20Inspection%20%281%29.csv?dl=0

 

Appreciated your time!

 

Samarth_18
Community Champion
Community Champion

Hi @Fazal 

 

You can update your code as below

 

_max_date =
CALCULATE (
    MAX ( 'Previous RMP Inspection (1)'[Date Inspected] ),
    FILTER (
        ALL ( 'Previous RMP Inspection (1)' ),
        'Previous RMP Inspection (1)'[Asset Id]
            = MAX ( 'Previous RMP Inspection (1)'[Asset Id] )
    )
)
_second_max_date =
CALCULATE (
    MAX ( 'Previous RMP Inspection (1)'[Date Inspected] ),
    FILTER (
        ALL ( 'Previous RMP Inspection (1)' ),
        'Previous RMP Inspection (1)'[Date Inspected] < [_max_date]
            && 'Previous RMP Inspection (1)'[Asset Id]
                = MAX ( 'Previous RMP Inspection (1)'[Asset Id] )
    )
)
_date_diff =
DATEDIFF ( [_second_max_date], [_max_date], DAY )

 

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Thanks! Works Perfectly. Thanks again.

Samarth_18
Community Champion
Community Champion

Hi @Fazal 

You can create measure with below code:-

1. For max date:-

max_date = CALCULATE(MAX(Max_dates[Inspection Date]),ALL(Max_dates))

2. For second recent date- 

second_max_date = CALCULATE(MAX(Max_dates[Inspection Date]),FILTER(ALL(Max_dates),Max_dates[Inspection Date] < [max_date]))

3. Date diff

date_diff = DATEDIFF([second_max_date],[max_date],DAY)

Output:-

Samarth_18_0-1627556830120.png

Thanks,

Samarth

Best Regards,
Samarth

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Connect on Linkedin

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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