Forum Discussion

luciasolda's avatar
luciasolda
Regular Visitor
1 year ago
Solved

Time between dates for each unique identifier - several date variables

Hi

I’m new to Power BI and having some troubles coming up with a way to calculate the average number of days passed between two dates with the data below, for a case management DB. The difference between the dates needs to be calculated in two different ways, considering closed cases only:

  • difference between the “initial_contact_date” for cases that were opened and closed on the same day (“Survivor_closed_case”=”yes”), which means the case only has one submission/row. In this case the difference will be zero as it is the same day. See for example "editedorg1-30".
  • difference between the “initial_contact_date” in the first submission and the “Follow_up_date” of the last submission/row, for cases when the latter was closed (“Survivor_closed_case”=”yes”)‎

Description of data:

  • One row = one case submitted
  • Each row has a unique identifier (“Unique_ID_merged”)
  • Each case (identified by “Unique_ID_merged”) may have one or multiple rows_
    • Each case will have the first submission (“New_or_registered”=“new_case”). If the case is closed, it will take the value “yes” to the “Survivor_closed_case”). This type of submission will have a date as value for “initial_contact_date”). The date of case closure will be the same as “initial_contact_date”. See "org2-30" in the data
    • Each case may have follow-up submissions (“New_or_registered”= “tracking_existing case”) and, if the case is closed, it will take the value “yes” to the “Survivor_closed_case”). For these submissions, we only have a date value as the “Follow_up_date”, and we can only know the “initial_contact_date” from the first submission, linking them through the “Unique_ID_merged”. See "org1-16" in the data.

This will help us know how long, on average, it takes to close one case of violence once the survivor has decided to close it. Thanks 😍 After this, I will try to calculate the average number of days a case has been open, for cases that have not been closed yet (see "org3-10" in the data)

 

Unique_ID_mergedNew_or_registeredInitial_contact_dateFollow_up_dateSurvivor_closed_case
org3-10new_case20/08/2024nullno
org3-10tracking_existing_casenull21/08/2024no
org1-16new_case27/08/2024nullno
org1-16tracking_existing_casenull28/08/2024no
org1-16tracking_existing_casenull30/08/2024yes
org2-30new_case06/09/2024nullyes
  • AvgDaysToClose=

    Averagex(

    Values( datum[Unique_ID_merged] ),

    Var initialContact =

    Calculate( min( datum[initial_contact_date ))

    Var closedInitialContact =

    Calculate(

    min( datum[inital_contact_date),

    Datum[Survivor_closed_case] = "yes"

    )

    Var lastFollowup =

    Calculate(

    Max( datum[follow_up_date),

    Datum[Survivor_closed_case] = "yes"

    )

    Return 

    Datediff(initialContact, coalesce (closedInitialContact, lastFollowup ))

    )

  • Hi, many thanks! Just a couple of tweaks, in particular, the third argument for DATEDIFF. Here is what worked!

     

    Avgdaysclosure =
    AVERAGEX(
        VALUES('DB'[Unique_ID_merged]),
        VAR initialContact =
            CALCULATE(MIN('DB'[initial_contact_date]))
        VAR closedInitialContact =
            CALCULATE(
                MIN('DB'[initial_contact_date]),
                'DB'[Survivor_closed_case] = "yes"
            )
        VAR closedLastFollowup =
            CALCULATE(
                MAX('DB'[follow_up_date]),
                'DB'[Survivor_closed_case] = "yes"
            )
        RETURN
            DATEDIFF(initialContact, COALESCE(closedInitialContact, closedLastFollowup),DAY)
    )

3 Replies

  • Deku's avatar
    Deku
    Icon for Super User rankSuper User

    AvgDaysToClose=

    Averagex(

    Values( datum[Unique_ID_merged] ),

    Var initialContact =

    Calculate( min( datum[initial_contact_date ))

    Var closedInitialContact =

    Calculate(

    min( datum[inital_contact_date),

    Datum[Survivor_closed_case] = "yes"

    )

    Var lastFollowup =

    Calculate(

    Max( datum[follow_up_date),

    Datum[Survivor_closed_case] = "yes"

    )

    Return 

    Datediff(initialContact, coalesce (closedInitialContact, lastFollowup ))

    )

  • luciasolda's avatar
    luciasolda
    Regular Visitor

    Hi, many thanks! Just a couple of tweaks, in particular, the third argument for DATEDIFF. Here is what worked!

     

    Avgdaysclosure =
    AVERAGEX(
        VALUES('DB'[Unique_ID_merged]),
        VAR initialContact =
            CALCULATE(MIN('DB'[initial_contact_date]))
        VAR closedInitialContact =
            CALCULATE(
                MIN('DB'[initial_contact_date]),
                'DB'[Survivor_closed_case] = "yes"
            )
        VAR closedLastFollowup =
            CALCULATE(
                MAX('DB'[follow_up_date]),
                'DB'[Survivor_closed_case] = "yes"
            )
        RETURN
            DATEDIFF(initialContact, COALESCE(closedInitialContact, closedLastFollowup),DAY)
    )
  • luciasolda's avatar
    luciasolda
    Regular Visitor

    Thanks so much for your help. I wanted to do a similar thing for the cases that remained open so both 'DB'[initial_contact_date]), 'DB'[Survivor_closed_case] = "no" AND 'DB'[follow_up_date]),   'DB'[Survivor_closed_case] = "no"), using TODAY(). 

    Does the below make sense? Many thanks again!

     

    Avgdaysopen = 
    AVERAGEX(
        FILTER(
            'DB',
            'DB'[Survivor_closed_case] = "no"
        ),
        VAR OpenInitialContact =
            CALCULATE(
                MIN('DB'[initial_contact_date])
            )
    
        RETURN
            DATEDIFF(OpenInitialContact, TODAY(), DAY)
    )