How can I Format the Date so that the fiscal week starts in December? - vba

I want to format a date as follows: Y17W15, but there is no option to set the start of the year. However, there is no consistent way of calculating this. I cannot just subtract a month (other times I will need to show the month too), and I cannot just add 4 or 5 to the week field due to leap years, etc.
Our year starts on a Saturday that is closest to December 1. This means if November 30 is on a Saturday, the Fiscal Year will start on November 30.
Currently what I have is below, which works fine except it shows Y17W10. The easiest option in my head is to have a way to actually set the start of a fiscal year, but if I have to go through a bunch of if statements it's okay as long as it works.
MsgBox(Format(Now, """Y""yy""W""mm"""))
Thanks for your time!
I am aware that: Given a 4-5-4 calendar and a date, how do I determine what fiscal week that date falls in? exists but I am looking for an answer that isn't as hard-coded.
Michael

Related

Oracle Week Number from a Date

I am brand new to Oracle. I have figured out most of what I need but one field is driving me absolutely crazy. Seems like it should be simple but I think my brain is fried and I just can't get my head around it. I am trying to produce a Sales report. I am doing all kinds of crazy things based on the Invoice Date. The last thing I need to do is to be able to create a Week Number so I can report on weekly sales year vs year. For purposes of this report my fiscal year starts exactly on December 1 (regardless of day of week it falls on) every year. For example, Dec 1-7 will be week 1, etc. I can get the week number using various functions but all of them are based on either calendar year or ISO weeks. How can I easily generate a field that will give me the number of the week since December 1? Thanks so much for your help.
Forget about the default week number formats as that won't work for this specific requirement. I'd probably subtract the previous 1 December from invoice date and divide that by 7. Round down, add 1 and you should be fine.
select floor(
(
trunc(invoiceDate) -
case
-- if December is current month, than use 1st of this month
when to_char(invoiceDate, 'MM') = '12' then trunc(invoiceDate, 'MM')
-- else, use 1st December of previous year
else add_months(trunc(invoiceDate, 'YYYY'), -1)
end
) / 7
) + 1
from dual;

How do I write query if I want 1st day of the month falls in 1st week in T-SQL

