Forum Discussion

MuppetyMe's avatar
MuppetyMe
Helper I
1 year ago
Solved

Assistance identifying earliest date, method, and finding difference between 2 dates

I have a dataset with 4 columns in a single table; a unique identifier (ID), create date, and 2 date columns with different contact methods. Here's what I want to accomplish: 


1) Identify which contact method (A or B) was the first/earliest.

* If neither, "No Contact"

* If A was first, "Contact A"

* If B was first, "Contact B"

2) Calculate the difference (in days and hours) between the create date and the earliest Contact Method. If no contact, "No Contact".

IDCreated DateContact A DateContact B Date
ABC1231/4/20241/11/20241/7/2024
ABC1241/4/20251/5/20251/6/2025
ABC1251/6/20251/9/2025 
ABC1261/27/20252/3/20253/16/2025
ABC1271/31/2025  
ABC1282/6/2025 2/8/2025
ABC1292/8/20252/9/2025 
ABC1302/9/2025 2/11/2025
ABC1312/12/20252/15/2025 
ABC1322/12/2025 2/14/2025
ABC1332/16/2025 2/18/2025
ABC1343/1/20253/2/2025 
ABC1353/1/2025 3/4/2025
ABC1363/3/20253/10/2025 
ABC1373/13/20253/16/2025 
ABC1383/13/2025 3/16/2025
ABC1393/30/20253/31/2025 
ABC1404/1/20254/8/20254/3/2025


Any assistance you can provide would be helpful. Thanks!

  • MuppetyMe  Create a new column that identifies the earliest contact method:

    Earliest Contact =
    VAR ContactADate = IF(ISBLANK('Table'[Contact A Date]), DATE(9999, 12, 31), 'Table'[Contact A Date])
    VAR ContactBDate = IF(ISBLANK('Table'[Contact B Date]), DATE(9999, 12, 31), 'Table'[Contact B Date])
    RETURN
    IF(
    ISBLANK('Table'[Contact A Date]) && ISBLANK('Table'[Contact B Date]),
    "No Contact",
    IF(
    ContactADate < ContactBDate,
    "Contact A",
    "Contact B"
    )
    )

     

    Then create a new column that calculates the difference in days and hours:

    DAX
    Contact Difference =
    VAR EarliestContactDate =
    SWITCH(
    'Table'[Earliest Contact],
    "Contact A", 'Table'[Contact A Date],
    "Contact B", 'Table'[Contact B Date],
    BLANK()
    )
    RETURN
    IF(
    ISBLANK(EarliestContactDate),
    "No Contact",
    VAR DiffDays = DATEDIFF('Table'[Created Date], EarliestContactDate, DAY)
    VAR DiffHours = DATEDIFF('Table'[Created Date], EarliestContactDate, HOUR) - (DiffDays * 24)
    RETURN DiffDays & " days " & DiffHours & " hours"
    )

3 Replies

  • MuppetyMe  Create a new column that identifies the earliest contact method:

    Earliest Contact =
    VAR ContactADate = IF(ISBLANK('Table'[Contact A Date]), DATE(9999, 12, 31), 'Table'[Contact A Date])
    VAR ContactBDate = IF(ISBLANK('Table'[Contact B Date]), DATE(9999, 12, 31), 'Table'[Contact B Date])
    RETURN
    IF(
    ISBLANK('Table'[Contact A Date]) && ISBLANK('Table'[Contact B Date]),
    "No Contact",
    IF(
    ContactADate < ContactBDate,
    "Contact A",
    "Contact B"
    )
    )

     

    Then create a new column that calculates the difference in days and hours:

    DAX
    Contact Difference =
    VAR EarliestContactDate =
    SWITCH(
    'Table'[Earliest Contact],
    "Contact A", 'Table'[Contact A Date],
    "Contact B", 'Table'[Contact B Date],
    BLANK()
    )
    RETURN
    IF(
    ISBLANK(EarliestContactDate),
    "No Contact",
    VAR DiffDays = DATEDIFF('Table'[Created Date], EarliestContactDate, DAY)
    VAR DiffHours = DATEDIFF('Table'[Created Date], EarliestContactDate, HOUR) - (DiffDays * 24)
    RETURN DiffDays & " days " & DiffHours & " hours"
    )

    • lbendlin's avatar
      lbendlin
      Super User

      Please don't use DATE(9999, 12, 31) - it puts unnecessary burden on the data model especially when Auto Date/Time is enabled. Better to leave it BLANK().

       

      Consider using MIN and COALESCE.