Using Workday when start date is a wekeend date - excel-formula

I have a spreadhseet that pulls Planned Maintenace due dates. I am calculating 5 potential days the work can be scheduled, from 2 days before to days days after, so a range of 5 days. Unfortunately, sometimes the original due date is a weekend date(our business system calculates but doesn't append for weekend dates). So when I calculate the first 2 days, =workdate(A1,-2,holiday) and =workdate(A1,-1,holiday)it works fine, however for the 3rd date =workdate(A1,0,holiday) returns the original due date even if it's a weekend date.
Example, July 6th 2013 is a Saturday, so
1. workdate(A1,-2,holiday) = 7/3/2013
2. workdate(A1,-1,holiday) = 7/5/2013
3. workdate(A1,0,holiday) = 7/6/2013 needs to be 7/8/2013
4. workdate(A1,1,holiday) = 7/8/2013 needs to be 7/9/2013
5. workdate(A1,2,holiday) = 7/9/2013 needs to be 7/10/2013
Is there any way to compensate for a start date that is a weekend date!
Thanks in advance for any help or suggestions.

The function it appears you are using is workday(), not workdate()? So what you probably need to do is put an adjustment on the # you pass to Workday if the day of the week is Sat/Sun.
=WORKDAY(A1,IF(OR(TEXT(A1,"ddd") = "Sat",TEXT(A1,"ddd") = "Sun"),1,0))
=WORKDAY(A1,IF(OR(TEXT(A1,"ddd") = "Sat",TEXT(A1,"ddd") = "Sun"),2,1))
=WORKDAY(A1,IF(OR(TEXT(A1,"ddd") = "Sat",TEXT(A1,"ddd") = "Sun"),3,2))

Related

how to calculate the date from the year, week-of-year and day-of-week in EXCEL?

I have the Year, Week-of-Year and Day-of-the-Week as follows:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
and I would like to estimate the Date as dd.mm.yyyy, which is highlighted in yellow as it shows in the EXCEL picture.
I tried many formulas, but I am sure there might be an easy one.
I think you are counting the weeks starting from zero because for 9/1/2022 (YYYY/MM/DD format) the corresponding week is 36 as per the result of function WEEKNUM(DATE(2022,9,1)). In order to use the logic to multiply the number of weeks by 7. You need to use as a reference the first day of the year, if it was a Sunday, if not then go back to the previous Sunday, so you can count the entire week. Bottom line use as a reference date, the Sunday of the first week of the year, not the first day of the year (YYYY/1/1)
Here is the approach we use in cell E2:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1))
We use the LET function to avoid repeating the same calculation. The following expression finds the previous Sunday if the first day of the year (fDay) was not a Sunday:
fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2))
The XLOOKUP function is used to get the numeric representation of the weekday and use the TEXT function to generate the weekdays in a long format. Since we count the entire week, if the weekday is a Sunday (column C in my screenshot), then we don't need to add any day to our reference date, that is why we use seq-1.
Here is the output for several sample data. Assuming the week count starts with zero, if not the formula needs to be adjusted as also the input data.
Notice that the year 2021 started on a Friday, so if we want to find a day for the first week (0) before Friday it will return a date from the previous year. Like in the case of Monday. If you want an error message instead, then the formula can be modified as follow:
=LET(y, A2:A6, wk, B2:B6, wDay, C2:C6, fDay, DATE(y,1,1), seq, SEQUENCE(7),
result, fDay - IF(WEEKDAY(fDay)=1,0, WEEKDAY(fDay,2)) + 7*wk
+ XLOOKUP(wDay, TEXT(seq,"dddd"), seq-1),
IF(YEAR(result) <> y, "ERROR: Date from previous year", result))
I found the solution:
Year = 2022 (A2) ; Week Year = 35 (B2); Week Day = 4 or Thursday (C2)
=DATE (A2,1,3)-WEEKDAY(DATE(A2,1,3)) + 7 * B2 + C2 - 6
I found this solution, but you need to do further testing if it really works.
I calculate month from week: =+MONTH(DATE(YEAR(A2);1;1)+B2*7-1)
I calculate week day number from week day name: =MATCH(D2;{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"};0)
And then make date using: =DATE(A2;C2;E2)

Excel formula to calculate elapsed minutes between 2 date timestamps only counting minutes during work hours

I have an excel sheet with about 50,000 records where I need to find the number of minutes between two date timestamps but I need to exclude any minutes that occurred during the times we are not working.
Our schedule is M-F 8:30am-5:30pm, Saturdays 8:30am-1:30pm
We don't work Sundays or holidays.
As an example
Cell B2: [7/3/2020 2:16:21 PM]
Cell C2: [7/6/2020 9:20:23 AM]
The manually calculated answer for this one should be about 244 minutes. Task started Friday afternoon, Saturday was a holiday, don't work Sundays, task completed at 9:20am on Monday.
Usually, I come here and start writing a question and by the time I've understood my own problem well enough to post a question I have figured it out on my own but not this time! Help!
Update:
#ForwardEd shared this...
=((I2-H2)
-MAX(0,(NETWORKDAYS.INTL(H2,I2,"0000011",$M$2:$M$12)-1+(WEEKDAY(I2,1)=7)))*TIME(15,0,0)
-MAX(0,(NETWORKDAYS.INTL(H2,I2,"1111101",$M$2:$M$12)-(WEEKDAY(I2,1)=7)))*TIME(19,0,0)
-NETWORKDAYS.INTL(H2,I2,"1111110",$M$2:$M$12)-(NETWORKDAYS.INTL(H2,I2,"0000000")
-NETWORKDAYS.INTL(H2,I2,"0000000",$M$2:$M$12)))*24*60
Where H:H is the Start Date Timestamp and I:I is the Response Date Timestamp and M2:M12 contains my holiday list.
It worked beautifully until I ran into an example like this:
H2 - 07/26/2020 7:48:45 PM
I2 - 07/27/2020 8:57:58 AM
The net result was -650.78333. It looks like anything that starts one one day and ends on the next is coming back as negative.
We want to measure the average response time in minutes for the applications that require manual underwriting. These start timestamps are times that loan applications were received online so they could come in any time of day. The stop times are timestamps that represent the system recorded response time. i.e. the timestamp where an underwriter first did something with the loan application. If a loan application was received at 7pm and was not auto-decisioned then a manual underwriter will need to do something with it the next day when we start working.
If that application came in at 7pm on Wed and is decisioned by an underwriter at 8:46am on Tuursday, we would want to document 16 minutes for that application - not 826 counting the hours between 7pm and 8:30am.
What you want to look at is NETWORKDAYS.INTL. Use this in conjunction with the custom settings to determine the number of Saturdays, Sundays and for the number of days in between your start and end time. You know you have X amount of time per day that is non working time, and Y amount per Saturday.
Then you formula in essence becomes
(End time - start time) - X * No. Weekdays - Y * No. Saturdays - No. Sundays - No. Holidays
Now there will be some tricks in there in order to count your days. but that is the gist of what it boils down to in a formula.
The formulas that are doing the brunt of the work are:
WORKDAY
NETWORKDAYS.INTL
TIME
I avoided the use of an if statement by using a boolean operation that excel will resolve from TRUE/FALSE to 1/0 when sent through a math operator. Side note: I read somewhere that this is also faster than an IF statement, but have no way of proving it and really does not matter on a small number of calculations.
WORKDAY
This formula will return the day of the week for a given date, and a set day of the week to be 1. It will be need in this solution to determine if the end date is a Saturday which has a value of 7 in default setup up as well when option 1 is picked. The format for the formula is:
WORKDAY(Excel Serial date, day 1 of the week)
For this solution
WEEKDAY(B3,1)
NETWORKDAYS.INTL
This formula will be used to count the number of specific days a start and an end date. It can exclude a custom weekend or count a custom week. If it is supplied with a list of dates that are holidays they can be excluded as well. The basic format of the formula is:
NETWORKDAYS.INTL(Start Date, End Date, Custom week choice or workweek pattern, range of holiday dates)
When entering the formula it will give you a list of predefined options for the weekend choices. It will not talk about the pattern.
The pattern is a string 7 digits long consisting of 1 or 0. 0's represents the days you want to count and 1's are days you want to ignore. An important part of the pattern is that the first entry is MONDAY. "1010111" would count only Tuesdays and Thursdays.
TIME
Excel stores date as an integer. 1 represents 1st of January 1900, 2 the 2nd of January 1900 and so on. Time is stored as a decimal or if you prefer the percentage/fraction of a day or 24 hour period. So rather than figuring out the math to determine what percentage of a day X number of hours is, it is simpler to let excel calculate it for us and make the number a little more understandable to someone who may be deciphering the formula later. The basic format of the formula is:
TIME(Hours, Minutes, Seconds)
So as stated earlier, 6 key components need to be determined:
X - Amount of non working time after a weekday
Y - Amount of non working time after a Saturday
Number of weekdays
Number of Saturdays
Number of Sundays
Number of holidays
1) Determine Weekday Non-Working Hours
Based on the supplied information that work day stops at 1730 and starts as 0830. There are a couple of ways of doing the math. Subtract the working hours from 24 hours or count the non work hours at the end of the day and add them to the non work hours at the start of the day.
24 - (17.5 - 8.5) = 15
or
(24 - 17.5) + (8.5 - 0) = 15
For this example 15 will be hard coded into the final formula
2) Determine Saturday Non-Working Hours
Similar to above. Note that we are ignoring Sunday as it is a designated non working day which we already know is 24 hours or 1 day. We are just interested in the time between end of shift Saturday and start of the next normal working Monday. So it really gets calculated the same with just with difference end of shift time.
24 - (13.5 - 8.5) = 19
or
(24- 13.5) - (8.5 - 0) = 19
For this example 19 will be hard coded into the final formula
3) Determine Number of Weekdays
Based on the description earlier of of NETWORKDAYS.INT and working with the assumption that holidays are stored in the range F2:F2, and using a pattern of "0000011" the number of weekdays the formula will be as follows:
=NETWORKDAYS.INTL(B2,B3,"0000011",F2)
For this example the formula is place in cell F6
4) Determine Number of Saturdays
Similar 3) adjust the pattern to only select Saturdays by using "1111101"
=NETWORKDAYS.INTL(B2,B3,"1111101",F2)
For this example the formula is place in cell F7
5) Determine Number of Sundays
Similar 4) adjust the pattern to only select Saturdays by using "1111110"
=NETWORKDAYS.INTL(B2,B3,"1111110",F2)
For this example the formula is place in cell F8
6) Determine Number of Holidays
To get the number of holidays there is not a direct way of doing it. Instead take the difference between all days counted without holidays being factored in and all days counted with holidays counted in.
=NETWORKDAYS.INTL(B2,B3,"0000000")-NETWORKDAYS.INTL(B2,B3,"0000000",$F$2:F2)
For this example the formula is place in cell F9
Now at this point I would love to say just substitute all of the above into the generic formula, but there are a couple of special cases that need to be taken care of. You may have also noted I have not used the WEEKDAY formula yet.
So in order to count the number of days to which X is going to apply, its really the number of days minus 1. The minus 1 is because you want to cont the intervals between days, not the number of days themselves. This gets a little bit more trickier when the end day is a Saturday because there is still an interval there but Saturday is not counted as a weekday. So the True count for number of weekday intervals is:
=MAX(0,(F6-1+(WEEKDAY(B3,1)=7)))
I originally had the MAX(0, calc) in there to prevent the posibility of the day count being negative. After arriving at this final format it may not be needed and you might get away with the following but its untested:
=F6-1+(WEEKDAY(B3,1)=7)
This same concept needs to be applied to your Saturday count. If you job ends on Saturday you do not need to subtract the non working hours after the last Saturday. You formula will look like:
=MAX(0,(F7-(WEEKDAY(B3,1)=7)))
and again further testing is required to make sure MAX can be removed, but if it can then the formula would look like:
=F7-(WEEKDAY(B3,1)=7)
So now with the understanding how dates and times are stored, determine the time difference between start end end time and subtract all the non working hours.
=(B3-B2)-MAX(0,(F6-1+(WEEKDAY(B3,1)=7)))*TIME(15,0,0)-MAX(0,(F7-(WEEKDAY(B3,1)=7)))*TIME(19,0,0)-F8-F9
Now you will not want to use helper cells, so you can take each of the individual formula from F6 to F9 and wind up with:
=(B3-B2)-MAX(0,(NETWORKDAYS.INTL(B2,B3,"0000011",F2)-1+(WEEKDAY(B3,1)=7)))*TIME(15,0,0)-MAX(0,(NETWORKDAYS.INTL(B2,B3,"1111101",F2)-(WEEKDAY(B3,1)=7)))*TIME(19,0,0)-NETWORKDAYS.INTL(B2,B3,"1111110",F2)-(NETWORKDAYS.INTL(B2,B3,"0000000")-NETWORKDAYS.INTL(B2,B3,"0000000",$F$2:F2))
The formula looks unruly, but is easier to understand when broken down into its parts.
Now the last step is to get the answer to display in minutes. There are two choices.
You can leave it as it is in an excel serial date format and change the formatting of to a custom format of [m]. The [ ] will force it into minutes and prevent spill over to hours. It will also round to the nearest minute.
You can convert the results to minutes by multiplying by 24*60 and the value will be in minutes and decimal of minutes.
Note that:
A11 has Time formatting applied
A12 has General formatting applied
A14 has custom formatting of [m] applied
It should be something like this:
Create a calendar table with the workinghours for each days in the year you have data in
Date | StartTime | End time
1/1/2020 1/1/2020 8:30:00 PM 1/1/2020 5:30:00 PM
...
7/3/2020 7/3/2020 8:30:00 PM 7/6/2020 5:30:00 PM
...
12/31/2020
Then paste this code in a module
Function CalcDays(dStart As Date, dEnd As Date, daysCalendar As Range)
Dim Cell As Range
Dim MinDaysCalendar As Date, MaxDaysCalendar As Date
Dim aWSF As WorksheetFunction
Set aWSF = Application.WorksheetFunction
'check the minimum en the maximum date in the calendar
With aWSF
MinDaysCalendar = .Min(daysCalendar)
MaxDaysCalendar = .Max(daysCalendar)
End With
'if the date you check is not in the calendar, exit the function
If dStart < MinDaysCalendar Or dStart > MaxDaysCalendar Then
MsgBox "Date not in calendar"
Exit Function
End If
If dEnd < MinDaysCalendar Or dEnd > MaxDaysCalendar Then
MsgBox "date not in calendar"
Exit Function
End If
'sum the time of all the dates between the start and the end
'pick min and max in order to start and stop at the right time per day
Dim tempTime As Integer
With daysCalendar
For i = 1 To .Rows.Count
If .Cells(i, 2).Value >= CLng(dStart) And .Cells(i, 3).Value <= CLng(dEnd) Then
daytime = aWSF.Max(.Cells(i, 2).Value, dStart) - aWSF.Min(.Cells(i, 3).Value, dEnd)
End If
tempTime = tempTime + daytime
Next i
End With
'return the total time
CalcDays = tempTime
End Function
You can call the function by typing =calcdays in a cell and then give the startDay, endDay and calendar column as parameters.
There might still be some flaws in this code but I think we can manage those.

