Excel year-week combination calculation - vba

I am currently making an excel model where the date is an important aspect of it. However as the information comes from a different source it is sometimes structured in such a way that it is not realistic.
I have a problem with the calculation of the year and week combination when adding weeks or subtracting them.
The start data looks like this:
yyyyww (example: 201525, year 2015 week 25)
Now if I want to add for example 3 weeks, I can just do that by adding 3, results is 201528. However when it comes to 201552 (and sometimes yyyy53) it is more difficult as I need to calculate to 2016.
The same goes for when calculating back in the time, towards 2014 or lower. Does anyone know or have a solution for this? Maybe in VBA? or a formula trick?

a formula to add 1 to your week number format looks like this:
=YEAR(DATE(LEFT(A2, 4), 1, 1) + MID(A2, 5, 2) * 7) * 100 +
WEEKNUM(DATE(LEFT(A2, 4), 1, 1) + MID(A2, 5, 2) * 7)
it will create a sequence of week numbers that includes: 201452 => 201453 => 201502
LEFT(A2, 4) and MID(A2, 5, 2) extract the year and week components
date(year, 1, 1) + (week - 1) * 7 converts that to an actual Excel Date value, e.g. 201502 => 2015-01-08
date(year, 1, 1) + week * 7 will create a date that is 1 week after the "current" date
year(date) * 100 + weeknum(date) converts that date back to yyyyww format

Related

SQL converting number of days to weeks

I have a SQL table with a column that says number of days, and contains entries like 23, 26, 45, etc...
I am trying to convert each entry to a "week number". In essence, what I mean is that if my day entry is between 0 and 6, then, this is Week 1, if it is 7 and 13, then this is Week 2, 14 and 20, week 3, etc... Is there an "efficient" way to do this in SQL?
Thanks.
Thomas.
You need just the standard divide function. It ignores the remainder:
SELECT (Days / 7) + 1
You can try this and no need to add +1;
SELECT (Days / 7.00)

SQL - Getting an integer relative to a date column

I have a date column called "Expiry_Date". What I've been asked to do is retrieve an integer based on the month relative to the current, for example:
Current Date: 7th May 2016
MyTable
----------
1) 12th May
2) 31st May
3) 2nd June
4) 11 June
5) 19 June
6) 28th July
7) 1st August
The result would be:
1) 1
2) 1
3) 2
4) 2
5) 2
6) 3
7) 4
So, it's getting the month number relative to the current for the given date (or periods). This also has to work across years so "1 January 2017" would be "9" etc.
Many thanks
Perhaps the function MONTHS_BETWEEN solves.
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions089.htm
Something like:
round(months_between(expiry_date, date '2016-05-07') + 1)

Rolling 6 Months

I have 2 parameters in my report, Month and Year.
I also have a table which in one column has the count of rows for that particular month and another column which has the count of rows for that specific month and the next 5 months (Rolling 6 month).
Both of these columns have an expression which links to the parameters as shown below:
Month Expression:
6 Month Expression:
My parameters use specific values which i have put in (1 - 12) so when my rolling month adds 1 value each time as you can probably guess this wont work when I come near the latter months of the year as my expression does not roll over to next year and just stops at December.
Overview: Parameters are integer values 1 - 12 and I am not sure how to calculate a rolling 6 months which will carry over to the next year when selecting months late in the year. e.g. If I select November as my Month parameter, my rolling 6 months will only display the sum of rows for November and December, not going to the next year. I am assuming this is because my month values are integers and in my expression I add numbers to the integers for each month therefore my rolling 6 will be trying to add months 11, 12, 13, 14, 15, 16 when obviously months only go to 12.
This is not an answer to your question, but looking at this, you may be able to find a solution with SQL itself.
See the query below. Consider this as your report source sql.
DECLARE #A TABLE (ID INT IDENTITY(1,1),DT DATE)
INSERT INTO #A (DT)
VALUES
('2013-01-26'),('2013-02-23'),('2013-03-20'),
('2013-04-23'),('2013-05-23'),('2013-07-23'),
('2013-08-23'),('2013-08-29'),('2013-09-23'),
('2013-12-10'),('2014-03-01')
If you join the result with itself, some what like below, you will get the result in sql query itself, you need to only show it in report. See below.
SELECT DATEPART(YEAR,DT) [Y],DATEPART(MONTH,DT) [M],COUNT(*) [ROLLING 6]
FROM (
SELECT A.*
FROM #A A,#A B
WHERE ((DATEPART(YEAR,B.DT) * 12) + DATEPART(MONTH,B.DT)) BETWEEN
(DATEPART(YEAR,A.DT) * 12) + DATEPART(MONTH,A.DT) AND
((DATEPART(YEAR,A.DT) * 12) + DATEPART(MONTH,A.DT) + 6)) LU
GROUP BY DATEPART(YEAR,DT),DATEPART(MONTH,DT)
ORDER BY [Y],[M]

Most efficient and easiest way in Excel VBA to classify date as week

I have one column that should either say Week 1, Week 2, Week 3, or Week 4.
I want to use VBA to enter the week based on what date is being looked at in a textfile...
For Example:
12/19/2013
12/01/2013
12/08/2013
12/26/2013
The output in Excel should be:
Week 3
Week 1
Week 2
Week 4
I am thinking of 4 if statements after using Split on the "/" and then converting the string to a number so it can be compared by an if statement but if I have days like "01" or "03", days starting with 0, wouldn't that cause problems in the if statements comparison?
Is there an easy way to do this in VBA?
As follow up from comments this one works:
Dim myDate as Date
myDate = DateValue("12/01/2013")
MsgBox "Week " & (1 + DateDiff("ww", DateSerial(Year(myDate), Month(myDate), 1), myDate))
There is a built in formula function for this.
http://office.microsoft.com/en-us/excel-help/weeknum-HP005209337.aspx
=weeknum(A1)

Finding the last day of the week

I have a table with a bunch of dates (option maturity dates to be precise). I need to query this database to find the last day of a specific week that is stored in the table.
All I will be given to query this table is the year, the month and the specific week. And based on this I need to find the date that is stored in the table that matches this.
I've created the following query to find this specific date March 28 2013
SELECT M_SETNAME, M_LABEL, M_MAT FROM OM_MAT_DBF
WHERE M_SETNAME = 'IMM_OSET '
AND MONTH(M_MAT) = 3
AND YEAR(M_MAT) = 2013
AND ((DATEPART(day,M_MAT)-1)/7 + 1) = 5
Do you guys have any idea of how I can change the last condition so that March 28th will be considered the 5th week of the month and not the 4th week as it is currently doing.
You can also use DATEPART to get the number of the week (in the year), but then, you could also get the 1st of each month, and take the week too so you can have: WEEK OF MY DATE - WEEK OF FIRST DAY FOR THIS MONTH + 1.
Here you have an example...
DECLARE #Dt datetime
SELECT #Dt='03-28-2013'
SELECT DATEPART( wk, #Dt) - DATEPART( wk, Convert(Date,Convert(varchar(4),YEAR(#Dt))
+ '-' + Convert(varchar(2), MONTH(#Dt))
+ '-' + Convert(varchar(2), 1))) + 1
EDIT: Also, looking at your code, you could add the CEILING. If the result == 2.7, it means it passed the 2nd week, however, it gets rounded to 2 when it should actually be 3.
If you add the CEILING and the CONVERT to decimal should work..
SELECT MONTH(#Dt),
YEAR(#Dt),
((CEILING(Convert(decimal,DATEPART(day,#Dt)-1)/7)) + 1)