PowerBI how to calculate response time with DAX?

Viewed 47

For example:

1.7.2021 10.49.03 TAK001 received a message from VEH270 with MessageId "AS8PR01M" 3.7.2021 11.58.03 TAK001 replied for this message as AS8PR01M is found from References column and TAK001 is sender and DisplayTo is including VEH270.

I would need to calculate datedifference between these to dates. Could someone help to solve this, thanks?

Sender  DisplayTo         DateTimeSent          DateTimeReceived    MessageId   References
TAK001  TAM210            28.10.2021 11.31.48   28.10.2021 11.31.50 AM7PR01M    HE1PR0101
TAM210  TAK001            18.10.2021 11.24.18   18.10.2021 11.24.19 HE1PR0101   
TAK001  VEH270, TAA770    3.7.2021 11.58.03     3.7.2021 11.58.05   DU2PR01M    AS8PR01M
VEH270  TAK001            1.7.2021 10.48.59     1.7.2021 10.49.03   AS8PR01M    
2 Answers

If I understand correctly, based on your example table: when there is a value in the References column, it means that this row is the reply to the referenced MessageID.

The most difficult thing based on your example is the handling or the time strings. 28.10.2021 11.31.48 cannot be handled as date/time out of the box.

So, here is the DAX for a calculated column to calculate the response time, in minutes:

ResponseTime =

VAR Date1_String = 
MINX(
    CALCULATETABLE(
        Table,
        [Reference] = EARLIER([MessageId])
    ),
    [DateTimeReceived]
)

VAR Date2_String = [DateTimeSent]

VAR Date1_Date = 
FORMAT(
    DATEVALUE(
        SUBSTITUTE(
            LEFT(
                Date1_String, 
                FIND(" ", Date1_String)
            ),
            ".",
            "/"
        )
    ),
    "dd/mm/yyyy"
) + 
TIMEVALUE(
    SUBSTITUTE(
        RIGHT(
            Date1_String, 
            FIND(" ", Date1_String)
        ),
        ".",
        ":"
    )
)

VAR Date2_Date = 
FORMAT(
    DATEVALUE(
        SUBSTITUTE(
            LEFT(
                Date2_String, 
                FIND(" ", Date2_String)
            ),
            ".",
            "/"
        )
    ),
    "dd/mm/yyyy"
) + 
TIMEVALUE(
    SUBSTITUTE(
        RIGHT(
            Date2_String, 
            FIND(" ", Date2_String)
        ),
        ".",
        ":"
    )
)

RETURN
IF(
    NOT ISBLANK([References]),
    DATEDIFF(
        Date1_Date,
        Date2_Date,
        MINUTE
    ),
    BLANK()
)

The code introduces a few variables :

  • Date1_String retrieves the DateTimeReceived from the initial message searching for the message with the MessageId in the current Reference column.
  • Date2_String is the DateTimeSent from the current line. i.e. the reply.
  • Date1_Date and Date2_Date is a (slightly convoluted) way to parse the string into a workable date/time format.

After the RETURN, a simple IF statement than calculate the time difference in minutes between the two date if only the column References is not empty.

I think I got this at least someway sorted out:

Response time =

VAR Responseday = CALCULATE(

MIN(Sheet1[DateTimeReceived]),

FILTER(Sheet1,[References] = EARLIER([MessageId])) )

VAR Receiveday = [DateTimeReceived]

RETURN

DATEDIFF(Receiveday,Responseday,DAY)
Related