How do I write query if I want 1st day of the month falls in 1st week. my report needs to show data from SUN-SAT.so,
if I run the report on anyway current week, it should only show the data for the previous week from SUN-Sat.
Even though 10/27 falls in the 5th week of October , I am required to show as 10/27- 11/02 falls in the first week of November since November 1 falls in the first
week of November.
Here it is how I want to display the date-range in my report for the month of November. and the same logic applies
for every month.
Week 1 10/27 to 11/02
Week 2 11/03 to 11/09
Week 3 11/10 to 11/16
Week 4 11/17 to 11/23
Week 5 11/24 to 11/30
so, I am not counting till 4th week of October not the 5th one because I am counting 5th week as week 1 for
November.
like wise, the first week of JAN will be 12/29 till 01/04 . I don't want to count 5th week of December because if
I count, there will be duplication.
Thank you. I appreciate it.
In simpler terms, an entire week 'belongs' to a specific month based on whatever month the Saturday of that week lies in. Then you want to work backwards and calculate for a given month, the set of all weeks that belong to that month.
This will give you the forward calculation:
declare #date date = getdate();
select month(dateadd(day,7+datediff(day,#date,'2000-01-01')%7,#date))
And the reverse calculation:
declare #year int = 2013
declare #month int = 11
select
datefromparts(#year,#month,1) as month,
dateadd(day,i*7+datediff(day,datefromparts(#year,#month,1),'2000-01-01')%7-6,datefromparts(#year,#month,1)) week_start,
dateadd(day,i*7+datediff(day,datefromparts(#year,#month,1),'2000-01-01')%7 ,datefromparts(#year,#month,1)) week_end
from (values(1),(2),(3),(4),(5)) wk(i)
I use datediff(day,#date,#known_date) instead of datepart(dw,#date) because it is deterministic.

Algorithm to convert fiscal periods to calendar periods

What I am trying to do
I am somewhat desperately trying to build an algorithm which converts financial figures from different companies from fiscal periods to calendar periods.
The problem
Fiscal periods often do not correspond to calendar periods, e.g. a company might report fiscal year 2011 revenues of 100 USD but its fiscal year does not end at the end of December 2011 but instead on September 2011. For instance, Apple's fiscal year ends end of September. Dell's fiscal year ends end of January and Intel's fiscal year ends end of December. For Apple and Dell, all fiscal quarter and fiscal half year ends are shifted as well.
In order to compare revenues or other financial metrics among these companies, I need to be able to convert each fiscal period into equivalent calendar periods. For instance, someone might ask, how much revenue each company generated in calendar year 2011.
In the case of Apple Corp., we would need to remove the revenues which have been generated in calendar period 2010, which would equal Apple's fourth quarter of FY2010 and add the first quarter of fiscal year 2012 (which ended December 2011).
What I have (data model)
My data model has the following attributes for each entity calendarPeriod and fiscalPeriod:
endYear (year in which the period ends)
endMonth (month number
1..12 at which last day the period ends)
length (number of months 1..12 of the period)
What I need (desperately)
What would be the most efficient and short algorithm I could accomplish this?
It would be great if the algorithm could handle "special situations" like Dell where it would need to take 1/3 of its fiscal first quarter, which ends awfully on January, of the following year and adding it to the last quarter of the preceding year. In addition the algorithm should be flexible enough to handle all period lengths and endMonths and try to combine periods if necessary (for instance for the first half of calendar year 2012, it should try to find a six month period which ends June 2012 or consecutively add two periods (one ending March 2012 and one ending June 2012 or taking a fiscal year which ends June 2012 and subtracting the quarters or half year which fall in calendar year 2011).
Thank you so much.
This is more of a financial question then technical question.
If you have end of year results only, there is no practical way you could compare unless they are for same financial period.
Further, if you somehow manage to get the monthly results and do some juggling to prepare comparable results, they will not be comparable as there are many accounting adjustments and provisions generally done in the end of year financial result and not in monthly results which you will miss here.
I would suggest that you should try to compare those results which yields more meaningful results.

Get the EOM and MOM of any month in SQL Server 2005

I've periods of time every 15 days, MOM and EOM.
What I need to check is if a date value on a date field is prior to the current period minus 1.
For example, if today is 12/29, the period is 12/31, and i need to check
if prior < 12/15
How can i get the EOM (End Of Month, i mean, the last day of the month) and the MOM (Middle of month, it's like every 15th of month) with GETDATE() function without doing a DATEADD with -15 days (because in feb will be fail, and i don't care the month)
Any help or work around will be preciated.
Thanks
If you need the value 15 then put it in your code.
If that is against your company's policies then challenge the person that made that policy. Writing 5 lines of code to replace two characters is not a good coding...
If writing the 5 lines made your application much more flexible then maybe I could understand, but you are still "hard coding" 15 into your comparisons.
Thinking in a work around, what I did it was this:
If actual day < 15 then get the month actual, convert to the first day of the month (01) and minus 1 day. I get the last day of the prior month. (EOM - 1 period)
If actual day > 15, then the prior period (MOM - 1 period) is: 15 of actual month.
It's a query with if structure.
If someone has a better answer, please answer it and I'll be accept it.
Thanks :)

Access query (SQL) to return records sorted (grouped by) WEEKS

Greetings SQL gurus,
I don't know if you can help me, but I will try. I have several large databases grouped by year (each year in a different database). I want to be able to compare values from a particular week from one year to the next. For example, "show me week 17 of 2008 vs. week 17 of 2002."
I have the following definition of weeks that ideally I would use:
Only 52 weeks each year and 7 days a week (that only takes 364 days),
The first day of the first week starts from January 2nd - which means we do not use January 1st data, and
In leap year, the first day of the first week ALSO starts from the January 2nd plus we skip Feb. 29.
Any ideas?
Thanks in advance.
Best to avoid creating a table because then you have to update and maintain it to get your queries to work.
DatePart('ww',[myDate]) will give you the week number. You may run into some issues though deciding which week belongs to which year - for example if Jan 1 2003 is on Wednesday does the week belong as week 52 in 2002 or week 1 in 2003? Your accounting department will have a day of the week that is your end of week (usually Sat). I usually just pick the year that has the most days in it. DatePart will always count the first week as 1 and in the case of the example above the last week as 53. You may not care that much either way. You can create queries for each year
SELECT DatePart('ww',[myDate]) as WeekNumber,myYearTable.* as WeekNumber
FROM myYearTable
and then join the queries to get your data. You'll loose a couple days at the end of the year if one table has 52 weeks and one has 53 (most will show as 53). Or you can do it by your weekending day - this always gives you Saturday which would push a late week into the following year.
(7-Weekday([myDate]))+[myDate]
then
DatePart('ww',(7-Weekday([myDate]))+[myDate])
Hope that helps
To get the week number
'to get the week number in the year
select datepart( week, datefield)
'to get the week number in the month
select (datepart(dd,datefield) -1 ) / 7 + 1
You don't need to complicate things thinking about leap years, etc. Just compare weeks mon to sun
SInce you havea a specifc defintion of when the week starts that is differnt that the standard used by the db, I think a weeks table is the solution to your problem. For each year create a table that defines the dates contained in each week and the week number. Then by joining to that table as well as the relevant other tables, you can ask for just the data for week 17.
Table structure
Date Week
20090102 1
20090103 1
etc.
I needed to create a query that shows BOTH year AND week numbers, like 2014-52. The year shows correct when you use the Datepart() formula to convert week 53 to week 52 in the previous year, but shows the wrong year for the week that was week 1 previously that should be week 52 now. It show that week as 2015-52 instead of 2014-52.
Furthermore, it sorts the data wrong if you only use only the week number, eg:
2014-1,2014-11,2014-2
To overcome this I created the following query to insert a 0 and also to check for days in week 1 that should still fall under week 52.
ActualWeek: IIf(DatePart("ww",[SomeDate],1,3)=52 And DatePart("ww",[SomeDate])=1, DatePart("yyyy",[SomeDate],1,3)-1,DatePart("yyyy",[SomeDate],1,3)) & "-" & IIf(DatePart("ww",[SomeDate],1,3)<10,"0" & DatePart("ww",[SomeDate],1,3),DatePart("ww",[SomeDate],1,3))