Search code examples
powerbidaxmeasure

Difference between two timestamps in hours


I need to create a DAX measure to calculate the difference between two date time columns in hours

Example:

Column 1 (input): 2021-02-09 19:30:02

Column 2 (input): 2021-02-10 22:00:02

Difference in hours (output): 26.30


Solution

  • The internal representation of DATETIME is like in Excel, using the number of days elapsed. So to get the difference in hours is enough to compute the difference between the datetimes and multiply by 24 hours.

    this is an example running on dax.do

    DEFINE
        TABLE T =
            DATATABLE (
                "DateTimeStart", DATETIME,
                "DateTimeEnd", DATETIME,
                {
                    { "2021-02-09 19:30:02", "2021-02-10 22:00:02" }
                }
            )
    EVALUATE
    ADDCOLUMNS
    (
        T,
        "Difference Hours", (T[DateTimeEnd] - T[DateTimeStart]) * 24
    )
    

    and this is the result

    the result