How to get / output all days in the current week in Automation Anywhere?

I'm attempting to output all days within the current week. e.g. for this week, show all days, 05/12/2019 through 05/18/2019 only. when the bot is executed next week, only show days 05/19/2019 through 05/25/2019. My current logic outputs the days for this week, but come tomorrow, the dates for this week will be thrown off. Please see the following
...could I get some help with this please?
Using VBS
I would do this using a VBS script, using Run Script command.
The default week start is Sunday you can change it check: https://www.w3schools.com/asp/func_weekday.asp
Pass the day you that you want as a parameter from 0 to 6, and get the data as a return value.
DayNumber: 0 = Sunday ..... 6 = Saturday
InputDate = Date
DayNumber = WScript.Arguments.Item(0)
Result = DateAdd("d", DayNumber - WeekDay(InputDate, 2), InputDate)
WScript.StdOut.Write(Result)
'MsgBox(Result)
Using MetaBot
Metabot Link: Change Date and Time Format
You will have to run the following logic in sequence.
Input: DayNumber: 0 = Sunday ..... 6 = Saturday
Using DayOfWeek Logic, Get the Day of the week and assign it to
WeekDay variable, it will return the name, not the number, and the input will be Date.
Using IF conditions convert the name of
the day to number, start from 0 to 6 as your first day in the week,
which is Sunday, and using variable operation assigns the value to
NumWeekDay variable.
Using variable operation, Get the offset by subtracting DayNumber, the day you want minus NumWeekDay,
and assign the value to Offset variable.
Using AddDays, Input
the date and the offset, and you will get the date of the day that you want.

