Forum Discussion

MH3's avatar
MH3
Helper V
6 years ago
Solved

Time Difference

Hello Everyone.

 

I have two columns Start Time  and End Time and want to calculate difference between two date/time columns but I can only get the column name by using an Aggregation function like Count, Min, Max.

Why is it like that how can I subtract them and get the desired results?

 

my Data Values here:



                         

Ed Time Column

                 Start Time Column





I want to create a measure,  Need Help!

  • Anonymous's avatar
    Anonymous
    6 years ago

    MH3 

    You can still create a measure with sum/max/min, and change the to Time format.

     

    Measure = SUM('Table (2)'[End])-SUM('Table'[Start])

     

     

    If you cannot make the columns in a same table, you should have a relationship between the two tables.

     

    Paul Zheng _ Community Support Team
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

7 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    MH3  - So, if these are in two different tables as I suspect, then there must be a relationship between the tables that joins them correct? And I would also need to make the assumption that there is something in the visual that uniquely identifies rows in the tables in question like an ID or something. In that case, you can do something like:

     

    Measure = DATEDIFF(MAX('Table1'[Start Date]), MAX('Table2'[End Date]),HOUR)

     

    That is not your only option (HOUR):

    https://docs.microsoft.com/en-us/dax/datediff-function-dax

     

    If they are in the same table, then replace 'Table2' with 'Table1', you will still need something in your visual for unique row identification.

     

    If these are columns in the same table and you would like a calculated column instead of a measure:

    Measure = DATEDIFF('Table1'[Start Date], 'Table1'[End Date],HOUR)

     

    Just FYI, you can also do the same calculation as above like this:

    'Table1'[Start Date] - 'Table1'[End Date] * 24

     

    The integer portion of a date/time column is the number of days since a certain point in time (december 30th, 1899 or something like that). The decimal portion is fractions of a day. You can get minutes 24 * 60 or seconds 24 * 60 * 60. 

     

    If this does not work or assumptions are bad please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

    The most important parts are:
    1. Sample data as text, use the table tool in the editing bar
    2. Expected output from sample data
    3. Explanation in words of how to get from 1. to 2.

  • Anonymous's avatar
    Anonymous
    Not applicable

    MH3 

    You can still create a measure with sum/max/min, and change the to Time format.

     

    Measure = SUM('Table (2)'[End])-SUM('Table'[Start])

     

     

    If you cannot make the columns in a same table, you should have a relationship between the two tables.

     

    Paul Zheng _ Community Support Team
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • MH3's avatar
      MH3
      Helper V

      Dear Sir Anonymous amitchandak  Greg_Deckler 

       

      I have used this Formula

      Time DF = SUM('Log Header'[EndTime])-SUM('Log Header'[StartTime])

       

      and my Table Structure is this:


      when I used the formula in the viusalization it shows me bigger values
      Filter is for HOTEL DAVOS, and the Table isSorted for Hotel Davos as Well

      , is it correct or worng I can't verify it please help!

      • amitchandak's avatar
        amitchandak
        Super User

        MH3 , Try like

        Time DF = sumx('Log Header',('Log Header'[EndTime])-('Log Header'[StartTime])

    • Syndicate_Admin's avatar
      Syndicate_Admin
      Administrator

      Hello! to calculate the time difference between two columns, you can use the DATEDIFF function, to calculate it in a new column, which in your case would be:

      Columna_nueva = DATEDIFF(tabla[EndTime], tabla[StartTime], minute)

      and you can change the unit of measurement you want to be returned, in this case "Minute".

      What if, the format of this is as integer or decimal.

      I do not know the method for this to be in time format, I hope it serves, Greetings!