Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

DAX measure for comparing rows and time difference

Hi,

I'm trying to build a measure (called Time Diff) that compares two consecutive rows, in this case the rows in the Time column. As shown in the column on the far right, I want the output to be in seconds, i.e. between row 1 and 2 it's 36 seconds, between row 2 and row 3 it's 53 seconds and so on.

If possible, I want to achieve this result by using a measure, not a calculated column. I've read about how to solve similar problems by creating a calcualted column using the EARLIER function.

 

Thanks in advance!

/Ella

 

  • Anonymous create this measure:

     

    Time Diff = 
    VAR _current_time = SELECTEDVALUE('Table'[Time])
    VAR _previous_time = 
        MAXX(
            FILTER(
                ALL('Table'),
                'Table'[Time] < _current_time
            ),
            'Table'[Time]
        )
    VAR _result = (_current_time - _previous_time) * 86400
    RETURN
        IF(
            NOT ISBLANK(_previous_time), 
            _result
        )

     





          

    Showcase Report – Contoso By SpartaBI

5 Replies

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

    Anonymous create this measure:

     

    Time Diff = 
    VAR _current_time = SELECTEDVALUE('Table'[Time])
    VAR _previous_time = 
        MAXX(
            FILTER(
                ALL('Table'),
                'Table'[Time] < _current_time
            ),
            'Table'[Time]
        )
    VAR _result = (_current_time - _previous_time) * 86400
    RETURN
        IF(
            NOT ISBLANK(_previous_time), 
            _result
        )

     





          

    Showcase Report – Contoso By SpartaBI

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for your answer! Almost there... 🙂 _previous_time returns _current_time - 1 sec, i.e. the previous timestamp. For example _previous_time for Time 07:06:13 becomes 07:06:12.

      • Anonymous's avatar
        Anonymous
        Not applicable

        Solved it! Just replaced the ALL function with ALLSELECTED. Thanks for your help SpartaBI ğŸ™‚