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
STS_Joshua
Helper II
Helper II

DateDiff between filtered rows

Hi All,

 

I have a table like so:

TransIDModAtModByEventID
11:27:20 PMBobPick
11:26:59 PMBobPick
21:26:48 PMJillPick
31:26:24 PMHarryPick
21:25:39 PMJillPick
31:24:59 PMHarryPick
11:24:09 PMBobPick
21:23:27 PMJillPick
31:20:51 PMHarryPick

 

 

What I want to show is the time differences between each row with that changing dynamically as the table is filtered like so:

TransIDModAtModByEventIDTimeDiff(In Seconds)
11:27:20 PMBobPick21
11:26:59 PMBobPick11
21:26:48 PMJillPick24
31:26:24 PMHarryPick45
21:25:39 PMJillPick40
31:24:59 PMHarryPick50
11:24:09 PMBobPick42
21:23:27 PMJillPick156
31:20:51 PMHarryPick0

 

 

Then Filtered:

TransIDModAtModByEventIDTimeDiff(In Seconds)
11:27:20 PMBobPick21
11:26:59 PMBobPick170
11:24:09 PMBobPick0

 

 

Thanks!

2 ACCEPTED SOLUTIONS
jdbuchanan71
Super User
Super User

Hello @STS_Joshua 

Give this a try.

TimeDiff(In Seconds) = 
VAR CurrentRowTime = SELECTEDVALUE ( 'Table'[ModAt] )
VAR TimeSec = 
DATEDIFF (
    CALCULATE ( MAX ( 'Table'[ModAt] ),
        ALLSELECTED ( 'Table' ),
        'Table'[ModAt] < CurrentRowTime ),
    CurrentRowTime, SECOND )
RETURN
    IF ( ISBLANK( TimeSec ), 0, TimeSec )

DateDiffSec.jpg

View solution in original post

That's odd, the same change works for me:

TimeDiff(In Seconds) TransID = 
VAR CurrentRowTime = SELECTEDVALUE ( 'Table'[ModAt] )
VAR TransID = SELECTEDVALUE ( 'Table'[TransID] )
VAR TimeSec = 
DATEDIFF (
    CALCULATE ( MAX ( 'Table'[ModAt] ),
        ALLSELECTED ( 'Table' ),
        'Table'[ModAt] < CurrentRowTime,
        'Table'[TransID] = TransID ),
    CurrentRowTime, SECOND )
RETURN
    IF ( ISBLANK( TimeSec ), 0, TimeSec )

DateDiffSecGrouped.jpg

View solution in original post

6 REPLIES 6
jdbuchanan71
Super User
Super User

Hello @STS_Joshua 

Give this a try.

TimeDiff(In Seconds) = 
VAR CurrentRowTime = SELECTEDVALUE ( 'Table'[ModAt] )
VAR TimeSec = 
DATEDIFF (
    CALCULATE ( MAX ( 'Table'[ModAt] ),
        ALLSELECTED ( 'Table' ),
        'Table'[ModAt] < CurrentRowTime ),
    CurrentRowTime, SECOND )
RETURN
    IF ( ISBLANK( TimeSec ), 0, TimeSec )

DateDiffSec.jpg

Thanks for the answer, I have a followup though. Is there a way to display the DateDiff grouped for each order when unfiltered? Like so:

 

TransIDModAtModByEventIDTimeDiff(In Seconds)
11:27:20 PMBobPick21
11:26:59 PMBobPick170
21:26:48 PMJillPick24
31:26:24 PMHarryPick45
21:25:39 PMJillPick132
31:24:59 PMHarryPick248
11:24:09 PMBobPick0
21:23:27 PMJillPick0
31:20:51 PMHarryPick0

 

I tried changing your calculation some but did not get what I wanted:

TimeDiff = 
VAR CurrentRowTime = SELECTEDVALUE ( 'Table'[ModAt] )
VAR CurrentTransID = SELECTEDVALUE( 'Table'[TransID])
VAR TimeSec = 
DATEDIFF (
    CALCULATE ( MAX ('Table'[ModAt]),
        'Table'[TransID]=CurrentTransID,
        'Table'[ModAt] < CurrentRowTime ),
    CurrentRowTime, SECOND )
RETURN
    IF ( ISBLANK( TimeSec ), 0, TimeSec )

 

 

That's odd, the same change works for me:

TimeDiff(In Seconds) TransID = 
VAR CurrentRowTime = SELECTEDVALUE ( 'Table'[ModAt] )
VAR TransID = SELECTEDVALUE ( 'Table'[TransID] )
VAR TimeSec = 
DATEDIFF (
    CALCULATE ( MAX ( 'Table'[ModAt] ),
        ALLSELECTED ( 'Table' ),
        'Table'[ModAt] < CurrentRowTime,
        'Table'[TransID] = TransID ),
    CurrentRowTime, SECOND )
RETURN
    IF ( ISBLANK( TimeSec ), 0, TimeSec )

DateDiffSecGrouped.jpg

Sorry, but one last question. Is there a way to do that as a calculated column vs. a measurement? 

Sure, you can just add a column to your table and make it = the measure.

Column 2 = [TimeDiff(In Seconds) TransID]

DateDiffSecColumn.jpg

My mistake was replacing your ALLSELECTED filter in CALCULATE. With that in there it works. Thanks!

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.