Tuesday, April 20, 2010

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 many ways by which people populate such facts. Here is what did today in one of my projects:

I am using 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) ))


















No comments:

Followers