count how many specific days are in a time period - google-sheets

I want to count say how many Mondays we have from 2022-02-01 - 2022-03-01. I found smth like this:
=SUMPRODUCT(WEEKDAY(B4:C4)=2) - B4 and C4 are the dates
But it returns 0. I assume it only checks if specific date is the specific day. Any ideas how I can do this but for a date range? So how count how many Mondays there are in February
I also found this
=NETWORKDAYS.INTL(B4;C4;"1000000")
but this returns 25

You can take advantage of the NETWORKDAYS.INTL function by using the string method to make all the days as weekend except for Monday.
The String method states:
weekends can be specified using seven 0’s and 1’s, where the first number in the set represents Monday and the last number is for Sunday. A zero means that the day is a work day, a 1 means that the day is a weekend. For example, “0000011” would mean Saturday and Sunday are weekends.
In this case since you only want to know the Mondays, the string would be "0111111" and the function would look like:
=NETWORKDAYS.INTL(StartDate,EndDate,"0111111")

I think this is right. It's counting inclusively so you would get five Mondays starting on Monday 7th Feb 2022 and ended on Monday 7th March 2022 for example.
=floor((B2-(A2+7-weekday(A2,12)))/7)+1
where A2 and B2 contain the start date end end date.
Obvs nul points for me again but for the record this could be generalised if you put the day number in C2 (e.g. 1 if you want to find Sundays, 2 for Mondays):
=floor((B2-(A2+7-weekday(A2,10+C2)))/7)+1

Related

EOMONTH returns the 1st day of the next month for months with 30 days

When I use the formula below the results of the EOMONTH function
returns the start of the next month for any month with 30 days instead of the last day of the specified month. The month and years are correct, so I'm pretty sure it's EOMONTH when used in another function.
For example,the results in B3 should be "11/31/1965" but it returns "12/1/1965".
=DATE(YEAR(B2),MONTH(B2)+6,DAY(TEXT(EOMONTH(MONTH(B2)+6,0))))
I have tried subtracting a day, but it returns the end-of-month -1 for months with 31 days (30). So I have the same problem in the other case.
I have also used IFS() to account for months with 30 days, and it miscalculates the date the same way.
=IFS( MONTH(B2)+6 = 4,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
6,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
9,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
MONTH(B2)+6 =
11,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0))-2) ,
TRUE ,DATE(YEAR(B2),MONTH(B2)+6,DAY(EOMONTH(MONTH(B2)+6,0)) ) )
The EOMONTH function by itself where I just pass in the date as a string works correctly (column F).
Any Idea on what I'm doing wrong?
Thanks in advance.
Yes, as #player0 has explained, you can't just add something to a month and feed it into eomonth. Try putting
=eomonth(month(B2)+6,0)
into B3 (formatted as a date).
You get
1/31/1900
Why? month(b2)+6 gives 11 (which is just a number). Dates in google sheets are represented as days since 12/31/1899. So 11 formatted as a date gives 1/11/1900. Applying eomonth to that gives the last day of January 1900, which is the 31st. Feeding that into your formula would give 11/31/65, but that date doesn't exist, so you get 12/1/65.
If you want to go forward 6 months and then get the last day of the month, you need
=eomonth(date(year(B2),month(B2)+6,1),0)
You can also use the Edate function, which does not roll over into the first day of the next month:
=eomonth(edate(B2,6),0)
EOMONTH does not understand MONTH. instead, it converts it into date. to use EOMONTH you need to supply it with valid date
=EOMONTH(B2, 0)

Show only the current Week

I am working with Google sheets, and the Form responses that get poured into it. I am wondering if there is a way to show the CURRENT work week? What I mean by this is a sheet that shows this Friday:
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY
124 123 193 -- 234
344
But then the next Monday:
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY
088 -- -- -- --
Is this possible, or is it to much for google sheets?
It's certainly possible. To begin with, I would put dates under Monday, Tuesday, etc: this simplifies subsequent computations, and is also helpful when reading the data. This would go under "Monday": it returns the Monday of the current workweek:
=today()-weekday(today())+2
Here +2 compensates for subtraction when the date is Monday (which is numbered 2). The other days of the week can be obtained by adding 1 to Monday (=A2+1), or directly by using =today()-weekday(today())+3 and so on.
Then you can pull data from the form sheet using functions such as query, filter, vlookup, ... For example, this formula returns the 3rd column from Form Responses 1 where the date is nearest to the content of A2 (current Monday)
=vlookup(A2, 'Form Responses 1'!A:A, 3)
A possible issue here is that Form Responses record both date and time, so the "nearest" record may be from 11:50pm of previous day. One way to solve this is to use filter:
=filter('Form Responses 1'!C:C, floor('Form Responses 1'!A:A) = A2)
This returns all C column entries from the form where the date in the A column matches the content of A2.

Excel Formula: weekly comparison with corresponding week of previous month

