Forum Discussion

kumsha1's avatar
kumsha1
Post Patron
6 years ago
Solved

DATEDIFF between previous row date and current row date

Hi,

 

Looking for help on below dataset to calculate DATEDIFF.

For OID: 190235781 the date difference required would be Delay End of previous row (OID-190233439) - current Delay Start (OID-190235781). Thanks in advance !

 

  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi kumsha1 ,

    You can try to below calculate column formula to get last end DateTime and calculate the duration between two DateTime fields:

    hour = 
    VAR diff =
        DATEDIFF (
            CALCULATE (
                MAX ( 'Table'[Delay End] ),
                FILTER (
                    'Table',
                    [Unit] = EARLIER ( 'Table'[Unit] )
                        && [Current Delay Start] < EARLIER ( 'Table'[Current Delay Start] )
                )
            ),
            [Current Delay Start],
            SECOND
        )
    RETURN
        diff / 3600
    

    Regards,

    Xiaoxin Sheng

7 Replies

  • Nathaniel_C's avatar
    Nathaniel_C
    Community Champion

    Hi kumsha1 ,
    Had a hard time seeing your picture, so made a quick table. 

     






     

    Time diff = 
    var _endPresent = MAX('myTable'[end])
    var _begPresent = MAX(myTable[beg])
    var _endPrevious = CALCULATE(MAX('myTable'[end]),'myTable'[end]<_endPresent,ALLEXCEPT(myTable,myTable[end]))
    return DATEDIFF(_endPrevious ,_begPresent,DAY)

     

     This should work for you when you insert your table and column names. Use variables and calculate to solve.
    Let me know if you have any questions.

    If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
    Nathaniel

    • kumsha1's avatar
      kumsha1
      Post Patron

      Thanks for your response.

      Being new to PBI, i am not sure i am doing wrong as the DATEDIFF is giving me wrong values. Below is the actual data for your reference.

      IDStartEndCategoryDescriptionTypeRequired
      3241058554/11/2019 6:00 SDSuper Structure shutdown  
      3242548564/11/2019 21:547/11/2019 11:38UD  DATEDIFF(NULL;4/11/2019 21:54)
      3245314466/11/2019 7:448/11/2019 4:00UD  DATEDIFF(7/11/2019 11:38;6/11/2019 7:44)
      3245320935/11/2019 19:1312/11/2019 14:20SD  DATEDIFF(8/11/2019 4:00;5/11/2019 19:13)
      • Nathaniel_C's avatar
        Nathaniel_C
        Community Champion

        Hi kumsha1 
        Would you please give us an example of expected output?

        Thanks,

        Nathaniel