Google Spreadsheet, finding the previous weekday - excel

In Google Spreadsheet, or excel if its the same, how do I find the last weekday of day D only if D is not a weekday itself.
I.E:
If D equals weekday = do nothing (D equals D).
If D equals weekend = D equals last weekday.
Edit: i tried this to get the workday one month away:
WORKDAY(EDATE(N18, 1)+1, -1)
But I Was getting some weird results. Perhaps it counts red days as well, but I never specified the region anywhere.
Edit again:
This above example actually works, eg:
=WORKDAY("2013-04-06"+1, -1)

=WORKDAY("2013-04-06"+1, -1)
...does the trick.

Another option may be:
=IF(WEEKDAY(D1, 2) > 5, D1 - (WEEKDAY(D1, 2) - 5), D1)

It's not so straightforward, but you can use something like that:
=IF(OR(WEEKDAY(A1)=1,WEEKDAY(A1)=7),A1-CHOOSE(WEEKDAY(A1),2,,,,,,1),A1)
Basically, if the weekday is 1 or 7 (Sunday or Saturday, as this is how Excel treats weekdays), return the date minus 2 if Sunday or minus 1 if Saturday, else the date itself.

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)

Create a rolling date list in Excel

I want to create a rolling list of dates in Excel like so:
Day Date
Day 1 01-Jul-19
Day 1 02-Jul-19
Day 1 03-Jul-19
Day 1 04-Jul-19
Day 1 05-Jul-19
Day 1 06-Jul-19
Day 1 07-Jul-19
Day 2 02-Jul-19
Day 2 03-Jul-19
Day 2 04-Jul-19
Day 2 05-Jul-19
Day 2 06-Jul-19
Day 2 07-Jul-19
Day 2 08-Jul-19
Day 3 03-Jul-19
Day 3 04-Jul-19
Day 3 05-Jul-19
Day 3 06-Jul-19
Day 3 07-Jul-19
Day 3 08-Jul-19
Day 3 09-Jul-19
Day 4 04-Jul-19
. .
. .
. .
So essentially what's happening is that the 7-day range moves forward by one day each time, from a specific start date (in the example above, 01-07-19) until it reaches an end date. Is there an automated way of doing this?
#ashvin10 you can do this in vba, but you can also accomplish this with 2 formulas without using vba at all, here's how:
for illustration purposes we'll just assume you are starting with 07/01/2019 on the first row and your information will be displayed in columns A and B.
in A1 enter the string Day 1
in B1 enter your starting date, like 07/01/2019
in A2 enter this formula: ="Day " & IF(MOD(ROW(A2),7)<>0, MID(A2,5,(LEN(A2)-4)), MID(A2,5,(LEN(A2)-4))+1)
in B2 enter this formula: =IF(A2=A1,B1+1,OFFSET(B2,-7,0)+1)
highlight cells A2 and B2
click on the cross that becomes available on the bottom right hand corner of cell B2
drag down the formula till you hit the end date you desire
the cells are populated with the values you requested in the format you requested
If you absolutely have to have it done using vba please let me know and I can show you how to do it that way as well, but this way is much easier.
EDIT: #ashvin10 I'm so sorry, the original formula I instructed you to put into A2 only works for Day 1 through Day 9, if you go into days past 9 it won't display correctly. I've fixed the formula that should be pasted into A2 so now it will work no matter how many days you go down. I'm so sorry for the confusion.
Alternatively, this can also be done in Python.
import datetime
start_date = '01-07-2019'
end_date = '31-01-2020'
output_file_name = 'rolling dates'
output_file_extension = '.CSV'
delimiter = '\t'
with open((output_file_name + output_file_extension.lower()), 'w+') as file:
header = "Day" + delimiter + "Date" + '\n'
file.write(header)
start_date_object = datetime.datetime.strptime(start_date, '%d-%m-%Y').date()
end_date_object = datetime.datetime.strptime(end_date, '%d-%m-%Y').date()
number_of_days = abs((end_date_object - start_date_object).days)
next_day = start_date_object
for i in range(1, number_of_days + 2):
for j in range(7):
file.write(("Day {0}" + delimiter + next_day.strftime('%d-%m-%Y') + '\n').format(i))
next_day += datetime.timedelta(days=1)
start_date_object += datetime.timedelta(days=1)
next_day = start_date_object
After running the code above, I simply created a blank Excel file and then imported the data from the CSV file output by this code.
This is arguably more complicated than #gharbad-the-weak's answer but thought I'd include this anyway.

SLA COUNT IN EXCEL FORMULA

