How to calculate the weekend when counting a date range in Google Sheets? - date

I have the below columns
StartDate EndDate CountDay
01 May 20 05 May 20 ?
As you see, 01 May is Friday, so from 01-05 May if we count all days including weekend it will be 4 days.
What I want is on column "CountDay" it only counts the Workdays, not the weekend.
SO the expected result would be 2.
Anyone know how to do it using a formula in Google Sheets?

Do you consider Fridays as part of the weekend?
If yes, then you could also try the following formula:
=NETWORKDAYS.INTL(A10, B10,"0000111")
If not, please use this formula:
=NETWORKDAYS.INTL(A10, B10)
How the formulas work.
By using the function NETWORKDAYS.INTL we can "adjust" the weekend (non-working weekdays) to our liking.
In this case we account Fridays as our non-working weekdays by using as the 3rd parameter 0000111 instead of the default 0000011 where every 0 represents a working weekday and every 1 a non-working weekday.
(Very useful for people working part-time)
Someone who has part-time work on only Mondays, Wednesdays and Fridays and wants to calculate the working days Friday, 1 May 2020 - Tuesday, 30 June 2020 could adjust the formula to:
=NETWORKDAYS.INTL(A10, B10,"0101011")
As explained on the official Google help page for NETWORKDAYS.INTL
weekend – [ OPTIONAL – 1 by default ] – A number or string representing which days of the week are considered weekends.
String method: Weekends can be specified using seven 0s and 1s, 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.
Number method: Instead of using the string method above, a single number can be used. 1 = Saturday/Sunday are weekends, 2 = Sunday/Monday and this pattern repeats until 7 = Friday/Saturday. 11 = Sunday is the only weekend day, 12 = Monday is the only weekend day and this pattern repeats until 17 = Saturday is the only weekend day.

I just found how to do it:
=if(weeknum(A10)<weeknum(B10),B10-A10-2*(weeknum(B10)-weeknum(A10)),B10-A10)
something like that

Related

I'm trying to calculate 3 days from today, but not include weekend or holidays, but

The workday function should work for this. I have a Range called "Holidays". The problem is that workdays doesn't count the weekend days. I need to count the weekend days. BUT, if the 3rd day comes on a weekend or a holiday choose the next non-weekend or non-holiday day. A2 is the Effective Date of the contract, which is the start date. I'm trying to calculate the day on which the Earnest Money is due. See the attached chart as to how it should calculate.
=if(OR(A5="",A5>workday(A2,3,Holidays)),workday(A2,3,Holidays),"") is the formula I have but it works for Effective dates that fall on a Sunday, Monday, Tuesday and Wednesday, but not for Effective dates that fall on a Thursday, Friday, Saturday.
Starting from Today
Use this formula to get the dates starting from today
={ArrayFormula(
VLOOKUP(WEEKDAY(SEQUENCE(7,1,TODAY(),1),2),
{SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},2)),
BYROW(SEQUENCE(7,1,TODAY(),1),
LAMBDA(d, VLOOKUP(WEEKDAY(IF(IFNA(MATCH(d+3,F2:F13*1,0),"")="",d+3,d+4),2),
{SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},2)))}
Starting from Monday
={ArrayFormula(
VLOOKUP(WEEKDAY(SEQUENCE(7,1,TODAY()-WEEKDAY(TODAY(),3),1),2),
{SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},2)),
BYROW(SEQUENCE(7,1,TODAY()-WEEKDAY(TODAY(),3),1),
LAMBDA(d, VLOOKUP(WEEKDAY(IF(IFNA(MATCH(d+3,F2:F13*1,0),"")="",d+3,d+4),2),
{SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},2)))}
Starting from Monday using Lambda
Starting from the current week monday.
=ArrayFormula(LAMBDA(vl,wd,
{VLOOKUP(WEEKDAY(wd,2),vl,2),
BYROW(wd,LAMBDA(d, VLOOKUP(WEEKDAY(IF(IFNA(MATCH(d+3,F2:F13*1,0),"")="",d+3,d+4),2),vl,2)))})
({SEQUENCE(7),{"Monday";"Tuesday";"Wednesday";"Thursday";"Friday";"Saturday";"Sunday"}},
SEQUENCE(7,1,TODAY()-WEEKDAY(TODAY(),3),1)))
Explanation
You need the Calendar Year 20xx Legal Public Holidays to skip one day if matched the legal holidays date after adding 3 days.
Used formulas help
BYROW - SEQUENCE - TODAY - LAMBDA - VLOOKUP - IF - IFNA - MATCH - ARRAYFORMULA

Function that takes a start and end date and counts how many Sundays between those dates fell on the 1st of the month on kdb+

