Fiscal year week

  • 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 regards

    sathiyan

  • 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 Year

    set @DateInt = datediff(dd, 0, @StartDate); -- Start Date as int

    set @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 Year

    set @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 < @DateEndInt

    begin

    -- 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;

    end

    select * from @a;

  • but for 2011 april first week starts from friday,saturday...

    using your query it gives like

    date week

    2011-04-01 1

    2011-04-02 2

    2011-04-03 2

    2011-04-04 2

    this looks like it starts from saturday....

    any suggestions please....

  • Try this:

    -- Start & End of Fiscal Year:

    declare @StartDate Date, @DateInt int, @DateEndInt Int;

    set @StartDate = '01 Apr 2012'; -- Start Date of Fiscal Year

    set @DateInt = datediff(dd, 0, @StartDate); -- Start Date as int

    set @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 < @DateEndInt

    begin

    insert into @a values ( dateadd(dd, 0, @DateInt), DATENAME(weekday,dateadd(dd, 0, @DateInt)), ((@dateInt-@Start)/7)+1 );

    set @DateInt = @DateInt +1;

    end

    select * from @a;

  • thanks laurie,

    but this one also gives like:

    date day week

    1-Apr-2011 Friday 1

    2-Apr-2011 Saturday 1

    3-Apr-2011 Sunday 1

    4-Apr-2011 Monday 1

    5-Apr-2011 Tuesday 1

    6-Apr-2011 Wednesday 1

    7-Apr-2011 Thursday 1

    8-Apr-2011 Friday 2

    But i want like...

    date day week

    1-Apr-2011 Friday 1

    2-Apr-2011 Saturday 1

    3-Apr-2011 Sunday 2

    4-Apr-2011 Monday 2

    5-Apr-2011 Tuesday 2

  • 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 END

    FROM Dates


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    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?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply