FIRST FRIDAY OF THE YEAR - excel-formula

I have a formula to calculate the first Friday of the Year and it works okay.
I understand how the formula gets the answer however, I can't seem to find the reasoning behind the formula. The formula is:
=DATE(YEAR(TODAY()),1,8)-WEEKDAY(DATE(YEAR(TODAY()),1,2))
This gives us 2020/01/08 - 5 = 2020/01/03 which is the First Friday of the Year. But why does the formula choose 8 and 2 as the dates?
Can someone please explain the reason.

The first, obvious, part of the formula is that DATE(YEAR(TODAY()),1,8) gives you the eighth day of the month which can't be the first Friday since one of the 7 days before it would must be a Friday.
My guess is that the second part has a little bit of kludgery going on. WEEKDAY(DATE(YEAR(TODAY()),1,1)) would give you the number of days from the first day of the month to the previous Sunday. But if you're looking for the previous Saturday you'd need WEEKDAY(DATE(YEAR(TODAY()),1,1)) + 1 or WEEKDAY(DATE(YEAR(TODAY()),1,2)).
Finally, the day of the week WEEKDAY(DATE(YEAR(TODAY()),1,2)) before the 1st of the month is the same day of the week before the 8th of the month.

Related

Formula that translates a date to Fiscal Year, Fiscal Period, and Fiscal Week

