Excel Qn, Formula to check if a date falls within the upcoming week. - excel

I am trying to check if a date falls within the upcoming week.
For example, today is 9/14/2016.
The upcoming week is defined as anything that are through 9/23/2016.
I was wondering if anyone can help me with it. Thanks.

Assuming standard convention for the week is in line with Excels, (Sunday through Saturday):
=IF(AND(A1 >= TODAY(),A1 <= TODAY() + 6 + WEEKDAY(A1)),TRUE,FALSE)
This will return TRUE if the value in A1 is within the upcoming week and false if it is not.

I can see that 9/23/2016 is a Friday but I wasn't sure what you wanted to do if it was currently Sunday (say) and you wanted to see if a date was on or before Friday in the upcoming week - would that be Friday 5 days later or 12 days later? So I've done an answer that can be tailored however you want it:-
=AND(D$2>=$A3,D$2<=$A3+CHOOSE(WEEKDAY($A3),5,11,10,9,8,7,6))
where A3 contains the current date and D2 contains the date you're trying to evaluate.
So the numbers in the choose statement say what you want to do with each day:-
If Sunday add 5 days
If Monday add 11 days
etc. etc.
See also

Related

Formula to check if working out of hours

I'm trying to find out if someone was not working during 'out of hours'.
If Monday to Friday, not between the hours of, 8am to 9pm, then the answer would be "Yes"
If it's a Saturday, not between the hours of, 8am to 5pm, the answer
would be "Yes"
If it's a Sunday, not between the hours of, 9am to 5pm, the answer
would be "Yes".
Otherwise the answer would be "No".
There are 4 columns: 1. Person's Name (A) 2. Date (B) 3. Time (C) 4. Out of Hours (D)
I've copied in the formula below which is working correctly for Monday to Friday, but I'm struggling with the second part of the weekend one.
=IF(OR(WEEKDAY(B2)=1,WEEKDAY(B2)=7),"Yes",IF(AND(C2>=8/24,C2<21/24),"No","Yes"))
If anyone could advise on how to change the formula to work with 'out of hours' for Saturday and Sunday?
It would be greatly appreciated.
Since your WEEKDAY(B2) will be from 1 (Sunday) to 7 (Saturday), you can use CHOOSE to select the Start/End time for your "In-Hours" period:
Start: =Choose(Weekday(B2),9,8,8,8,8,8,8)
End: =Choose(Weekday(B2),17,21,21,21,21,21,17)
So, if HOUR(C2) is >= Start, and HOUR(C2) is < End then you are "In-Hours" ("No"), otherwise you are "Out of Hours" ("Yes")
=IF(AND(HOUR(C2)>=CHOOSE(WEEKDAY(B2),9,8,8,8,8,8,8), HOUR(C2)<CHOOSE(WEEKDAY(B2),17,21,21,21,21,21,17)),"No","Yes")
Of course, I would recommend replacing the CHOOSEs with a pair of VLOOKUPs, and having a hidden sheet where you list the Weekday and the Start/End-of-Hours hours. (Much easier to update if The Boss decides to change the hours!)
=IF(WEEKDAY(B2)=7,IF(OR(C2<TIMEVALUE("8:00"),C2>TIMEVALUE("17:00"))=TRUE,"Yes","No"),IF(WEEKDAY(B2)=1,IF(OR(C2<TIMEVALUE("9:00"),C2>TIMEVALUE("17:00"))=TRUE,"Yes","No"),IF(WEEKDAY(B2)<>1,IF(OR(C2<TIMEVALUE("8:00"),C2>TIMEVALUE("21:00"))=TRUE,"Yes","No"),"No")))
You will have to use nested ifs. Hope above formula will work.

Week number of a quarter

