Forum Discussion

android1's avatar
android1
Post Patron
10 years ago

Time difference in hours

Hi,

 

I'm trying to calculate the diiference in hours between two date/time columns. When I use DateDiff I am not able to use the two columns below.

 

13 Replies

  • Sean's avatar
    Sean
    Community Champion

    android1 So you've verified both columns are date/time.

     

    Then maybe you have some start (FROM) times that are after the corresponding end (TO) times.

     

    See if this works - but don't forget that this means some of your data is not in the right order...

     

    Hours = SWITCH( 
                TRUE(), 
                   'Table'[TimeFrom]<'Table'[TimeTo], DATEDIFF('Table'[TimeFrom], 'Table'[TimeTo], HOUR),
                   'Table'[TimeFrom]>'Table'[TimeTo], DATEDIFF('Table'[TimeTo], 'Table'[TimeFrom], HOUR),
                   0 )
    • abodqmm's avatar
      abodqmm
      Regular Visitor

      Dear Sean i tried this method and it worked fine i want to see it in minutes how can i do it ?????

  • Vvelarde's avatar
    Vvelarde
    Community Champion

    DATEDIFF(TablaHoras[TimeFrom];TablaHoras[TimeTo];HOUR)

    • android1's avatar
      android1
      Post Patron

      Thanks for both answers. I'm getting the below error for both;

       

      • Vvelarde's avatar
        Vvelarde
        Community Champion

        DateDiff_Big...In PowerBI this function doesnt work.

         

        Your Measure should be look like this:

         

        MeasureName=DateDiff(xxxxxx,xxxxxx,Hour)

  • Anonymous's avatar
    Anonymous
    Not applicable

    Also can use a measure, i feel little faster loading time with this 


    TimeDiff = CONVERT(SELECTEDVALUE(Table[Date1],0)-SELECTEDVALUE(Table[Date2],0),DATETIME)


    and then change the format to time hh:mm:ss
    Cheers