Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Calculating time difference between rows with slicers

I have a DAX formula that is calculating the time difference between each consecutive row in a table visulaization. It works when I have only one process selected, but when I select multiple processes the Time Diff measure is incorrect. See images below.

 

DAX formula: 

Time Diff = VAR Previous = CALCULATE ( MAX ('Main Table'[Date.Time] ), FILTER ( ALLSELECTED ( 'Main Table' ), 'Main Table'[Date.Time] < MAX ( 'Main Table'[Date.Time] ) ) ) RETURN DATEDIFF ( Previous, MAX ( 'Main Table'[Date.Time] ), MINUTE )

 

One selectionTwo selection

  • It looks like it is correctly calculating the difference between rows (regardless of the Process).  If you'd like to keep the Process in context, you need to add VALUES('Main Table'[Process]) to your Calculate().

     

    If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

5 Replies

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft Employee

    It looks like it is correctly calculating the difference between rows (regardless of the Process).  If you'd like to keep the Process in context, you need to add VALUES('Main Table'[Process]) to your Calculate().

     

    If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

    Regards,

    Pat

    • Anonymous's avatar
      Anonymous
      Not applicable

      That works! Thank you

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    Anonymous Seems like the issue is that when you are getting the MAX, it is pulling for Line 11 or basically the wrong thing and this is likely because of your ALLSELECTED which I try not to use if at all possible and not sure why you need it. I would start by dropping it.

     

    This post may help. See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586

     

    I have a great version of that in my book, DAX Cookbook.

     

    Also, if you can paste some of your source data or sample source data I can probably adapt the formula from my book for you. 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.

  • Greg_Deckler's avatar
    Greg_Deckler
    Icon for Community Champion rankCommunity Champion

    I would start by dropping your ALLSELECTED and not using CALCULATE.

     

    See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/339586

     

    I have a great version of that in my book, DAX Cookbook. If you can post sample data, I'll try to adapt it for your situation. 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

     

     

    Hi Anonymous ,
     
    Incase you want difference between each row
     

    Time Difference =

    var _a = MAX('Table'[Date/Time])
    var _previousdate = CALCULATE(MAX('Table'[Date/Time]), FILTER(ALLSELECTED('Table'[Date/Time]),'Table'[Date/Time] < _a) )

    RETURN
    CALCULATE(DATEDIFF(_previousdate,_a,MINUTE))


     
     
    This measure too gives the same results.
     
    Time Difference =
    VAR _a =
    MAX ( 'Table'[Date/Time] )
    VAR _previousdate =
    CALCULATE (
    MAX ( 'Table'[Date/Time] ),
    FILTER (
    ALLEXCEPT('Table', 'Table'[Process])
    ,
    'Table'[Date/Time] < _a
    )
    )
    RETURN
    DATEDIFF (
    _previousdate,
    _a,
    MINUTE
    )
     
    Regards,
    Harsh Nathani
    Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)