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.
Related
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
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.
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?
I need help on calculating my start date for my report date parameters.
The end date will always be the last Sunday, here: =DateAdd("d", 1 - WeekDay(Today(), 1), Today())
What I need help with is how to write a formula to go back 6 months from today and pick the 1st Saturday in that range..
Thanks in advance.
Assuming your start day is a Sunday then you can use this...
=DATEADD(
DateInterval.Day,
7 - WEEKEDAY( DATEADD(DateInterval.Month,-6,Today()), FirstDayOfWeek.Sunday),
DATEADD(DateInterval.Month,-6,Today())
)
This works as follows
WEEKEDAY( DATEADD(DateInterval.Month,-6,Today()), FirstDayOfWeek.Sunday)
Takes today's date, subtracts 6 months and then finds out what daynumber that is. Running that today (2018-11-08) gives use (2018-05-10) which is a Thursday, this is day number 5
Saturdays are day number 7 (if your first day of week is a Sunday). As there can be no higher number than 7 we can do a simple subtraction of 7 minus the day number we landed on (from above) which gives us a required adjustment of 2 days.
Finally the outer DATEADD function simply says add our calculated 2 days to the date 6 months ago.
Hope that makes sense!?
If the first day of the week is not a Sunday for you then you may have to do some Mod% calc on the second argument to calculate the correct number of days to adjust by.
The MS SQL DateDiff function counts the number of boundaries crossed when calculating the difference between two dates.
Unfortunately for me, that's not what I'm after. For instance, 1 June 2012 -> 30 June 2012 crosses 4 boundaries, but covers 5 weeks.
Is there an alternative query that I can run which will give me the number of weeks that a month intersects?
UPDATE
To try and clarify exactly what I'm after:
For any given month I need the number of weeks that intersect with that month.
Also, for the suggestion of just taking the datediff and adding one, that won't work. For instance February 2010 only intersects with 4 weeks. And the DateDiff calls returns 4, meaning that simply adding 1 would leave me the wrong number of weeks.
Beware: Proper Week calculation is generally trickier than you think!
If you use Datepart(week, aDate) you make a lot of assumptions about the concept 'week'.
Does the week start on Sunday or Monday? How do you deal with the transition between week 1 and week 5x. The actual number of weeks in a year is different depending on which week calculation rule you use (first4dayweek, weekOfJan1 etc.)
if you simply want to deal with differences you could use
DATEDIFF('s', firstDateTime, secondDateTime) > (7 * 86400 * numberOfWeeks)
if the first dateTime is at 2011-01-01 15:43:22 then the difference is 5 weeks after 2011-02-05 15:43:22
EDIT: Actually, according to this post: Wrong week number using DATEPART in SQL Server
You can now use Datepart(isoww, aDate) to get ISO 8601 week number. I knew that week was broken but not that there was now a fix. Cool!
THIS WORKS if you are using monday as the first day of the week
set language = british
select datepart(ww, #endofMonthDate) -
datepart(ww, #startofMonthDate) + 1
Datepart is language sensistive. By setting language to british you make monday the first day of the week.
This returns the correct values for feburary 2010 and june 2012! (because of monday as opposed to sunday is the first day of the week).
It also seems to return correct number of weeks for january and december (regardless of year). The isoww parameter uses monday as the first day of the week, but it causes january to sometimes start in week 52/53 and december to sometimes end in week 1 (which would make your select statement more complex)
SET DATEFIRST is important when counting weeks. To check what you have you can use select ##datefirst. ##datefirst=7 means that first day of week is sunday.
set datefirst 7
declare #FromDate datetime = '20100201'
declare #ToDate datetime = '20100228'
select datepart(week, #ToDate) - datepart(week, #FromDate) + 1
Result is 5 because Sunday 28/2 - 2010 is the first day of the fifth week.
If you want to base your week calculations on first day of week is Monday you need to do this instead.
set datefirst 1
declare #FromDate datetime = '20100201'
declare #ToDate datetime = '20100228'
select datepart(week, #ToDate) - datepart(week, #FromDate) + 1
Result is 4.