Working on a formula that will take a date and translate it to the format FYxxPxxWx.
For example. Input the date of 03/22/20 and the formula will give you FY20P06W4 which is correct.
However if you input 02/02/20 the formula will give you FY20P05W2. The correct output would be FY20P05W1. This issue also rears its head with the date 09/29/19. It gives you FY20P12W5. The correct output would be FY20P1W1.
Something else weird happens when you put in the date 04/5/20 you get FY21P07W2 when it should be FY20P07W2.
The formula is
=CONCATENATE("FY",RIGHT(YEAR(DATE(YEAR(D5),MONTH(D5)+(10-1),1)),2),"P",TEXT(CHOOSE(MONTH(D5),4,5,6,7,8,9,10,11,12,1,2,3),"0#"),"W",WEEKNUM(D5,1)-WEEKNUM(DATE(YEAR(D5),MONTH(D5),1),1)+1)
I think this issue is caused by the strange weeks where the the month ends and another begins throwing off the formula.
I do have a formula that calculates the years fiscal year start date
=(DATE(YEAR(TODAY())-1,10,1)-(WEEKDAY(DATE(YEAR(TODAY())-1,10,1),1)))+1
This outputs 09/29/19 as the start date of the Fiscal year as its the same week as 10/1/19 which is the first month of the fiscal year. IF that makes sense.
The separate formulas are
For FY and grabs only last two digits of year
RIGHT(YEAR(DATE(YEAR(D5),MONTH(D5)+(10-1),1)),2)
For Period (gives me a two digit Period
TEXT(CHOOSE(MONTH(D5),4,5,6,7,8,9,10,11,12,1,2,3),"0#")
For Week
WEEKNUM(D5,1)-WEEKNUM(DATE(YEAR(D5),MONTH(D5),1),1)+1)
I believe I have a solution for you. Discussion to follow, but here's the full formula:
=CONCAT("FY",RIGHT(YEAR(D5+91+WEEKDAY(DATE(YEAR(D5),10,1))),2),"P",TEXT(IF(MONTH(D5+(7-WEEKDAY(D5)))<>MONTH(D5),IF(MONTH(D5)=9,1,CHOOSE(MONTH(D5),5,6,7,8,9,10,11,12,1,2,3,4)),CHOOSE(MONTH(D5),4,5,6,7,8,9,10,11,12,1,2,3)),"0#"),"W",ROUNDUP(((D5-IF(MONTH(D5+(7-WEEKDAY(D5)))<>MONTH(D5),DATE(YEAR(D5),MONTH(D5)+1,1)-WEEKDAY(DATE(YEAR(D5),MONTH(D5)+1,1))+1,DATE(YEAR(D5),MONTH(D5),1)-WEEKDAY(DATE(YEAR(D5),MONTH(D5),1))+1))/7)+0.01,0))
One issue is that this still calculates 2/2/2020 the way you said was incorrect. When I verify it against a calendar, though, it seems that FY20P05W02 should be correct. If the week that includes the first of the month begins a new pay period, that would mean 2/1/2020, falling on a Saturday, would be the last day of fiscal week 1. That would make 2/2/2020 the first day of fiscal week 2.
To calculate fiscal year, I used RIGHT(YEAR(D5+91+WEEKDAY(DATE(YEAR(D5),10,1))),2). Since you can count on there always being 91 days from the beginning of October to the end of December, it helps with this calculation. In your formula, you had MONTH(D5)+(10-1), which would push you 9 months out past the month in D5. This explains why your result for 4/5/2020 was off by a year.
Fiscal period was a bit trickier, requiring a couple nested IF statements. I used IF(MONTH(D5+(7-WEEKDAY(D5)))<>MONTH(D5) first to account for days at the end of the month that would fall into the next fiscal period, then IF(MONTH(D5)=9 to account for the few days at the end of September that might fall into the next fiscal year. Days at the end of September would default to 1, days at the end of a month that are included in the next fiscal period use the first CHOOSE function (they need the next month's number), and everything else gets the CHOOSE function as you wrote it.
The fiscal week took a bit more, but in the end I evaluated the beginning of the current fiscal month and subtracted it from the date in D5, then divided by 7 and added 0.01 so that even numbers would round up correctly.
I tested this out over a few years of dates and it seemed to be functioning correctly, but let me know if you have questions or issues.
One thing to consider when using WEEKNUM is that you'll have a week that is counted twice at the beginning of the year unless you use option 21 or ISOWEEKNUM. These give the same result as each other, and ensure that only one week number is assigned to any given day, no matter the year.

Excel formula: Find Date from Weeknum and weekday

I am trying to find the date out of a weeknum, weekday and year.
There are many information on the web but after several attempts, I couldn't find what I was looking for.
So far, I have this formula:
=DATE(AZ22,1,AZ3*7-5)-WEEKDAY(DATE(AZ22,1,3))
AZ22=YEAR
AZ3=WEEKNUM
Unfortunately it doesn't seem to work.
In order to add some complexity, my weeks should start for Thursday (Thursday 1st day of the week).
If you are so kind to provide an answer, could you please post the explanation of the formula as well?
as JvdV asked, the output should be a date like
01 dec 2019.
I need this formula to map a current date (and I can't use date-364) with the last two years.
The process works as follow:
I have the current date (date A)
The current week ( of date A) is mapped with a different week last year
From the week (and eventually the WEEKDAY) I should find my date B
I hope it clarifies my request
Thank you,
Use this:
=DATE(A2,1,(B2-1)*7-WEEKDAY(DATE(A2,1,B2*7),14)+C2)
This is just math. But by adding the 14 to the Weekday we start our week on Thursday. This does assume that 2019-1-3, the first Thursday of this year, is the start of the 2nd week. If that is not the case and the week that started on the 3rd is the first week, change the (B2-1) to just B2

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.

How to calculate DOT week numbers in Excel

I apologize if this has been answered elsewhere, but I have searched the other week number answers and haven't found a solution that works for multiple years.
The tire industry calculates week numbers starting with the first full week that begins on a Sunday. For example, in 2016 Week 1 commenced with 3 January. In 2017, Week 1 will begin on Sunday, 1 January. In 2018 Week 1 will start with Sunday, 3 January.
In Excel 2010, using returns type 1 and 17 (week starting on Sunday), 1 January for all three years is Week 1 when that should only be correct for 2017. It should return Week 201552 for 2016 and 201653 for 2018
I have tried the examples posted in other answers and also checked Ron de Bruin's page with his formulas for calculating the week number, but I've been unable to modify it correctly to get the formula to work consistently.
Here is Ron's example for calculating ISO week numbers:
=INT((B4-DATE(YEAR(B4-WEEKDAY(B4-1)+4),1,3)+WEEKDAY(DATE(YEAR(B4-WEEKDAY(B4-1)+4),1,3))+5)/7)
Thanks in advance for any suggestions or guidance.
You can use this formula:
=IFERROR(YEAR(B4) &TEXT(INT(DATEDIF(DATE(YEAR(B4),1,AGGREGATE(15,6,{1,2,3,4,5,6,7}/(WEEKDAY(DATE(YEAR(B4),1,{1,2,3,4,5,6,7}))=1),1)),B4,"d")/7)+1,"00"),YEAR(B4)-1 &TEXT(INT(DATEDIF(DATE(YEAR(B4)-1,1,AGGREGATE(15,6,{1,2,3,4,5,6,7}/(WEEKDAY(DATE(YEAR(B4)-1,1,{1,2,3,4,5,6,7}))=1),1)),B4,"d")/7)+1,"00"))
I see this question has been answered way back but for future reference.....
I have previously posted formulas which give ISO week number - see here ...and that formula can be adjusted depending on how the start day or date is defined, so to get the YEAR&WEEKNUMBER for a date in A2 where week 1 starts on the first Sunday of the year you can use this formula
=YEAR(A2+1-WEEKDAY(A2))&TEXT(INT((A2-WEEKDAY(A2)-DATE(YEAR(A2+1-WEEKDAY(A2)),1,7))/7)+2,"00")

Resources