Calculate amount of date ranges' overlaps - node.js

I try to store availability hours for different services. (Every work days (8, 9 hours), 24/7 etc.)
A work day can have different office times (08:00-16:00 or 09:00-17:00, 08:00-18:00 etc.)
There are also national holidays where a normal work day acts like a weekend, and any given weekend day can be also work day.
What is the best way to store this in a database (PostgreSQL)?
Background
The main goal is to calculate if how many minutes of a given time range is in service time.
A) A service time is only work days (08:00-16:00) and the date range is
[2019-10-21 15:00 (Monday) - 2019-10-22 09:00 (Tuesday)]. The whole range is 18 hours long, but only 2 hours were in service time.
B) I also want to calculate if I know the start time (2019-10-21 15:00) and there is 3 hours to finish the work (in work time) then when will be the end date? (2019-10-22 10:00 - 1 hour on Monday, 2 hours on Tuesday)

I would store the data exactly as how you described it.
You can have a table for work windows, i.e. Mon-Fri 08:00 to 16:00
And each service will have it's date ranges, start and stop. I'm guess a service will have multiple date ranges so you might need a date range table that has dateRangeId, start & stop columns.
When you want to calculate something, you pull data for the service and pull data for the work days and calculate what is the actual service time and any other questions you need answered :)

Related

Excel formula or VBA Identifying gaps in overlapping dates and times

I am attempting to find any gaps calculated in minutes between a start and stop date/time range. Essentially time when there are no appointments in the schedule, this is a 24hr service and I am looking for "dead" time when there isn't a customer in the office.
Currently I was attempting to use the =SUMPRODUCT((A2<B$2:B$19)*(B2>A$2:A$19))>1 to find overlaps and the issue I am running into is if there are any overlap in start or stop it disqualifies and does not truly identify the space between appointments just if that appointment is double booked at all.
Here is a new version of the Gap and Island solution to this problem, using Excel 365 functionality:
=LET(start,A2:A19,
end,B2:B19,
row,SEQUENCE(ROWS(start)),
maxSoFar,SCAN(0,row,LAMBDA(a,c,IF(c=1,INDEX(start,1),IF(INDEX(end,c-1)>a,INDEX(end,c-1),a)))),
SUM(IF(start>maxSoFar,start-maxSoFar,0)))
The algorithm is very simple:
- Sort data by start time if necessary, then for each pair of times:
- Record the latest finish time so far (maxSoFar) (not including the present appointment)
- If the start time (start) is greater than maxSoFar, add start-maxSoFar to the total.
The first time interval is a special case - initialise maxSoFar to the first start time.
It can be seen that there are only two gaps in the appointments, from 4:15 to 7:31 (3 hours 16 minutes) and from 11:48 to 14:17 (3 hours 29 minutes) totalling 5 hours 45 minutes.
Why didn't I just use Max to make the code shorter? I don't know:
=LET(start,A2:A19,
end,B2:B19,
row,SEQUENCE(ROWS(start)),
maxSoFar,SCAN(0,row,LAMBDA(a,c,IF(c=1,INDEX(start,1),MAX(INDEX(end,c-1),a)))),
SUM(IF(start>maxSoFar,start-maxSoFar,0)))
To find the gaps between appointments in a schedule, you can try using the following formula:
=SUM(B2:B19)-SUMPRODUCT((A2<B$2:B$19)*(B2>A$2:A$19))
You can then convert the duration to minutes by multiplying the result by 1440 (the number of minutes in a day).
=1440*(SUM(B2:B19)-SUMPRODUCT((A2<B$2:B$19)*(B2>A$2:A$19)))

Calculating regular hours and night hours between time and dates

i have got a problem in where i try to make an excel spreadsheet to keep track of my wage and that i am paid the correct amount.
I recieve different rates depending on the time of day. I get X during the day and Y during the night which is from 23:00 (11PM) Which means if i show up to work at 16:00 (4PM) and leave at 1:15 (01:15AM) i should get 7 hours at X rate and 2.25 hours at Y rate.
However i can't seem to figure out how to format my cells in excel so that they take and Check in time and a check out time. And then figure out how many regular hours and how many night hours are in it.
Snip of data sample

Calculate annual leave between two dates in Excel

This task seems easy enough yet I just can't figure a way of doing this without resorting to vba.
All I need is to know the number of hours an employee has used up on annual leave, based on a start and end date, and their hours of work.
To be more clear, this example shows one employees contracted hours from Monday to Sunday i.e. they work only Weds, Thurs, and Friday, for 7.5 hours each day.
Below shows the start and end date that the employee has chosen to take for annual leave. I need to calculate, based on their contracted hours, how much annual leave is used between the two dates. The answer would be 45 hours in this case.
Here's another approach - I've expanded the days between the start and end dates into an array then used the resulting day numbers to offset into the days of work range
=SUMPRODUCT(N(OFFSET(A3,0,WEEKDAY(ROW(INDIRECT(A6&":"&B6)),2)-1)))
If you had a list of holiday dates somewhere (say in I3:N3) you could exclude them as follows
=SUMPRODUCT(N(OFFSET(A3,0,WEEKDAY(ROW(INDIRECT(A6&":"&B6)),2)-1))*ISNA(MATCH(ROW(INDIRECT(A6&":"&B6)),I3:N3,0)))
- it is a bit long-winded but the only way I can think of at the moment.

Excel function to create due dates that land on a business day

I'm stuck creating a formula that will calculate days before the end of the month then adjust to make sure it is a business day. For example: 30 days before 6/30/2015 is 5/31/2015 which is a Sunday. I need that to adjust to the Friday before.
I'm working on finding the due dates of a number of documents that are due a certain number of days before another date. For example: documents are due 30 days before the last day of the month. However, the number of days varies and the due date needs to fall on a business day (Monday-Friday). Sometimes it's 30 days, sometimes it's 60 days, sometimes it's 30 calendar days + 5 business days, etc.
I've been able to calculate 30 days + 5 business days with the following formula:
=workday(start_date-30,-5)
Any ideas how to adjust this so that I can just have the due date be 30 calendar days before a certain date but also always be a business day?
Using WORKDAY you can use a formula like this:
=WORKDAY(A1+B1+1,-1)
where A1 is your start date and B1 the number of days to add.
You probably need to write a macro function or maybe some nested IF statements in your cell's formula.
Take a look at http://www.mrexcel.com/forum/excel-questions/481558-round-date-nearest-workday.html
That solution moves forward to the nearest workday, but the principle is sound: just subtract instead of add.

Excel - Find if worker has worked certain OT if weekday and or Sunday

I have a worksheet with days running along the columns, within each day there are different hourly categories, 130%, 150%, 200% and 215% etc, there are other categories within each day, but these are not to be included. (Can't post Image as new, can email?)
If a worker works over 3.5 hours in any of these categories they get an extra lunch, except on Sunday's where they must work 4 hours.
I've been using countif's to check each day over 3.5, and another column for Sunday's.
The weekday and Sunday can be combined into the same column as the lunch price is still the same, just number of working hours different.
The main problem is I have to adjust the countif's every month, I want something that will look at days of the week and/or sunday's and check without adjusting every month.
Have been trying to get my head around sumproduct with countif!
Any help, very much appreciated.
Thanks!
Since you have not mentioned what is where this might take some adjustment, but may get you started:
=IF(SUM(B3:G3)>=IF(WEEKDAY(B$1)=1,4,3.5),"lunch","no lunch")

Resources