I'm trying to create a week on week comparison in Google Sheets.
The nuance is that the Comparison Week needs to dynamically populate as the corresponding week of the previous month.
So if the dataset is the first week of May, the Comparison Week would be the first week of April. Likewise, week 2 of May would be compared to week 2 of April, etc.
As an example, if A1 is 5/1/2016 and A2 is 5/7/2016, cells B1 and B2 should populate as 4/3/2016 and 4/9/2016 respectively (where Sunday is the first day of the week).
I've built the following formula which I think gets me about halfway:
=WEEKNUM(A1)-WEEKNUM(DATE(YEAR(A1),MONTH(A1),1),1)+1
This formula returns the week number in the month from a given date. For example, 5/1/2016 returns the value '1' because the date occurs on the first week of the month. 5/8/2016 would return '2', etc.
Use formula below. It looks ugly, but it should work exactly as your example.
As an example, if A1 is 5/1/2016 and A2 is 5/7/2016, cells B1 and B2
should populate as 4/3/2016 and 4/9/2016 respectively (where Sunday is
the first day of the week).
=DATE(YEAR(A1),MONTH(A1)-1,DAY(A1)+IF(WEEKDAY(A1-DAY(A1)+1)=1,8,WEEKDAY(A1-DAY(A1)+1))-WEEKDAY(DATE(YEAR(A1),MONTH(A1)-1,1),1))
here is how it works
=DATE(
YEAR(A1),
MONTH(A1)-1,
return date of previous month with day calculated as follows:
DAY(A1)
start at the current date
+IF(WEEKDAY(A1-DAY(A1)+1)=1,8,WEEKDAY(A1-DAY(A1)+1))
find out what day is the first day of the current month and add its number. Only if it is Sunday (1st day of week) it needs to skip week 0 so it adds 8
-WEEKDAY(DATE(YEAR(A1),MONTH(A1)-1,1),1)
deduct number representing first day of previous month
So for example if you have 5/3/2016 it does:
(3rd day of month) + (first day of current month in Sunday, so add 8) - (first day of previous month is Friday, so it deduct 6)
3 + 8 - 6 =5 and this is day of previous month, so result is 4/5/2016

Get date from week number in Google Sheets

If I have week 7 in 2017 what week date is the Monday in that week in Google Sheets?
=DATE(B9,1,1)-WEEKDAY(DATE(B9,1,1),3)+7*(WEEKDAY(DATE(B9,1,1),3)>3)+7*(A9-1)
is the least complicated formula I know which works for week numbers in Sweden (i.e. Monday first day of week, ISO rules for what is week 1).
Short answer (A1==Week, B1==Year):
=DATE(B1;1;1)+((A1-1)*7)-WEEKDAY(DATE(B1;1;1);3)
Long answer:
DATE(<year>;1;1) // days since 1970 until the frist day of the year
plus
((<week number>-1)*7) // how many days into the year is this week
minus
WEEKDAY(DATE(<year>;1;1);3) // how many extra days from previous year in first week
PS:
This assumes monday as the first day of week you have to change the arguments for WEEKDAY to change it to sunday
Because of this definition (https://en.wikipedia.org/wiki/Week) the 4th of January must be used instead the 1st. The 4th of January is the first day which is always in the week 1.
=DATE(B1;1;4)+((A1-1)*7)-WEEKDAY(DATE(B1;1;4);3)
If you are using ISO weeks, the accepted answer doesn't account for weeks overlapping on 2 technical years like 2020-w53, which is from 28 Dec 2020 until 3 Jan 2021.
Therefore I'm using this formula instead:
=DATE(K2,1,1)-WEEKDAY(DATE(K2,1,1),2)+7*(WEEKDAY(DATE(K2,1,1),2)>3)+7*(L2-1) +1
Where K is the Year, and L is the Week number (split in 2 columns from yyyy-ww)
to have it in an arrayformula:
=ArrayFormula(if(K2:K="",, DATE(K2:K,1,1)-WEEKDAY(DATE(K2:K,1,1),2)+7*(WEEKDAY(DATE(K2:K,1,1),2)>3)+7*(L2:L-1) +1 ))
You can use =ArrayFormula(if(E2:E="",,split(E2:E,"-"))) to split yyyy-ww in two columns.
NOTE: This formula would return the Monday (Which is the first day of the week in international standard, ISO)
Worked this up for 2023. It will work through end of 2024 too .. that said the AND logic is flawed .. feel free to suggest something to make this better
=IFS(
AND(ISOWEEKNUM(A8)=52,YEAR(A8)<>YEAR(A7)),
DATE(YEAR(A8-1),1,1)-WEEKDAY(DATE(YEAR(A8-1),1,1),3)+7*(WEEKDAY(DATE(YEAR(A8-1),1,1),3)>3)+7*(ISOWEEKNUM(A8)-1),
DATE(YEAR(A8),1,1)-WEEKDAY(DATE(YEAR(A8),1,1),3)+7*(WEEKDAY(DATE(YEAR(A8),1,1),3)>3)+7*(ISOWEEKNUM(A8)-1)
)

'weeknum' function

Here is what I'm trying to do:
I have three Spreadsheets.
(1) Days (holds increase in user-nr per day)
(2) Weeks (is supposed to sum up user increases so they are shown each week)
(3) Months (is supposed to sum up user increases so they are shown each month)
To give an example: if we have 10 users on Monday, 20 more on Tuesday, 15 more on Wednesday (that's when the next calendar week starts), then I want in the sheet "weeks" to see e.g. 45 users in calendar week 27 or so.
So what I try is this: =SUMIF(WEEKNUM(Days!A2:A977); A2; Days!B2:B977)
A holds the date of the day
B holds the number of users.
What happens is it does not sum up the number of the users shown in B, but only gives the number in the first cell of the weeknumber shown in A2.
What is my mistake?
The formula seems to be correct, but two things are needed.
You should embrace it in ArrayFormula()
You should use one more weeknum()
=ArrayFormula(sumif(weeknum(Days!A2:A977);weeknum(Days!A2);Days!B2:B977))

Resources