EXCEL IF function logical test with date/time formats

I've been working for a project to improve the managing of key performance indexes and I've hit a wall.
Project: To evaluate whether the process(warehouse and quality) meets the deadline. When an Order comes in, the date/time is recorded and depending on the date/time, the deadline is different.
For ease of explanation let B = Order received date/time (FORMAT DD.MM.YYYY HH.MM), X = Deadline (IN SAME FORMAT)
Conditions:
1. If B is between 5.01 am and 1pm, then X = 5pm, else X = next day 9am
2. If B is between sat 5am to 5am mon, then X = mon 9am.
I have managed to figure out condition 1 but i have no idea how to incorporate condition 2 into my formula.
Current formula
=IF(AND(B>DATE(YEAR(B),MONTH(B),DAY(B))+5/24,B<=(DATE(YEAR(B),MONTH(B),DAY(B))+13/24)),DATE(YEAR(B),MONTH(B),DAY(B))+17/24,DATE(YEAR(B),MONTH(B),DAY(B))+24/24+9/24)
I'm hoping the community can give me some insight on how to solve this enigma as i've been having plenty of luck finding solutions here.
My Answer:
=IF(OR((AND(WEEKDAY(B6)=7,TIME(HOUR(B6),MINUTE(B6),SECOND(B6))>5/24)),WEEKDAY(B6)=1,(AND(WEEKDAY(B6)=2,TIME(HOUR(B6),MINUTE(B6),SECOND(B6))<5/24))),WORKDAY(B6-1,1)+9/24,(IF(AND(B6>DATE(YEAR(B6),MONTH(B6),DAY(B6))+5/24,B6<=(DATE(YEAR(B6),MONTH(B6),DAY(B6))+13/24)),DATE(YEAR(B6),MONTH(B6),DAY(B6))+17/24,(IF(B6<=(DATE(YEAR(B6),MONTH(B6),DAY(B6))+5/24),DATE(YEAR(B6),MONTH(B6),DAY(B6))+9/24, DATE(YEAR(B6),MONTH(B6),DAY(B6))+24/24+9/24)))))
Its lengthy but i'll try my best to explain.
1. The first part is the weekend portion: IF(OR((AND(WEEKDAY(B6)=7,TIME(HOUR(B6),MINUTE(B6),SECOND(B6))>5/24)),WEEKDAY(B6)=1,(AND(WEEKDAY(B6)=2,TIME(HOUR(B6),MINUTE(B6),SECOND(B6))<5/24))),WORKDAY(B6-1,1)+9/24
IF SAT>5am OR SUN OR MON <5am, VALUE IF TRUE = Next workday 9am (mon)
VALUE IF FALSE = (IF(AND(B6>DATE(YEAR(B6),MONTH(B6),DAY(B6))+5/24,B6<=(DATE(YEAR(B6),MONTH(B6),DAY(B6))+13/24)),DATE(YEAR(B6),MONTH(B6),DAY(B6))+17/24,(IF(B6<=(DATE(YEAR(B6),MONTH(B6),DAY(B6))+5/24),DATE(YEAR(B6),MONTH(B6),DAY(B6))+9/24, DATE(YEAR(B6),MONTH(B6),DAY(B6))+24/24+9/24)))))

