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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
jobf
Helper I
Helper I

Date difference in a specific location and period

Hi. Please take a look at the table below:

Location  Year  Application  Date
P1  2024  1st application  04/01/2024
P1  2025  1st application  01/02/2025
P1  2024  2nd application  05/20/2024
P1  2025  2nd application  01/21/2025
P2  2024  1st application  05/05/2024
P2  2025  1st application  01/06/2025
P2  2024  2nd application  07/15/2024
P2  2025  2nd application  01/19/2025

 

I need a column or measure that shows the difference (days) between the current application of the year and the most recent date of the last application at a given location of the year. It would look something like this:

Location  Year  Application  DateDiff
P1  2024  1st application  04/01/2024 
P1  2025  1st application  01/02/2025 
P1  2024  2nd application  05/20/202449
P1  2025  2nd application  01/21/202519
P2  2024  1st application  05/05/2024 
P2  2025  1st application  01/06/2025 
P2  2024  2nd application  07/15/202471
P2  2025  2nd application  01/19/202513

If this is the 1st application, it should show a null value. Sometimes, there will be 12 or more applications in the same location and year.

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@jobf You could create a calculated column like the following:

Column = 
  VAR __Location = [Location]
  VAR __Year = [Year]
  VAR __Date = [Date]
  VAR __Table = FILTER( 'Table', [Location] = __Location && [Year] = __Year && [Date] < __Date )
  VAR __Previous = MAXX( __Table, [Date] )
  VAR __Result = ( __Date - __Previous ) * 1.
RETURN
  __Result

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
 ( __Current - __Previous ) * 1.



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

2 REPLIES 2
v-kongfanf-msft
Community Support
Community Support

Hi @jobf ,

 

Maybe you can try formula like below to create calculated column:

DateDiff = 
VAR CurrentDate = 'Table'[Date]
VAR PreviousDate = 
    CALCULATE(
        MAX('Table'[Date]),
        FILTER(
            'Table',
            'Table'[Location] = EARLIER('Table'[Location]) &&
            'Table'[Year] = EARLIER('Table'[Year]) &&
            'Table'[Date] < EARLIER('Table'[Date])
        )
    )
RETURN
IF(
    ISBLANK(PreviousDate),
    BLANK(),
    DATEDIFF(PreviousDate, CurrentDate, DAY)
)

vkongfanfmsft_0-1737599210313.png

 

Best Regards,
Adamk Kong

 

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

Greg_Deckler
Super User
Super User

@jobf You could create a calculated column like the following:

Column = 
  VAR __Location = [Location]
  VAR __Year = [Year]
  VAR __Date = [Date]
  VAR __Table = FILTER( 'Table', [Location] = __Location && [Year] = __Year && [Date] < __Date )
  VAR __Previous = MAXX( __Table, [Date] )
  VAR __Result = ( __Date - __Previous ) * 1.
RETURN
  __Result

See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
 ( __Current - __Previous ) * 1.



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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