Forum Discussion

alam6's avatar
alam6
Regular Visitor
4 years ago
Solved

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...
  • Anonymous's avatar
    Anonymous
    4 years ago

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

     

     

    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:

     

    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.