Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Calculating duration between two dates in different rows based on duplicate value

Hi, I want to calculate the duration (# of days) between two fields based on the same value.

 

For example, I have data:

 

Case_IDCreated DateSent DateAccount_IDDuplicates
3033/1/20203/16/202041235960
3253/2/20203/13/202038003020
3282/1/20203/2/202034510870
3352/12/20203/3/202040836960
3363/1/20203/11/202033209160
3372/8/20203/17/202011835460
3402/9/20203/9/202037974730
3412/10/20203/3/202029216600
3442/11/20203/3/202011054870
3492/12/20203/25/20201844700
3502/13/20203/16/202021341530
3542/14/20203/2/202038048590
3552/15/20203/3/202026991550
3562/16/20203/3/202026504620
3572/17/20203/16/202026588590
3582/18/20203/12/202028273330
3632/19/20203/23/202030810940
3662/20/20203/20/202030566380
3472/21/20203/2/202037886460
3602/22/20203/4/202032099030
3642/23/20203/20/202037972300
3682/24/20203/30/202031981900
3622/25/20203/18/202032947491
3613/22/20203/28/202032947491
3672/27/20203/19/202031489211
3693/22/20203/28/202031489211

 

 

The duplicates show that there are two different cases for the same account. I would like to find out if the account is the same then what is the duration between when the case was created on the second case - report sent date for the first case. The order will be based on the created date. The goal is to understand if the same account is coming back to create another case within 90 days of when their report was sent.

 

 

For example:

Account 3294749

1st Case = 362 (created on 2/25, report sent 3/18/2020)

2nd Case = 361 (created on 3/22, report sent 3/28/2020)

 

Expected Outcome (new column):

361 Created Date - 362 Report Sent Date = 4 days

 

Thank you!

 

 

  • This calculated column will do the trick:

     

    Date Difference =
    VAR CurrentAccount = [Account_ID]
    VAR CurrentRecordSet =
        FILTER(
            'DateDiff Table',
            'DateDiff Table'[Account_ID] = CurrentAccount
        )
    VAR AccountCount =
        COUNTX(
            CurrentRecordSet,
            'DateDiff Table'[Account_ID]
        )
    VAR CreatedDate =
        IF(
            AccountCount > 1,
            MAXX(
                CurrentRecordSet,
                'DateDiff Table'[Created Date]
            ),
            0
        )
    VAR SentDate =
        IF(
            AccountCount > 1,
            MINX(
                CurrentRecordSet,
                'DateDiff Table'[Sent Date]
            ),
            0
        )
    VAR DateDifference =
        DATEDIFF(
            SentDate,
            CreatedDate,
            DAY
        )
    RETURN
        DateDifference
    

     

    It returns zero if there is only one record for an account.

     

    If you want it to return the dates between sent and created even if there is only one record, get rid of the IF() function. So for example:

    VAR SentDate =
        IF(
            AccountCount > 1,
            MINX(
                CurrentRecordSet,
                'DateDiff Table'[Sent Date]
            ),
            0
        )

    becomes

    VAR SentDate =
            MINX(
                CurrentRecordSet,
                'DateDiff Table'[Sent Date]
            )

     

    Same logic for the CreatedDate variable. You didn't specify in your OP, so wasn't sure how you wanted those handled.

5 Replies

  • edhans's avatar
    edhans
    Community Champion

    This calculated column will do the trick:

     

    Date Difference =
    VAR CurrentAccount = [Account_ID]
    VAR CurrentRecordSet =
        FILTER(
            'DateDiff Table',
            'DateDiff Table'[Account_ID] = CurrentAccount
        )
    VAR AccountCount =
        COUNTX(
            CurrentRecordSet,
            'DateDiff Table'[Account_ID]
        )
    VAR CreatedDate =
        IF(
            AccountCount > 1,
            MAXX(
                CurrentRecordSet,
                'DateDiff Table'[Created Date]
            ),
            0
        )
    VAR SentDate =
        IF(
            AccountCount > 1,
            MINX(
                CurrentRecordSet,
                'DateDiff Table'[Sent Date]
            ),
            0
        )
    VAR DateDifference =
        DATEDIFF(
            SentDate,
            CreatedDate,
            DAY
        )
    RETURN
        DateDifference
    

     

    It returns zero if there is only one record for an account.

     

    If you want it to return the dates between sent and created even if there is only one record, get rid of the IF() function. So for example:

    VAR SentDate =
        IF(
            AccountCount > 1,
            MINX(
                CurrentRecordSet,
                'DateDiff Table'[Sent Date]
            ),
            0
        )

    becomes

    VAR SentDate =
            MINX(
                CurrentRecordSet,
                'DateDiff Table'[Sent Date]
            )

     

    Same logic for the CreatedDate variable. You didn't specify in your OP, so wasn't sure how you wanted those handled.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi edhans ! Thank you for your reply, your column has exactly what I am looking for. I'm not sure why I am getting the 'Token Eof Expected' error where the CurrentAccount expression is. Any thoughts?

       

      VAR CurrentAccount = [Account_ID]

       

      Here are the data types that I am using:

       

      Case_ID = Text

      Account_ID = Text

      Sent Date = Date

      Report Date = Date

       

      Thank you again!

      Jenny

      • amitchandak's avatar
        amitchandak
        Super User

        Anonymous , See if this can help

        Working Days = CALCULATE(DISTINCTCOUNT('Date'[Working date]),VALUES(Sheet1),filter(all('Date'),'Date'[Date]>=[Min Created Date] && 'Date'[Date] <=[Max Send date]),NOT(ISBLANK('Date'[Working date])))

         

        File attached after signature