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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
HenryJS
Post Prodigy
Post Prodigy

Calculate Time Between

Hi all,

 

How can create the column 'Break' below?

 

It calculates the time between the end of the shift and start of the next one. They can be on different dates but have to be the next day worked (date column).

 

This also has to correlate to Timesheet ID.

 

Timesheet IDDateStartEndBreak
14469902/09/202007:3017:000
14469903/09/202007:3017:0014:30
14469906/09/202007:3017:0014:30
14470001/0908:0017:000
14470005/0908:0017:0009:00
1 ACCEPTED SOLUTION
v-lionel-msft
Community Support
Community Support

Hi @HenryJS ,

 

Start_time = 
VAR __start = MAX(Sheet7[Start])
RETURN
DATE(2020, 1, 2) + __start
End_time = 
VAR __end = 
CALCULATE(
    MAX([End]),
    FILTER(
        ALL(Sheet7),
        Sheet7[Timesheet ID] = MAX(Sheet7[Timesheet ID]) && Sheet7[Date] < MAX(Sheet7[Date]) 
    )
)
RETURN
IF(
    __end <> BLANK(),
    DATE(2020,1,1) + __end,
    BLANK()
)
Measure 2 = 
IF(
    [End_time] <> BLANK(),
   [Start_time] - [End_time],
    TIME(0,0,0)
)

v-lionel-msft_0-1599812017116.png

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-lionel-msft
Community Support
Community Support

Hi @HenryJS ,

 

Start_time = 
VAR __start = MAX(Sheet7[Start])
RETURN
DATE(2020, 1, 2) + __start
End_time = 
VAR __end = 
CALCULATE(
    MAX([End]),
    FILTER(
        ALL(Sheet7),
        Sheet7[Timesheet ID] = MAX(Sheet7[Timesheet ID]) && Sheet7[Date] < MAX(Sheet7[Date]) 
    )
)
RETURN
IF(
    __end <> BLANK(),
    DATE(2020,1,1) + __end,
    BLANK()
)
Measure 2 = 
IF(
    [End_time] <> BLANK(),
   [Start_time] - [End_time],
    TIME(0,0,0)
)

v-lionel-msft_0-1599812017116.png

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

amitchandak
Super User
Super User

@HenryJS , Try new columns like

 

create new columns
start time = [Date] + [start]
End time = [Date] + [end]

Break = maxx(flter(Table, [Timesheet ID] =earlier([Timesheet ID]) && [Date]<earlier([date])),[End time]) -[start time]

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak thanks that works!

 

However on the first instance it calculates a break time even though there's no shift before?

 

The one highlighted red should be 0

 

 

 

Capture.PNG

Greg_Deckler
Community Champion
Community Champion

@HenryJS 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 __Previous = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Value])
RETURN
  __Current - __Previous

 

In your case you would use DATEDIFF between your row and the previous row once you extract the necessary values.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.