How to Ignore an Overnight Break When Returning Last Shift Time in Excel
Question details
The user needs to retrieve the final valid activity end time from a list of shifts, specifically excluding next-morning times that occur after an overnight rest break longer than nine hours.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Tracking employee shift schedules where some shifts cross midnight, and long overnight breaks might incorrectly skew the calculation of the final activity time.
- Observed behavior
- Standard lookup functions return the absolute last time entry in the column, inadvertently including next-morning times rather than identifying the final activity of the actual shift block.
Ensure your start and end time data are formatted correctly as Time or Custom (h:mm) in your spreadsheet, and verify that the start times and end times are aligned in adjacent columns.
Use LOOKUP and MOD Functions to Filter by Time Interval
Apply a formula combining LOOKUP, MOD, and TIME to dynamically find the last valid end time that does not exceed a 9-hour gap from the start time.
This approach uses the LOOKUP function to search for the last matching record in an array. The MOD function calculates the duration correctly even when the shift crosses midnight, and TIME(9,0,0) sets the maximum valid shift duration to 9 hours.
Click on the cell where you want the final shift time to appear.
Type the following formula into the formula bar: =LOOKUP(2,1/(B16:B32<>"")/(MOD(B16:B32-A16:A32,1)<=TIME(9,0,0)),B16:B32). This assumes your start times are in column A (A16:A32) and end times are in column B (B16:B32).
Modify the cell references (like A16:A32 and B16:B32) to exactly match your actual data range if your table extends to different rows (e.g., A16:A33).
Press Enter to calculate. Format the output cell as Time if it displays as a decimal number.

Easily Calculate Complex Time Data with WPS Office
WPS Spreadsheet supports all advanced lookup and time functions needed for tracking shift schedules, including those that cross midnight or involve complex break logic.
- 1. Download WPS Office: Download and install WPS Office for free from the official website.
- 2. Open your schedule: Launch WPS Spreadsheet and open your shift tracking file.
- 3. Apply the formula: Enter the =LOOKUP(...) formula directly in your summary cell to filter out the overnight breaks.
- 4. Format the output: Right-click the cell, select 'Format Cells', and choose 'Time' to display the result correctly.

Frequently Asked Questions
Why use the MOD function for times crossing midnight?
When a shift ends after midnight, subtracting the start time from the end time results in a negative number in spreadsheet logic. Using the MOD(End - Start, 1) function converts this negative value into the correct positive time duration.
Can I change the overnight break threshold to 12 hours instead of 9?
Yes, simply modify the TIME(9,0,0) part of the formula to TIME(12,0,0). The parameters inside the TIME function represent hours, minutes, and seconds respectively.
Why does the formula use LOOKUP(2, 1/...)?
This is a standard spreadsheet technique to find the last matching value in a column. Since 1 divided by the condition array results in only 1s and #DIV/0! errors, searching for the number 2 guarantees finding the last valid '1' (the last row meeting your criteria).
What if my end time column has empty cells?
The condition (B16:B32<>'') in the formula explicitly ignores any empty cells in the end time column, ensuring that blank rows or unfinished shifts do not cause calculation errors.