How do I get the Month End Personal Time Off Days used?

Using the Start Date and End Date of PTO - Personal Time Off Days Used only count days used up to end of prior month, excluding weekends and U.S Holidays in that certain month. Example of a Holiday is Sept 7th 2015 in the United States.
My goals are:
Create a Data Item Month End Personal Time Off Days used.
Of course it should be getting the number of PTO Days USED from the prior month only.
Exclude weekends in that certain month. So if the Resource takes a Leave on Friday and Monday, Saturday and Sunday should not be excluded in the computation.
How to exclude U.S Holidays, if this is possible that's great but if it's not possible then I'm okay with numbers 1, 2 and 3.
I have created a Data Item column that captures the PTO days used. But this is good for Year to date.
Case when [PTO Info].[PTO Audit].[PTOAuditTypeId] = 31571
and [PTO Info].[PTO Audit].[TimeOffTypeId] = 31566
then [PTO Info].[PTO Audit].[PTODays]
when [PTO Info].[PTO Audit].[PTOAuditTypeId]=31572
and [PTO Info].[PTO Audit].[TimeOffTypeId] = 31566
and [PTO Info].[PTO Audit].[PTODays] < 0
then abs([PTO Info].[PTO Audit].[PTODays] )
else 0 end
I'm not sure if the query below can help.
A calendar table is really going to help you out here. Assuming it has one record per calendar date, you can use this table to note weekends, holidays, fiscal periods vs Calendar periods, beginning of month/end of month dates. A number of things that can help simplify your date based queries.
See this question here for an example on creating a calendar table.
The main point is to create a data set with 1 record per date, with information about each date including Month, Day of Week, Holiday status, etc.
Without a calendar table, you can use database functions to generate your set of dates on the fly.
Getting the Month number for a date can be done with
extract([Month], <date field goes here> )
Getting a list of values from nothing will be required to generate your list of dates (if you don't have a calendar table with 1 record per date to use) will vary depending on your source database. In oracle I use a 'select from all_objects' type query to achieve this.
An example from Ask Tom:
select to_date(:start_date,'dd-mon-yyyy') + rownum -1
from all_objects
where rownum <=
to_date(:end_date,'dd-mon-yyyy')-to_date(:start_date,'dd-mon-yyyy')+1
For Sql Server refer to this stackoverflow question here.
Once you have a data set with your calendar type information, you can join it to your query above:
join mycalendar cal on cal.date >= c.PTOStartDate
and cal.date <= c.PTOEndDate
Also note, _add_days is a Cognos function. When building your source queries, try and use Native functions, like in oracle you can 'c.PTOStartDate + a.PTODays'. Mixing Cognos functions with native functions will sometime force parts of your queries to be processed locally on the Cognos server. Generally speaking, the more work that happens on the database, the faster your reports will run.
Once you have joined to the calendar data, you are going to have your records multiplied out so that you have 1 record per date. (You would not want to be doing any summary math on PTODays here, as it will be inflated.)
Now you can add clauses to track your rules.
where cal.Day_Of_Week between 2 and 6
and cal.Is_Holiday = 'N'
Now if you are pulling a specific month, you can add that to the criteria:
and cal.CalendarPeriod = '201508'
Or if you are covering a longer period, but wanting to report a summary per month, you can group by month.
Final query could look something like this:
select c.UserID, cal.CalendarPeriod, count(*) PTO_Days
from dbo.PTOCalendar c
join myCalendar cal on on cal.date >= c.PTOStartDate
and cal.date <= c.PTOEndDate
where cal.day_of_week between 2 and 6
and cal.Is_Holiday = 'N'
group by c.UserID, cal.CalendarPeriod
So if employee with UserID 1234 Took a 7 day vacation from thursday June 25th to Friday July 3th, that covered 9 days, the result you get here will be:
1234 201506 4
1234 201507 3
You can join these results to your final query above to track days off per month.

Resources