I'm trying to get the week number of a given quarter based on the date.
I currently have this formula
=1+(WEEKNUM(EDATE(Y4,-1)))-(WEEKNUM(DATE(YEAR(EDATE(Y4,-1)),
LOOKUP(MONTH(EDATE(Y4,-1)),{1,4,7,10}),1)))
But for January, it should be giving me 1 but it's giving me 10. Any suggestions?
How do you expect this to work at the start and end of the quarter? Default WEEKNUM function starts week 1 on the 1st of January every year and week 2 starts on the next Sunday after 1st January.
Assuming your quarter week numbers should work the same way, i.e. week 1 starts on the 1st of Jan/Apr/Jul/Oct and week 2 starts on the next Sunday then that's actually equivalent to counting Sundays since 6 days back into the previous quarter.
You can do that using NETWORKDAYS.INTL function, i.e. with this formula:
=NETWORKDAYS.INTL(EOMONTH(Y4,MOD(1-MONTH(Y4),-3)-1)-5,Y4,"1111110")
format result as number with no decimal places
NETWORKDAYS.INTL function is available in Excel 2010 and later versions - for older versions of Excel you can get the same results with this formula:
=INT((13-WEEKDAY(Y4)+Y4-EOMONTH(Y4,MOD(1-MONTH(Y4),-3)-1))/7)
(Expanded from comment)
when you choose a date in January, it's going back to December. 12 in your lookup array gives 10 as the result. Perhaps instead of EDATE, you should use EOMONTH(Y4,-1)+1, so you look at the 1st of the current month for your calculation
=1+(WEEKNUM(EOMONTH(Y4,-1)+1))-(WEEKNUM(DATE(YEAR(EOMONTH(Y4,-1)+1), LOOKUP(MONTH(EOMONTH(Y4,-1)+1),{1,4,7,10}),1)))
This is fairly interesting, since it changes with the year, and changes with what day of the week is the "start" of the week. So if a quarter starts on Saturday, and the week starts on a Saturday, the entire week is week 1. However, if it starts on a Sunday, week 1 is only one day long, and week 2 starts on Sunday.
The first question we have is, what day is it?
=DayCheck
Additionally, I'm going to call the start of each quarter the following:
Q1Start = Date(Year(DayCheck),1,1)
Q2Start = Date(Year(DayCheck),4,1)
Q3Start = Date(Year(DayCheck),7,1)
Q4Start = Date(Year(DayCheck),10,1)
The next question is, what's the first day of the week? We have some control over this with the Weekday function. For the sake of keeping it simple, Sunday is the start of the week.
Ok, that's our day. Next, what quarter is it?
`Quarter=ROUNDDOWN(MONTH(O16)/4,0)+1`
This gives us 1 for Q1, 2 for Q2, etc.
What day of the week is it now?
=WEEKDAY(DayCheck,1)
Ok, and now, what week are we on?
=WEEKNUM(DayCheck,1)
I'm going to put it together in a not very elegant fashion. I'm sure there's a better way out there.
=(Quarter=1)*((Weeknum(DayCheck)-WeekNum(Q1Start)+1)+(Quarter=2)*((Weeknum(DayCheck)-WeekNum(Q2Start)+1)+(Quarter=3)*((Weeknum(DayCheck)-WeekNum(Q3Start)+1)+(Quarter=4)*((Weeknum(DayCheck)-WeekNum(Q4Start)+1)
Try this:
=CHOOSE((MOD(WEEKNUM(Y4),13)=0)+1,WEEKNUM(Y4)-(ROUNDDOWN(WEEKNUM(Y4)/13,0)*13),13)
This will get the week number of a given date within a quarter.
I used this in one of my applications so you might be able to use it too. HTH.
Note: If you use 1st day other than Sunday, then adjust the WEEKNUM formula.
Can try this as I got this as combination of 2 formula
=WEEKNUM(A1,1)-(INT((MONTH(A1)-1)/3)*13)
second part - INT((MONTH(A1)-1)/3) gives us the quarter number of previous quarter which then multiplied with 13 weeks/quarter gives us how many weeks have passed in all previous quarter before current quarter.
First part - "WEEKNUM(A1,1)" gives us the week number of current week in the year.
so by deducting all the previous weeks in previous quarters from current week number of year, we get the current week number in current quarter.

Excel weeknum function returns wrong week

Question is as in title.
I have a cell, "D4", with the date "09/07/2016" in it. Adjacent cell has formula "=weeknum(D4,1)". The output of this function is "28". But on a Sunday-Saturday basis, Saturday the 9th of July wasn't in week 28 - it was in week 27.
I thought this might be something to do with Saturday/Sunday and when the week starts and finishes, etc, so I tried multiple different dates from last week - Monday the 4th, Tuesday the 5th, Wednesday the 6th, etc. In each case, "weeknum" returns a value of "28".
I only noticed the problem because I have a macro which uses the value of the cell with the week number to look for a spreadsheet saved by our accounts team on a weekly basis. As they have - correctly - saved the spreadsheet as "week 27", it didn't work. I initially assumed that the accounts team were wrong, but I checked online and they are correct.
How could this happen? Surely Excel can't be wrong and I must have made a mistake of some sort?
I know that there is an issue with Excel not following ISO standards for when weeks 53 and 1 begin and end, but I don't see how that could affect a mid-year week.
It did occur to me that the issue might be to do with UK versus US date formatting. But, of course, the 7th of June wasn't in week 28 either.
That all is documented in WEEKNUM.
There are two systems used for this function:
System 1 The week containing January 1 is the first week of the
year, and is numbered week 1.
System 2 The week containing the first Thursday of the year is the
first week of the year, and is numbered as week 1. This system is the
methodology specified in ISO 8601, which is commonly known as the
European week numbering system.
Syntax
WEEKNUM(serial_number,[return_type])
Return_type Week begins on System
...
21 Monday 2
So =WEEKNUM("09/07/2016",21) will calculate as defined in ISO 8601 since Return_type 21 is the only one with System 2.
ISOWEEKNUM
=ISOWEEKNUM("09/07/2016")
will also do it.
=ISOWEEKNUM(a2)-1 returns the correct week for me, the same as =WEEKNUM(a2,21)-1

Using a dynamic single formula to calculate a date

This is a follow up to an earlier question.
#ForwardED I am trying to convert your original single static formula into a single dynamic formula.
Unfortunately my employer's filters will let me up certain things to a hyperlink, but will not let me download or view from the same site. I am also trying to come up with a formula for floating dates.
Below is a copy of the expanded explanation I gave on the original question. I am not sure if you missed it or not. It deals with holidays that have a set date like Christmas, 25th of December of every year. However if it fall on a Saturday the time of work is the Friday and if it is on a Sunday the day off is the Monday.
Dealing with a holiday falling on a Saturday or Sunday
Again we need to refer to some cell in your spreadsheet with the year so I will again use Q10 as the example and we will assume a date of 2014/10/24.
=IF(WEEKDAY(DATE(YEAR(Q10),12,25))=7,DATE(YEAR(Q10),12,24),IF(WEEKDAY(DATE(YEAR(Q10),12,25))=1,DATE(YEAR(Q10),12,26),DATE(YEAR(Q10),12,25)))
The formula checks first if the weekday is a Saturday. We do this using a function that will return the day of the week See step 2) from the original question. It is this part from the equation above:
WEEKDAY(DATE(YEAR(Q10),12,25))
It will return a single integer 1 through 7 corresponding to the day of the week the date function results in, in this case. If its a 1 we known its Sunday, if its 7 we know its Saturday. So the check for Saturday is:
WEEKDAY(DATE(YEAR(Q10),12,25))=7
If WEEKDAY()=7 is true then we provides the date of the day before which is really just subtracting 1 from the date we were looking at. We use this part of the formula to calculate that:
DATE(YEAR(Q10),12,24)
notice how I changed the day from 25 to 24. An alternate way would be to recycle our date and make the computer do one more calculation using this formula:
DATE(YEAR(Q10),12,25)-1
or
DATE(YEAR(Q10),12,25-1)
That all sits in the TRUE portion of the if statement. so if the date does not fall on Saturday then we wind up in the FLASE portion of the IF statement. Here we check with a second IF for the date falling on a Sunday. we use the same theory and process as we did for the Saturday check.
IF(WEEKDAY(DATE(YEAR(Q10),12,25))=1,DATE(YEAR(Q10),12,26),DATE(YEAR(Q10),12,25))
Placing an IF statement inside an IF statement is commonly referred to as "nesting". This whole IF statement happens in the FALSE portion of the previous IF that checked to see if it was Saturday. This time we checked for Sunday:
WEEKDAY(DATE(YEAR(Q10),12,25))=1
When this is true, then we need to increase the date by 1 day instead of decreasing it like was done for Saturday:
DATE(YEAR(Q10),12,26)
or
DATE(YEAR(Q10),12,25)+1
or
DATE(YEAR(Q10),12,25+1)
So that was the true portion of the Sunday check. Logically speaking the only way to get to the FALSE portion of this nested IF statement is to fail the Saturday check and then fail the Sunday check. Which means you do not need to go through and check if is the WEEKDAY comes out as 2, 3, 4, 5 or 6! Its one of those by the process of eliminating Sunday and Saturday (1 and 7). And if the date falls on Monday-Friday we dont need to change the date and can leave it just as is:
DATE(YEAR(Q10),12,25)
And I realized I did not explain how the date function works, though I think I tried to in one of the previous questions...regardless! DATE(arg1,arg2,arg3) requires three different arguments as integers or other functions that return integers.
arg1 is the year so 2014, 1995, 1965 are all acceptable integers. Also we could use YEAR(Q10), where the cell Q10 holds the date of 2014/10/24. In this case YEAR(Q10) would return 2014.
arg2 is the month and needs to be an integer in the range of 1 to 12. Again you can always use a formula that returns an integer in that range as well such as MONTH(Q10) which from our previous value of Q10 would return 10.
arg3 is day and similar to the above it needs to be an integer. A formula such as DAY(Q10) would return a value of 24.
What this means is if we know what day a holiday is on we can force it to a date by supplying a set month and day, and letting the year be determined by a formula that supplies the year you are interested in. So if you look at the last formula you can see we fixed the month at 12 and the day at 25. They year will be determine from the year of the date supplied in cell Q10.

How do I calculate a date by adding 30 calendar days, where end date must be business day?

I'm wondering if this is possible. I am creating a spreadsheet to track project due dates. Each project must be completed by the 30th calendar day, but must be turned in on a business day.
Currently, I am just adding 30 days to the start date but this means some due dates aren't always accurate. For example, if the 30th day is Saturday, April 2nd, then the real due date would be Friday April 1st.
Is there a way to construct a conditional such that the due date equals the 30th calendar day, unless that falls on a weekend / holiday, where it then falls on the next earliest business day?
I've been struggling to figure out a way to do this.
For English settings in Excel, with a date in A1, in B1 enter:
=IF(TEXT(A1+30,"DDDD")="Sunday",A1+28,IF(TEXT(A1+30,"DDDD")="Saturday",A1+29,A1+30))
This simple-minded approach only handles Saturdays and Sundays, not arbitrary holidays.
I would prefer more elegant way like using WORKDAY.INTL
=WORKDAY.INTL(A2+31,-1,1,E2:E)
Explanation: start date + 31 days (1 day more than maximum calendar days)
then subtract 1 working day - going to last previous working day
Reason: because this formula does know when are weekends (by using variables) and also knows to skip hollydays by a custom list.
here is an example sheet you can use
Revised after comment:
Try this:
=(A4+30)+CHOOSE(WEEKDAY(A4+30),1,0,0,0,0,0,-1)
Your date, in A4, + 30 days, then add an amount of days until the next workday. If A4 + 30 is a Saturday it will subtract 1 day, a Sunday will add 1.

Resources