Creating a function that takes a start and end date and counts how many Sundays between those dates fell on the 1st of the month on kdb+, how would I do this?
The function needs to show how many times this has happened since 1950
Let's define a function which returns a weekday of its argument (of type date) first.
The underlying value of a date is the count of days from 1/1/2000 and we know that 1/1/2000 was Saturday. The next day was obviously Sunday, then Monday etc. and every 7th, 14th, 21st, etc. day after and before Jan 1, 2000 was Saturday too. So if we take a date modulo 7 we'll get a weekday number where 0 is Saturday, 1 is Sunday, etc. which leads us to the following definition.
weekday:{ `sat`sun`mon`tue`wed`thu`fri x mod 7 }
Now we can create a function that answers the original question:
sundaysThe1st:{[start;end]sum `sun=weekday dates where 1=`dd$dates:start+til 1+end-start }
start+til 1+end-start generates a list of dates between start and end, dates where 1=`dd$dates returns only the first days of the months and `sun=weekday dates returns 1b if the 1st day of the month is Sunday and 0b otherwise. sum is effectively the number of 1's which is exactly what we need.
Hope this helps.

Week Number restarting at 1 every month as per ISO format in Tableau

I am trying to get week numbers ( resetting at 1 for each month) as per ISO format for each month in 2019.For example I am interested in getting
All dates in July 2019: week 1 to 4,
All dates in Aug 2019 : week 1 to 4 and so on.
I first created the calculated field (Week_Number_ISO) to get the overall week number in year 2019.I used the following formula;
DATEPART('iso-week',[ Date]) which works as intended.
To get the monthly week number I used the following formula
INT((DATEPART('day',[Created Date])-DATEPART('iso-weekday',[Created Date])+7)/7)+1.
(Idea was to calculate the date of the first day of each week & then divide by 7 and take the integer part)
As per the ISO format, shouldn't July 29 to 31st be a part of week 4 for July?But the formula is showing it as week 5 for July 2019.I feel I am missing something in the formula or am missing something about ISO week number resetting at 1 for each month.
Can someone help me?
Here is an example of the dates in July 2019 and the associated week numbers.
Why would July 28th-July 31st 2019 be considered week 4?

Converting month to date format in access

I have an access column which has values like May, May-June, November, January-February etc.
Requirement is that
1) If only one month is there, I should create a transformation with a date column pointing to the last date of that month and the year would be the current year.
Eg May would be 5/30/2015, January would be 1/31/2015
2) If it is having two months, then second month must be taken and the same logic as in Point 1 should be implemented.
Eg May-June would be 6/31/2015, January-February would be 2/28/2015.
My first preference would be without using VBA code.
Please help.
You can use a one-liner in a function:
Public Function GetDateFromMonth(ByVal Months As String) As Date
Dim Ultimo As Date
Ultimo = DateAdd("d", -1, DateAdd("m", 1 + UBound(Split(Months, "-")), CDate(Split(Months, "-")(0) & "/1")))
GetDateFromMonth = Ultimo
End Function
Split the string on the "-", use the second month, add one to that month to get the next month, then take the first day of that month and subtract one from it. For example Jan-Feb : build 3/1/2015 and then subtract 1 day to get the last day of Feb.
Make a table that holds a list of Months:
MonthName MonthNumber
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12
Create this query:
SELECT InputData.Months, DateSerial(Year(Date()),[MonthNumber]+1,0) AS ReqdDate
FROM InputData, Months
WHERE (((IIf(InStr([Months],'-')=0,[Months],Mid([Months],InStr([Months],'-')+1)))=[MonthName]));
That's it. Output looks like this:
Months ReqdDate
May 31/05/2015
May-June 30/06/2015
November 30/11/2015
January-February 28/02/2015
March 31/03/2015
April-September 30/09/2015
Sorry about formatting - noob. I'm in Eu so my dates look odd to you, but you get the idea; you can make them US dates in a moment I'm sure. You can use the bits of this you need in your code in two secs.
If you don't like the lookup table for month number, I'm sure you can use some format function to turn the month name into a month number.

DB2 separate number of weeks per quarter from timestamp

I'm trying to separate weeks from timestamp per quarter so it should be between 1-13 week per quarter so I used function week() but it takes between 1-52 week as whole year so I made it to be divided by function of quarter like below
select Week (EVENTTIMESTAMP) / QUARTER (EVENTTIMESTAMP) from KAP
The thing here that results aren't accurate; for example it shows:
time stamp 2014-07-06 12:13:03.018
week number 9
which isn't correct because July is first month in Q3 and it's in the 6 days so it should be 1 week from Q3 not 9.
Any suggestion where it go wrong?
You want something like WEEK modulo 13 to get week number within a quarter. You will have to tinker with 'modulo 13 yields 0..12' by adding or subtracting one at appropriate points.
Some minimal Google searching using 'ibm db2 sql modulo' yields DB2 MOD function:
The MOD function divides the first argument by the second argument and returns the remainder.
Hence MOD(WEEK(...), 13), except you probably need MOD(WEEK(...)-1, 13) + 1, as intimated already.
You may need to watch for what the WEEK() function does at year ends:
The WEEK function returns an integer in the range of 1 to 54 that represents the week of the year. The week starts with Sunday, and January 1 is always in the first week.
I'm curious about how they can come up with week 54. I suppose it requires 1st January to be a Saturday (so 2nd January is the start of week 2) of a leap year, as in 2000 and 2028. Note that week 53 and (occasionally) week 54 will show up as weeks 1 and 2 of Q5 unless you do something. Also, Saturday 2000-03-25 would be the end of Q1 and Sunday 2000-03-26 would be the start of Q2 under the regime imposed by the WEEK() function and a simple MOD(WEEK(...), 13) calculation. You're likely to have to tune this to meet your real requirements.
There's also the WEEK_ISO() function:
The WEEK_ISO function returns an integer in the range of 1 to 53 that represents the week of the year. The week starts with Monday and includes seven days. Week 1 is the first week of the year that contains a Thursday, which is equivalent to the first week that contains January 4.
Note that under the ISO scheme, the 3rd of January can be in week 52 or 53 of the previous year, and the 29th of December can be in week 1 of the next year. Curiously, there doesn't seem to be a YEAR_ISO() function to resolve such ambiguities.
In a data warehouse, the proper solution to this is to create a time dimension that contains static mappings for days/weeks/months/quarters/years. This provides the ability to define these based on your business' fiscal calendar (if it is not following on the calendar year).
See: http://www.kimballgroup.com/1997/07/10/its-time-for-time/ for more information.