Log in  ::  Register  ::  Not logged in

 Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Fiscal year week Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, September 26, 2012 12:03 AM
 Valued Member Group: General Forum Members Last Login: Thursday, May 15, 2014 2:05 PM Points: 73, Visits: 475
 Hi all, I'm here with another question may be it is simple.but i searched in web i didn't get the correct answer...I want to populate fiscal year week in date dimension table.(from 1 April to 31 march)datepart(ww,date) will give week from 1 Jan.but i want for fiscal year.thanks and regardssathiyan
Post #1364446
 Posted Wednesday, September 26, 2012 3:34 AM
 SSC-Addicted Group: General Forum Members Last Login: Thursday, September 29, 2016 5:30 AM Points: 424, Visits: 1,271
 Here's a possible solution:`-- Start & End of Fiscal Year:declare @StartDate Date, @DateInt int, @DateEndInt Int;set @StartDate = '01 Apr 2012'; -- Start Date of Fiscal Yearset @DateInt = datediff(dd, 0, @StartDate); -- Start Date as intset @DateEndInt = datediff(dd, 0, dateadd(yy, 1, @StartDate)); -- End Date as int (+1 day)-- Number of days from 1st Jan:declare @FirstDayOfYear Date, @DateOffset int;set @FirstDayOfYear = DATEADD(year,DATEDIFF(year,0,@StartDate),0); -- Date of First Day of Calendar Yearset @DateOffset = datediff(dd, @FirstDayOfYear, @StartDate) -- Difference between start of Calendar & Fiscal Years--print @DateInt--print @DateEndInt--print @DateShift-- Create date table with Date & Fiscal Week:declare @a as table ( [Date] Date, FiscalWeek Int );while @DateInt < @DateEndIntbegin -- Insert next Date, & week number subtracting offset to get correct fiscal week insert into @a values ( dateadd(dd, 0, @DateInt), datepart(ww, dateadd(dd, (-@DateOffset), @DateInt)) ); set @DateInt = @DateInt +1;endselect * from @a;`
Post #1364525
 Posted Wednesday, September 26, 2012 5:42 AM
 Valued Member Group: General Forum Members Last Login: Thursday, May 15, 2014 2:05 PM Points: 73, Visits: 475
 but for 2011 april first week starts from friday,saturday...using your query it gives like date week2011-04-01 12011-04-02 22011-04-03 22011-04-04 2this looks like it starts from saturday....any suggestions please....
Post #1364583
 Posted Wednesday, September 26, 2012 6:26 AM
 SSC-Addicted Group: General Forum Members Last Login: Thursday, September 29, 2016 5:30 AM Points: 424, Visits: 1,271
 Try this:`-- Start & End of Fiscal Year:declare @StartDate Date, @DateInt int, @DateEndInt Int;set @StartDate = '01 Apr 2012'; -- Start Date of Fiscal Yearset @DateInt = datediff(dd, 0, @StartDate); -- Start Date as intset @DateEndInt = datediff(dd, 0, dateadd(yy, 1, @StartDate)); -- End Date as int (+1 day)-- Create date table with Date & Fiscal Week:declare @a as table ( [Date] Date, DayName Varchar(10), FiscalWeek Int );declare @Start int;set @Start = @DateInt -- Populate table:while @DateInt < @DateEndIntbegin insert into @a values ( dateadd(dd, 0, @DateInt), DATENAME(weekday,dateadd(dd, 0, @DateInt)), ((@dateInt-@Start)/7)+1 ); set @DateInt = @DateInt +1;endselect * from @a;`
Post #1364616
 Posted Wednesday, September 26, 2012 12:35 PM
 Valued Member Group: General Forum Members Last Login: Thursday, May 15, 2014 2:05 PM Points: 73, Visits: 475
 thanks laurie, but this one also gives like:date day week1-Apr-2011 Friday 12-Apr-2011 Saturday 1 3-Apr-2011 Sunday 14-Apr-2011 Monday 15-Apr-2011 Tuesday 16-Apr-2011 Wednesday 17-Apr-2011 Thursday 18-Apr-2011 Friday 2But i want like...date day week1-Apr-2011 Friday 12-Apr-2011 Saturday 1 3-Apr-2011 Sunday 24-Apr-2011 Monday 25-Apr-2011 Tuesday 2
Post #1364885
 Posted Wednesday, September 26, 2012 7:29 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 How about something like this?`DECLARE @StartDT DATETIME = '2011-04-01';WITH Dates AS ( SELECT TOP (1+DATEDIFF(day, @StartDT, DATEADD(day, -1, DATEADD(year, 1, @StartDT)))) d=DATEADD(day, ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1, @StartDT) FROM sys.all_columns)SELECT d, Day=DATENAME(dw, d) ,Week=CASE WHEN DATEPART(ww, d) <= 13 THEN 40+DATEPART(ww, d) ELSE DATEPART(ww, d) - 13 ENDFROM Dates` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1364997

 Permissions