Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ellac
Helper III
Helper III

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.

ellac_1-1658156832946.png

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

 

1 ACCEPTED SOLUTION
SpartaBI
Community Champion
Community Champion

@ellac 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
    )

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

@ellac 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/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
SpartaBI
Community Champion
Community Champion

@ellac 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
    )

 


2022-05-19 17_30_22-Re_ Need help on DAX function with measure vs colu... - Microsoft Power BI Commu.png


Full-Logo11.png

SpartaBI_3-1652115470761.png   SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Showcase Report – Contoso By SpartaBI

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.

Solved it! Just replaced the ALL function with ALLSELECTED. Thanks for your help @SpartaBI 🙂

SpartaBI
Community Champion
Community Champion

@ellac my pleasure 🙂
Check out my showcase report - got some high level stuff there. Sure you will find there a lot of cool ideas. 
https://community.powerbi.com/t5/Data-Stories-Gallery/SpartaBI-Feat-Contoso-100K/td-p/2449543
Give it a thumbs up over there if you liked it 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.