I'm looking for an excel (Office 2016 packet) formula that count SLA for ticket resolution.
the SLA counter must consider :
1) Monday To Saturday as work day ;
2) Holiday as no working days (must be skipeed as work day);
3) SLA start for ticket generate in workdays during range-time "08:00-20:00" out of this time-range SLA count is "0" ;
4) Output should result as R1= first 24Hours ; R2= from 25 to 48 hours ; R3= from 49 to 72 hours; "Out of Sla" = since 72 hours
Data to count SLA is formatted as below for either for ticket open and closure:
Column N Column O Column P Column Q
"TICKET START DATE" "TICKET STOP DATE" "SLA" "HOLIDAY"
28/4/18 13:30 30/4/18 19:20 2 25/04/2018
28/4/18 13:11 29/4/18 13:11 1 01/05/2018
28/4/18 12:57 28/4/18 12:57 1
I solved point 1) & 2) with NETWORKDAYS.INTL formula using the first column as start_date ; the second column as end_date; 11 as third formula's field to exclude Sunday as workdays; fourth formula's value pointing a column where are listed the "holidays" date .
I could not find a solution for point 3).
It will also appreciate a possible solution for point 4) .
Thank you in advance.
Formula example of above fields :
=NETWORKDAYS.INTL(N15;O15;11;Q16:Q17)
Alessandro.
I've needed to do something similar in the past. Please see the formula below. It will give you the amount of time between working days (Monday-Friday), only taking into account times 8:30am to 5:30pm. Try inserting your specific parameters.
=IFERROR((NETWORKDAYS.INTL(H8,I8,,)-1)*("17:30"-"8:30")
+IF(NETWORKDAYS.INTL(I8,I8,,),MEDIAN(MOD(I8,1),"8:30","17:30")
,"17:30")-MEDIAN(NETWORKDAYS.INTL(H8,H8,,)*MOD(H8,1),"8:30","17:30"),"")
H8 = Start Date
I8 = End Date
8:30 = Start Time
17:30 = End Time

How to determine a week given start day and end day?

I would like to write a function in Google Spreadsheets which simply gives me the week A given Start day B and End Day C.
A B C
201507 09/02/2015 15/02/2015
201508 16/02/2015 22/02/2015
201509 23/02/2015 01/03/2015
201510 02/03/2015 08/03/2015
201511 09/03/2015 15/03/2015
201512 16/03/2015 22/03/2015
201513 23/03/2015 29/03/2015
201514 30/03/2015 05/04/2015
201515 06/04/2015 12/04/2015
201516 13/04/2015 19/04/2015
I though about implementing a mod function mod(x,7)=0 which restarts the week after 7 days but that was somehow difficult to implement, at least for me.
Similar to #Munkey's solution, but shorter:
=year(B1)&text(weeknum(B1),"00")
Assumes 09/02/2015 is in B1.
So not the most elegant of solutions, but a start I guess.
Assuming your data starts at B2 and goes down. Try this in A2 and drag down
=CONCATENATE(right(B2,4),If(WEEKNUM(B2,2)<10,CONCATENATE(0,WEEKNUM(B2,2)),WEEKNUM(B2,2))
This formula assumes your weekday starts on Monday. If your weekday starts on a different day, Change the 2 in the weeknum to whatever suits. 1 being sunday, 2 being monday, etc.
Lastly this formula doesn't even look at end date.

Determine if date is a workday in Excel

I have a date in column H10 and need to add 45 days to this date in the next Column I
If there are not dates Column I must be blank
If the 45th day falls on a weekend the calculation must move to the next workday which is Monday
You need to combine two fundamental functions.
First, DATE + INT = DATE. For example, if H10 = 1/8/2015 and H11 = H10 + 10 then H11 will show 1/18/2015.
In your case, you want to use H10 + 45.
Second, you can use the Weekday(date,mode) function to determine the day of the week. Personally, for your purpose, you could use weekday(h10 + 45, 2) which would give a 1-5 for MTWRF, and a 6-7 for a weekend day. So something like
=if(weekday(h10+45,2) < 6, "weekday", "weekend")
=if(weekday(h10+45,2) = 1, "Monday!!", "not monday...")
But we aren't done yet - you need to make sure your day actually ends up on a weekday. So we can do something like this - when determining a weekday, we can use that to determine how much we need to add. If we end up with a 6 (Saturday) we want to add 2 days to push it to a Monday. In the case of a 7, we want to add 1 day to push it to a Monday. Thus, we can simply take the 8 - weekday(h10+45) to add when it's a weekday. So our add value becomes
// determine day type weekday weekend, so add the offset
= if(weekday(h10+45) < 5, h10+45, h10 + 45 + (8 - weekday(h10+45))
You also have a requirement about being blank, so you'll want to wrap whatever you use with
=if(isblank(h10),"", /* your real function here */)
You can combine the functions for IF(), WEEKDAY() and WORKDAY() to calculate your finish date and ensure that it does not fall on a weekend.
I've used
WEEKDAY(WORKDAY(H10+45),16)
to have Saturday and Sunday be represented as days 1&2 respectively.
IF(WEEKDAY(WORKDAY(H10,45),16)=1,WORKDAY(H10,45)+2,IF(WEEKDAY(WORKDAY(H10,45),16)=2,WORKDAY(H10,46),H10))

Resources