in

Prologika Forums

Business Intelligence to the Masses
Latest post 03-16-2007 6:50 PM by wgpubs. 9 replies.
Page 1 of 1 (10 items)
Sort Posts: Previous Next
  • 03-07-2007 1:18 PM

    How to populate a Date Dimension?

    Ok ... I'm putting together a date dimension following the suggested schema proposed by Kimball in his DW Toolkit book on pp.38-41.  But how to populate it?

    Is there a SQL script out there already in the form of a stored proc or UDF which can do this given a begin and end date as parameters???

    Just curious to know ... don't want to re-invent the wheel here.

    Thanks - Wayde 

    Filed under:
  • 03-07-2007 1:21 PM In reply to

    • dz0001
    • Top 10 Contributor
    • Joined on 02-07-2006
    • Dallas
    • Posts 114

    Re: How to populate a Date Dimension?

    There are tons of script avaliable out there, you can go to Google and search for populate datetime dimension, here is one example:

     set nocount on
    go
    drop   function dbo.date$calcIdValue
    go
    create function dbo.date$calcIdValue
    (
     @dateValue datetime
    )
    returns int
    as
     begin
     return datediff(day,'1/1/1800',@dateValue)
     end
    go


    drop table generic_date_dim
    go
    CREATE TABLE generic_date_dim (
           generic_date_dim_id  int not null primary key,
           date_value           smalldatetime NOT NULL unique,
           the_day              varchar(60) NOT NULL,
           the_month            varchar(60) NOT NULL,
           the_year             varchar(60) NOT NULL,
           day_of_the_month     int NOT NULL,
           day_of_the_year      int NOT NULL,
           week_of_the_year     int NOT NULL,
           month_of_the_year    int NOT NULL,
           calendar_quarter     int NOT NULL,
           fiscal_year          int NOT NULL,
           fiscal_quarter       int NOT NULL
    )
    go


    declare @startDay datetime, @endDay datetime


    select  @startDay = '1/1/2003',
     @endDay =   '4/30/2004'


    declare @i int, @currentDay smalldatetime
    set @currentDay = @startDay


    begin transaction


    while @currentDay <= @endDay
     begin


     insert into generic_date_dim([generic_date_dim_id], [date_value],
    [the_day], [the_month], [the_year], [day_of_the_month], [day_of_the_year],
    [week_of_the_year], [month_of_the_year], [calendar_quarter], [fiscal_year],
    [fiscal_quarter])
     select  dbo.date$calcIdValue(@currentDay),
      @currentDay as dateValue,
      datename(dw, @currentDay) as theDay,
      datename(month, @currentDay) as theMonth,
      datename(year, @currentDay) as theYear,
      datepart(day,@currentDay) as dayOfTheMonth,
      datepart(dayofyear,@currentDay) as dayOfTheYear,
      datepart(week,@currentDay) as weekOfTheYear,
      datepart(month,@currentDay) as monthOfTheYear,
      cast((datepart(month,@currentDay) / 4) + 1 as varchar(3)) as
    calendarQuarter,
      datepart(year, @currentDay) as fiscalYear,
      cast((datepart(month,@currentDay) / 4) + 1 as varchar(3)) as fiscalQuarter


     if @@error <> 0
      begin
      raiserror 50000 'Error creating timeByDay dimension'
      rollback transaction
      goto endofscript
      end


     set @currentDay = dateadd(day,1,@currentDay)
     end


    commit transaction

  • 03-07-2007 2:42 PM In reply to

    Re: How to populate a Date Dimension?

    As a side note, there are pros and cons for pre-populating the Date dimension. On the positive side, you can easily calculate the number of children in period and you don't need to worry about this in ETL. On the negative side, depending on the browser tool selected, loading a selector with past and future periods can be bad from usability standpoint. For example, if you use the Report Builder and InstanceSelection is set to List or DropdownList, the user will see all periods and they will sure complain about this. For reason like these, I usually populate the Date dimension in ETL on demand (only when there isn't a period already).
  • 03-07-2007 4:01 PM In reply to

    Re: How to populate a Date Dimension?

    If you populate the Date dimension on demand ...

    1.  Do you only put records in the date dimension for which there is an associated record in the fact table(s) it touches?

    2.  How do you handle columns like 'Holiday Indicator', etc... which seem difficult to simply script out? 

    Filed under:
  • 03-07-2007 6:23 PM In reply to

    Re: How to populate a Date Dimension?

    1. Yes, just like as you would do with any other dimension.

    2. How would you deal withi such columns if you pre-generate the dimension? I suppose a second pass will be needed to mark them.

  • 03-08-2007 9:43 AM In reply to

    • dz0001
    • Top 10 Contributor
    • Joined on 02-07-2006
    • Dallas
    • Posts 114

    Re: How to populate a Date Dimension?

    Regarding the date time dimensions:

    1. if you only put records in date dimension with associated record in the fact, then we do not have complete calendar, right?

    2. Teo, the AS 2005 provide server time dimension, do you feel it is good practice to use this more than setting up your own date dimension, I try parse my date field in fact to year, quarter, month, day, then map to the server time.

    Thanks

  • 03-08-2007 12:52 PM In reply to

    Re: How to populate a Date Dimension?

    That is a good question ... does not having a full calendar carry any implications?

    Also, as a side note ... what is generally considered a good "Unknown" value for a Date Dimension?  1/1/1900?

    thanks - wg 

  • 03-08-2007 3:14 PM In reply to

    Re: How to populate a Date Dimension?

    1. Probably yes depending on if you have data for that period or not. This could be an issue if you need all periods, e.g. find the number of days in a month or doing data allocation. But, the ETL process can fill in the period gaps for past periods. If I have to compare this approach with sticking in days for 10 years ahead and asking the user to navigate through all of them to find the right period, I'll go with on-demand load in a heartbeat.

    2. No. Always use your own Date dimension if you can. Server-based Date dimension is just that. You have no control over it.

  • 03-08-2007 3:19 PM In reply to

    Re: How to populate a Date Dimension?

    I'd say you should never have unknown date but that's just me.

  • 03-16-2007 6:50 PM In reply to

    Re: How to populate a Date Dimension?

    For anyone interested, I posted a modified UDF I found on the web that you can use to populate your Date dimension either on-demand or in advance.

    Click here to download 

    If you have any questions and/or feedback it would be appreciated.

    thanks - wayde 

Page 1 of 1 (10 items)
Copyright © 2005 Prologika, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems