Data warehouse: Populating fact table with multiple date keys

In any practical data warehouse, there will be always few fact tables which will have more than one date key (order date, shipped date for example).

 There are different ways by which people populate such facts. I found the following method to be very useful.
I used Kimball’s spread sheet to populate the date dimension. So, surrogate keys are kind of ‘smart’ (I know , I know you don’t want surrogate keys to be smart …)
For example key for ’12/5/2009′ will be ‘20091205’.
Assuming this is the scheme you are using for date dimension surrogate keys, you can use the following script to load the fact table:

 

ORDER_DATE_KEY = RTRIM(LTRIM(STR(DATEPART(yyyy,@myDate)) +
  RTRIM(CASE  WHEN Month(@myDate) <>
   THEN ‘0’ + CONVERT(Char,DATEPART(Month,@myDate))
    ELSE CONVERT(Char,DATEPART(Month,@myDate)) END) +
  RTRIM(CASE  WHEN Day(@myDate) <>
   THEN ‘0’ + CONVERT(Char,DATEPART(dd,@myDate))
    ELSE CONVERT(Char,DATEPART(dd,@myDate)) END) )),
 
SHIP_DATE_KEY = RTRIM(LTRIM(STR(DATEPART(yyyy,@myDate2)) +
  RTRIM(CASE  WHEN Month(@myDate2) <>
   THEN ‘0’ + CONVERT(Char,DATEPART(Month,@myDate2))
    ELSE CONVERT(Char,DATEPART(Month,@myDate2)) END) +
  RTRIM(CASE  WHEN Day(@myDate2) <>
   THEN ‘0’ + CONVERT(Char,DATEPART(dd,@myDate2))
    ELSE CONVERT(Char,DATEPART(dd,@myDate2)) END) ))

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: