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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
alam6
Regular Visitor

Date difference in same column with duplicate dates

Hi,

I have an Excel table ("Site Table") below of (1) location names, (2) dates of visits, and (3) difference between visits. Each visit has its own row/entry. I'm trying to calculate the difference (days) of visits for each site, and was able to do this in Excel in the "Date Difference" column:

alam6_0-1655400623451.png

 

When I try to replicate in Power BI for Site 1, I cannot get the 0 to show up in the second record for 8/13/2021. Rather than subtracting from the previous entry (the first record for Aug 13, 2021), it subtracts from the most recent date that is before Aug 13, 2021 (which is June 25, 2021):

alam6_1-1655400623454.png

This is my current DAX formula (from online video):

 

 

Date Diff =

VAR FirstDateSub =

CALCULATE(

    MIN('SiteTable'[Completed Date]),

    ALLEXCEPT('SiteTable','SiteTable'[Site Name]))

VAR DemoteSub =

CALCULATE(

    MAX('SiteTable'[Completed Date]),

    FILTER(

        ALLEXCEPT('SiteTable','SiteTable'[Site Name]),

        'SiteTable'[Completed Date]<EARLIER('SiteTable'[Completed Date])))



return

(DATEDIFF(DemoteSub,'SiteTable'[Completed Date],DAY))/12

 

 

Does anyone know how to revise the formula to show the 0? Thank you.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@alam6 , yes that's right, add an Index Column via "Transform Data", then "Add Column", then "Index Column"

EylesIT_0-1655405933838.png

 

 

Then the DAX for the Calculated Column for [Date Diff] should be as per below. The [Index] field used in the Calculated Column DAX must be the name of the index column you created.

 

Date Diff = 
    VAR vCompletedDatePriorRow =
        CALCULATE(
            MAX('Site Table'[Completed Date])
            ,FILTER(ALLEXCEPT('Site Table', 'Site Table'[Site Name])
            ,'Site Table'[Index] < EARLIER('Site Table'[Index])
            )
        )

    RETURN
        DATEDIFF(vCompletedDatePriorRow, 'Site Table'[Completed Date], DAY)

 

 This gives me the correct results:

EylesIT_1-1655406029170.png

 

This will also work when there are 3 or 4 etc visits to a site on the same day.

Bear in mind that for this solution to work correctly with an index column, the spreadsheet must be sorted by SiteName and Completed date. If not, it may not work correctly.

View solution in original post

4 REPLIES 4
alam6
Regular Visitor

Thanks @Anonymous. By sequence number, are you referring to an index column?

Anonymous
Not applicable

@alam6 , yes that's right, add an Index Column via "Transform Data", then "Add Column", then "Index Column"

EylesIT_0-1655405933838.png

 

 

Then the DAX for the Calculated Column for [Date Diff] should be as per below. The [Index] field used in the Calculated Column DAX must be the name of the index column you created.

 

Date Diff = 
    VAR vCompletedDatePriorRow =
        CALCULATE(
            MAX('Site Table'[Completed Date])
            ,FILTER(ALLEXCEPT('Site Table', 'Site Table'[Site Name])
            ,'Site Table'[Index] < EARLIER('Site Table'[Index])
            )
        )

    RETURN
        DATEDIFF(vCompletedDatePriorRow, 'Site Table'[Completed Date], DAY)

 

 This gives me the correct results:

EylesIT_1-1655406029170.png

 

This will also work when there are 3 or 4 etc visits to a site on the same day.

Bear in mind that for this solution to work correctly with an index column, the spreadsheet must be sorted by SiteName and Completed date. If not, it may not work correctly.

This has worked so far, thank you very much @Anonymous!

Anonymous
Not applicable

@alam6 , I think you will need to add a sequence number per site into the table in Power BI, and then use that sequence number field in the DAX measure. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.