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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
JCKong
Frequent Visitor

Difference between rows by Name by date

I can't figure out how to do this in dax column, getting the duration difference from the next row per name. 
The table is already sorted by Name then by shift date then by punch time in ascending order.

JCKong_3-1694114434165.png

 

 

 

1 ACCEPTED SOLUTION

Hi @JCKong 

please try

Duration =
VAR CurrentTime = 'Table'[Punch Time]
VAR CurrentNameDateTable =
CALCULATETABLE (
VALUES ( 'Table'[Punch Time] ),
ALLEXCEPT ( 'Table', 'Table'[Name], 'Table'[Short Date] )
)
VAR TableAfter =
FILTER ( CurrentNameDateTable, 'Table'[Punch Time] > CurrentTime )
VAR NextTime =
MINX ( TableAfter, 'Table'[Punch Time] )
RETURN
COALESCE ( NextTime, CurrentTime ) - CurrentTime

View solution in original post

6 REPLIES 6
JCKong
Frequent Visitor

Hi @tamerj1 ,

Encountered an issue when the time stamp passed the next day, example:

Shift Date: 11/10/2023 Punch Time 1:  11:00 PM
Shift Date: 11/11/2023 Punch Time 2: 12:30 AM

@JCKong 

Ctreate a Punch DateTime column by adding Date and Time then use the following 

Duration =
VAR CurrentTime = 'Table'[Punch Time]
VAR CurrentNameTable =
CALCULATETABLE (
VALUES ( 'Table'[Punch Time] ),
ALLEXCEPT ( 'Table', 'Table'[Name] )
)
VAR TableAfter =
FILTER ( CurrentNameTable, 'Table'[Punch DateTime] > CurrentTime )
VAR NextTime =
MINX ( TableAfter, 'Table'[Punch DateTime] )
RETURN
IF (
'Table'[Activity] = "Logout",
0,
COALESCE ( NextTime, CurrentTime ) - CurrentTime
)

JCKong
Frequent Visitor

Currently I use this but some of the Names doesn't show their duration:

Duration = 
VAR NextRow =
CALCULATE (
SUM (Table[PunchTime]),
FILTER (Table, Table[Index] = EARLIER (Table[Index]) + 1)))
RETURN
IF(Table[Activity] = "Logout", 0 ,  NextRow - Table[PunchTime])

 

Hi @JCKong 

please try

Duration =
VAR CurrentTime = 'Table'[Punch Time]
VAR CurrentNameDateTable =
CALCULATETABLE (
VALUES ( 'Table'[Punch Time] ),
ALLEXCEPT ( 'Table', 'Table'[Name], 'Table'[Short Date] )
)
VAR TableAfter =
FILTER ( CurrentNameDateTable, 'Table'[Punch Time] > CurrentTime )
VAR NextTime =
MINX ( TableAfter, 'Table'[Punch Time] )
RETURN
COALESCE ( NextTime, CurrentTime ) - CurrentTime

Thank you @tamerj1 , this did work without the index column which increases the file size.

JCKong
Frequent Visitor

Used a measurement to Sum(Table[Duration]) as the Duration column above only shows earliest and there's no selection for SUM.

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.