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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
WorkHard
Helper V
Helper V

Date Difference between two values in the same column with same Id

I'm trying to calculate the difference in days between two values in the same column that have the same id.

Consider the following data:

-EventId--Start Date--Desired Result-
Event11/15/2021 
Event13/15/202159
Event13/17/20212
Event23/1/2021 
Event22/1/2021-28

 

I feel like I have the formula right in this calculated column but for some reason all the results are blank.

 

 

 

 

 

c_DateDiff = 
VAR temp =
    TOPN (
        1,
        FILTER (
            datasource,
            datasource[EventId] = EARLIER ( datasource[EventId] )
                && datasource[Start Date] < EARLIER ( datasource[Start Date] )
        ),
        [End Date], DESC
    )
RETURN
    DATEDIFF ( MINX ( temp, [Start Date] ), datasource[Start Date], DAY )

 

 

 

 

 

Edit: What if I want to calculate the Start Date in relation to the previous/earlier snapshot date?

-EventId--StartDate--Snapshot Date--Desired Result-
Event11/15/20211/1/2021 
Event13/15/20213/1/2021-2
Event13/17/20212/1/202161
Event23/1/20211/1/2021 
Event22/1/20213/1/2021-28
1 ACCEPTED SOLUTION
v-janeyg-msft
Community Support
Community Support

Hi, @WorkHard 

 

You can create a column to calculate the dasired result.

Like this:

 

column = 
VAR a =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            [-Snapshot Date-] < EARLIER(  'Table'[-Snapshot Date-] )
                && [-EventId-] = EARLIER(  'Table'[-EventId-] )
        ),
        [-Snapshot Date-]
    )
VAR b =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            [-Snapshot Date-] = a
                && [-EventId-] = EARLIER(  'Table'[-EventId-] )
        ),
        [-StartDate-]
    )
RETURN
    DATEDIFF ( b, 'Table'[-StartDate-] , DAY )

 

7.png

If it doesn’t solve your problem, please feel free to ask me.

 

Best Regards

Janey Guo

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

7 REPLIES 7
v-janeyg-msft
Community Support
Community Support

Hi, @WorkHard 

 

You can create a column to calculate the dasired result.

Like this:

 

column = 
VAR a =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            [-Snapshot Date-] < EARLIER(  'Table'[-Snapshot Date-] )
                && [-EventId-] = EARLIER(  'Table'[-EventId-] )
        ),
        [-Snapshot Date-]
    )
VAR b =
    MAXX (
        FILTER (
            ALL ( 'Table' ),
            [-Snapshot Date-] = a
                && [-EventId-] = EARLIER(  'Table'[-EventId-] )
        ),
        [-StartDate-]
    )
RETURN
    DATEDIFF ( b, 'Table'[-StartDate-] , DAY )

 

7.png

If it doesn’t solve your problem, please feel free to ask me.

 

Best Regards

Janey Guo

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

v-janeyg-msft
Community Support
Community Support

Hi, @WorkHard 

 

Have your problem been solved? I can see from your dax code that you have two tables, but they don't appear in your description, such as 'end date' and 'snapshot date', You need to provide more detail information and the final result you want, then we can help you soon.

 

Best Regards

Janey Guo

Ashish_Mathur
Super User
Super User

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


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

Thanks. I was hoping for a DAX formula but I'll try to do this in Power Query, like you did. 


What if I want to calculate the Start Date in relation to the previous snapshot date?
Check OP for an example.

You are welcome.  How did you arrive at the numbers in the Desired result column of the second table in your original post?  I am not clear.


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

Hi @Ashish_Mathur ,


The 3 rows for Event1 come from different points in time. I take a "snapshot" of the data once a month and then every month I add another row for the same event. So over time, the Start Date of the event can change. This gives me the ability to see how the start date for the same event changes every month.

The Start Date for Event1 in the snapshot from January 2021 was January 15.

The Start Date for Event 1 in the snapshot from February 2021 was March 17 (61 days).

The Start Date for Event 1 in the snapshot from March 2021 was March 15 (-2 days)

 

The Start Date for Event 2 in the snapshot from January 2021 was March 1.

The Start Date for Event 2 in the snapshot from March 2021 was February 1. (+28 days)

Hi,

This calculated column formula works

if(ISBLANK(LOOKUPVALUE(Data[StartDate],Data[EventId],Data[EventId],Data[Snapshot Date],CALCULATE(MAX(Data[Snapshot Date]),FILTER(Data,Data[EventId]=EARLIER(Data[EventId])&&Data[Snapshot Date]<EARLIER(Data[Snapshot Date]))))),BLANK(),1*(Data[StartDate]-LOOKUPVALUE(Data[StartDate],Data[EventId],Data[EventId],Data[Snapshot Date],CALCULATE(MAX(Data[Snapshot Date]),FILTER(Data,Data[EventId]=EARLIER(Data[EventId])&&Data[Snapshot Date]<EARLIER(Data[Snapshot Date]))))))

Hope this helps.

Untitled.png


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

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.