PTO — halve days off

Dealing with single shifts:

Christiaan Huizer

--

In my previous post, I shared code for handling day and night shifts, even with multiple breaks. Now, let’s tackle half-day leaves — it’s trickier than I initially thought! But first, a quick fix: the original code didn’t work for days without breaks, so I added a SwitchIf() to handle those cases too.

Now, let’s see how to get those accurate half day calculations, no matter the shift!

Taxonomie

My first idea was to simply use First() for morning shifts and Last() for afternoon shifts. However, this doesn’t work if someone only works in the morning or afternoon. Then first & last are both the same.

Let’s break down the structure of a workday — which can be any day of the week. We have times when we’re working (shifts) and times when we’re not (breaks). A workday always has at least one shift, but might not have a break.

The breaks

So, let’s focus on the breaks because they’re what change when we’re talking about a workday.

  • breakSlots.Count() < 1
  • breakSlots.Count() = 1
  • breakSlots.Count() > 1

No breaks

The first version, which can also be expressed as breakSlots.Count() = 0, handles cases where you only work during a specific part of the day. In this scenario, we can't rely on break information. Instead, we need to make an assumption within our function. This is where it gets a bit tricky because we need to establish a convention. One way to address this is by using a table that allows any authorized user to modify the settings.

we link to a value using the @ symbol and type shift

I took a closer look at the button generating the hour values and realized the code was getting lengthy and hard to follow due to all the scenarios we need to cover. To keep things manageable, I added a small snippet to the second button, which handles day values and is rather short.

This snippet prints the timeslots in a new helper column named “hlprSlots.” I also added a description to the column to remind myself later on why it’s important.

hlprSlots

This helper column keeps the table running smoothly because it doesn’t have any active functions — they only run when the button is pressed. With this column in place, we can check if a half day off falls within a scheduled work shift or during time off.

Here’s an example of taking a half day off on the first and last days of work. Typically, we’d assume you’re taking off either the first or last shift, especially if there are two shifts on those days.

Taking a half day off on the first and last days of work.

But, there are cases where you might only work in the morning or afternoon. In those situations, we first need to check if your half-day off falls during your scheduled work time or not.

If someone typically works Friday afternoons, and their time off request starts and ends on a Friday, they’ll only get 2 hours off on the first Friday. On the last Friday, they won’t have any time off since their workday starts in the afternoon.

Breaks

We continue with these two:

  • breakSlots.Count() = 1
  • breakSlots.Count() > 1

Here we can apply the already developed logic, nothing changes.

If you have multiple breaks during a long workday, we’ll assume the half-day off is the last shift. However, this is flexible, and you could choose the middle and last shifts instead, depending on your company’s policies. The simplest option is to take the last shift in the list. If you’d rather skip the first shift and take all the others, you can use a slice function (without the Sum() function) as shown below.

grab the last two values — in this scenario out of three

Ultimately, it comes down to company conventions.

A lot of this hassle can be avoided by asking people to always request full days off if they have any half-day shifts. But, in my experience, this leads to uncertainty, confusion, and ultimately, mistakes. I prefer to make it as straightforward as possible for the employee.

I hope this article was informative and helpful. Did it help you to solve a problem you unlike would have solved other ways? What about a donation?

My name is Christiaan, and I regularly blog about Coda. While this article is free, my professional services (including consultations) are not, but I’m always happy to chat and explore potential solutions. You can find my free contributions in the Coda Community and on X. The Coda Community is a fantastic resource for free insights, especially when you share a sample doc.

--

--

Christiaan Huizer

I write about how to Coda . You find blogs for beginners and experienced makers. I publish about 1 / week. Welcome!