Friday, 17 January 2014

Create and Populate Date Dimension for Data Warehouse

Recently I have been working on new data warehouse project using SSAS and SSIS. And I had to populate the Date dimension table, with granularity at individual day with various other values like Day of Month, Day of Week, Day Of Quarter, Day Name, Day of Week, Day of Week in month, First Day of Month, First Day of Quarter etc. 

Quickly I did search internet but could not find the script which reasonably satisfy my requirement. So I have decided write my script as listed below. 

This script will populate Date dimension for required as per UK dates like Holidays, Monday week start day etc. If you need to populate it for USA or other country then you have to change few things like Holiday Listing, FullDate, Week Start Day etc.

I found one issue while running this script while running on different server. This was related to First Day of Week. You can check first day of week using @@DATEFIRST. If it returns 1 then this script will work fine, but if it is other then 1 then you have to change the script calculation. To get away with this problem I have statement at beginning of this script to set first day of week to 1 using SET DATEFIRST 1.

This scripts populated below fields for Date dimension table.
DateKey -- Integer date key is created as YYYYMMDD
[Date] -- Actual date value

Day Of Month -- Day number of month
Day Of Month With Suffix -- Apply suffix as 1st, 2nd ,3rd etc

Day Of Quarter -- Day number of quarter
Day Of Quarter With Suffix -- Apply suffix as 1st, 2nd ,3rd etc
Month Of Quarter -- Month number of quarter

Day Name -- Name of the day, Sunday, Monday 
Day Of Week In Month --1st Monday, 2nd Monday etc in Month
Week Of Month -- Week Number of Month 
Week Of Quarter -- Week Number of the Quarter
Month Name -- January, February etc
Month Year -- Jan-2014, Feb-2014 etc
MMYYYY 
First Day Of Month -- Date for first day of month
Last Day Of Month  -- Date for last day of month
First Day Of Quarter   -- Date for first day of quarter
Last Day Of Quarter  -- Date for last day of quarter

Full Date -- Date in dd-MM-yyyy format
Day Of Week -- First Day Monday=1 and Sunday=7

Day Of Calender Year -- Day number in calender year
Week Of Calender Year -- Week Number of the calender Year
Calender Month  -- Number of the Month 1 to 12
Calender Quarter -- Calender quarter number Jan-Mar = 1, Apr-Jun=2, Jul-Sep = 3 and Oct-Dec = 4
Calender Year -- Year value of Date stored as YYYY

Day Of Financial Year -- Day number in financial year
Week Of Financial Year -- Week Number of the financial Year
Financial Month -- Month number in financial year being Apr=1 and Mar=12
Financial Quarter -- Calender quarter number Apr-Jun = 1, Jul-Sep = 2, Oct-Dec = 3 and Jan-Mar = 4
FinancialYear -- Year value of Date stored as YYYY/YYYY

IsWeekday -- 0=Week End ,1=Week Day
IsHoliday -- Flag 1=National Holiday, 0=No National Holiday
HolidayName -- Name of Holiday in UK

Please find the compete script as below. Please let me know if you need any help or if you feel I have done something wrong in this script.


 

SET DATEFIRST 1

GO

 

DECLARE @Holiday TABLE

       (

       [Date]                     DATE,

       HolidayName                VARCHAR(50)

       )

 

--Data Source https://www.gov.uk/bank-holidays (as on 2014-01-07), Only England and Wales holidays are considered

--

INSERT INTO @Holiday([Date], HolidayName)

VALUES('2012-01-02', 'New Year''s Day(substitute day)'),

       ('2012-04-06', 'Good Friday'),

       ('2012-04-09', 'Easter Monday'),

       ('2012-05-07', 'Early May bank holiday'),

       ('2012-06-04', 'Spring bank holiday (substitute day)'),

       ('2012-06-05', 'Queen''s Diamond Jubilee (extra bank holiday)'),

       ('2012-08-27', 'Summer bank holiday'),

       ('2012-12-25', 'Christmas Day'),

       ('2012-12-26', 'Boxing Day'),

 

       ('2013-01-01', 'New Year''s Day'),

       ('2013-03-29', 'Good Friday'),

       ('2013-04-01', 'Easter Monday'),

       ('2013-05-06', 'Early May bank holiday'),

       ('2013-05-27', 'Spring bank holiday'),

       ('2013-08-26', 'Summer bank holiday'),

       ('2013-12-25', 'Christmas Day'),

       ('2013-12-26', 'Boxing Day'),

 

       ('2014-01-01', 'New Year''s Day'),

       ('2014-04-18', 'Good Friday'),

       ('2014-04-21', 'Easter Monday'),

       ('2014-05-05', 'Early May bank holiday'),

       ('2014-05-26', 'Spring bank holiday'),

       ('2014-08-25', 'Summer bank holiday'),

       ('2014-12-25', 'Christmas Day'),

       ('2014-12-26', 'Boxing Day'),

 

       ('2015-01-01', 'New Year''s Day'),

       ('2015-04-03', 'Good Friday'),

       ('2015-04-06', 'Easter Monday'),

       ('2015-05-04', 'Early May bank holiday'),

       ('2015-05-25', 'Spring bank holiday'),

       ('2015-08-31', 'Summer bank holiday'),

       ('2015-12-25', 'Christmas Day'),

       ('2015-12-28', 'Boxing Day (substitute day)')

 

DECLARE @DimDate TABLE

       (     

              DateKey                           INT PRIMARY KEY,  -- Integer date key is created as YYYYMMDD

              [Date]                     DATE, -- Actual date value

 

              [DayOfMonth]               VARCHAR(2), -- Day number of month

              DayOfMonthWithSuffix VARCHAR(4), -- Apply suffix as 1st, 2nd ,3rd etc

             

              DayOfQuarter               VARCHAR(2), -- Day number of quarter

              DayOfQuarterWithSuffix     VARCHAR(4), -- Apply suffix as 1st, 2nd ,3rd etc

              MonthOfQuarter                    VARCHAR(1), -- Month number of quarter

 

              [DayName]                  VARCHAR(9), -- Name of the day, Sunday, Monday

              DayOfWeekInMonth     VARCHAR(15), --1st Monday, 2nd Monday etc in Month

              WeekOfMonth                VARCHAR(1), -- Week Number of Month

              WeekOfQuarter        VARCHAR(2), -- Week Number of the Quarter

              [MonthName]                VARCHAR(9), -- January, February etc

              MonthYear                  CHAR(10), -- Jan-2014, Feb-2014 etc

              MMYYYY                     CHAR(6),            

              FirstDayOfMonth            DATE, -- Date for first day of month

              LastDayOfMonth             DATE, -- Date for last day of month

              FirstDayOfQuarter    DATE, -- Date for first day of quarter

              LastDayOfQuarter     DATE, -- Date for last day of quarter

             

              FullDate                   CHAR(10), -- Date in dd-MM-yyyy format

              [DayOfWeek]                CHAR(1), -- First Day Monday=1 and Sunday=7

 

              --Calender

              DayOfCalenderYear    VARCHAR(3), -- Day number in calender year

              WeekOfCalenderYear   VARCHAR(2), -- Week Number of the calender Year

              CalenderMonth        VARCHAR(2), -- Number of the Month 1 to 12

              CalenderQuarter            CHAR(1), -- Calender quarter number Jan-Mar = 1, Apr-Jun=2, Jul-Sep = 3 and Oct-Dec = 4

              CalenderYear         CHAR(4), -- Year value of Date stored as YYYY

 

              --Financial

              DayOfFinancialYear   VARCHAR(3), -- Day number in financial year

              WeekOfFinancialYear  VARCHAR(2), -- Week Number of the financial Year

              FinancialMonth             VARCHAR(2), -- Month number in financial year being Apr=1 and Mar=12

              FinancialQuarter     CHAR(1), -- Calender quarter number Apr-Jun = 1, Jul-Sep = 2, Oct-Dec = 3 and Jan-Mar = 4

              FinancialYear        CHAR(9), -- Year value of Date stored as YYYY/YYYY

                          

              [IsWeekday]                BIT, -- 0=Week End ,1=Week Day

              IsHoliday                  BIT, -- Flag 1=National Holiday, 0=No National Holiday

              HolidayName                VARCHAR(50) -- Name of Holiday in UK

       )

 

 

DECLARE @year        INT,

       @startDate           DATE,

       @endDate             DATE,

       @currentDate  DATE,

 

       @DateKey                   INT,

       @Date                      DATETIME,

 

       @DayOfMonth                       VARCHAR(2),

       @DayOfMonthWithSuffix      VARCHAR(4),

      

       @DayOfQuarter              VARCHAR(2),

       @DayOfQuarterWithSuffix    VARCHAR(4),

       @MonthOfQuarter                   VARCHAR(1),

 

       @DayName                   VARCHAR(9),

       @DayOfWeekInMonth    VARCHAR(15),

       @WeekOfMonth         VARCHAR(1),

       @WeekOfQuarter             VARCHAR(2),

       @MonthName                 VARCHAR(9),

       @MonthYear                 CHAR(10),

       @MMYYYY                           CHAR(6),            

       @FirstDayOfMonth     DATE,

       @LastDayOfMonth            DATE,

       @FirstDayOfQuarter   DATE,

       @LastDayOfQuarter    DATE,

 

       @FullDate                  CHAR(10),

       @DayOfWeek                 CHAR(1),

             

       @DayOfCalenderYear         VARCHAR(3),

       @WeekOfCalenderYear        VARCHAR(2),

       @CalenderMonth                    VARCHAR(2),

       @CalenderQuarter           CHAR(1),

       @CalenderYear              CHAR(4),

             

       @DayOfFinancialYear        VARCHAR(3),

       @WeekOfFinancialYear VARCHAR(2),

       @FinancialMonth                   VARCHAR(2),

       @FinancialQuarter          CHAR(1),

       @FinancialYear                    CHAR(9),

      

       @IsWeekday           BIT,

       @IsHoliday           BIT,

       @HolidayName  VARCHAR(50)

 

SET @startDate             = '2014-01-01'

SET @endDate         = '2014-12-31'

 

SET @currentDate     = @startDate

WHILE @currentDate<=@endDate

BEGIN

      

       SET @DateKey = CAST(CONVERT(VARCHAR, @currentDate, 112) AS INT)

       SET @Date = @currentDate

 

       SET @DayOfMonth = CASE WHEN DAY(@currentDate) <= 9 THEN '0' + CAST(DAY(@currentDate) AS VARCHAR) ELSE CAST(DAY(@currentDate) AS VARCHAR) END

       SET @DayOfMonthWithSuffix = CASE WHEN DAY(@currentDate) IN (1, 21, 31) THEN CAST(DAY(@currentDate) AS VARCHAR) + 'st'

                                                              WHEN DAY(@currentDate) IN (2, 22) THEN CAST(DAY(@currentDate) AS VARCHAR) + 'nd'

                                                              WHEN DAY(@currentDate) IN (3, 23) THEN CAST(DAY(@currentDate) AS VARCHAR) + 'rd'

                                                              ELSE CAST(DAY(@currentDate) AS VARCHAR) + 'th'

                                                       END

 

       SET @DayOfQuarter = CASE WHEN MONTH(@currentDate) IN (1, 2, 3) THEN DATEDIFF(D, CAST(YEAR(@currentDate) AS VARCHAR) + '-01-01', @currentDate) + 1

                                                WHEN MONTH(@currentDate) IN (4, 5, 6) THEN DATEDIFF(D, CAST(YEAR(@currentDate) AS VARCHAR) + '-04-01', @currentDate) + 1

                                                WHEN MONTH(@currentDate) IN (7, 8, 9) THEN DATEDIFF(D, CAST(YEAR(@currentDate) AS VARCHAR) + '-07-01', @currentDate) + 1

                                                WHEN MONTH(@currentDate) IN (10, 11, 12) THEN DATEDIFF(D, CAST(YEAR(@currentDate) AS VARCHAR) + '-10-01', @currentDate) + 1

                                         END

      

       SET @DayOfQuarterWithSuffix = CASE WHEN @DayOfQuarter IN (1, 21, 31, 41, 51, 61, 71, 81, 91) THEN CAST(@DayOfQuarter AS VARCHAR) + 'st'

                                                              WHEN @DayOfQuarter IN (2, 22, 32, 42, 52, 62, 72, 82, 92) THEN CAST(@DayOfQuarter AS VARCHAR) + 'nd'

                                                              WHEN @DayOfQuarter IN (3, 23, 33, 43, 53, 63, 73, 83, 93) THEN CAST(@DayOfQuarter AS VARCHAR) + 'rd'

                                                              ELSE CAST(@DayOfQuarter AS VARCHAR) + 'th'

                                                       END

      

       SET @DayOfQuarter = CASE WHEN CAST(@DayOfQuarter AS INT) <= 9 THEN '0' + CAST(CAST(@DayOfQuarter AS INT) AS VARCHAR) ELSE @DayOfQuarter END

 

       SET @MonthOfQuarter  = CASE WHEN MONTH(@currentDate)%3 = 0 THEN 3 ELSE MONTH(@currentDate)%3 END

      

       SET @DayName = DATENAME(DW, @CurrentDate)

 

       DECLARE @FirstDayNameOfTheMonth   VARCHAR(9),

              @DayInWeekKey                     INT

             

       SET @FirstDayOfMonth = CAST(YEAR(@currentDate) AS VARCHAR) + '-' + CAST(MONTH(@currentDate) AS VARCHAR) + '-01'

       SET @FirstDayNameOfTheMonth = DATENAME(DW, @FirstDayNameOfTheMonth)

       SET @DayInWeekKey = DATEPART(dw, @FirstDayOfMonth)

      

       SET @WeekOfMonth = CASE WHEN DATENAME(DW, @FirstDayOfMonth) = 'Monday' THEN (DAY(@CurrentDate)-1)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfMonth) = 'Tuesday' THEN (DAY(@CurrentDate))/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfMonth) = 'Wednesday' THEN (DAY(@CurrentDate)+1)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfMonth) = 'Thursday' THEN (DAY(@CurrentDate)+2)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfMonth) = 'Friday' THEN (DAY(@CurrentDate)+3)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfMonth) = 'Saturday' THEN (DAY(@CurrentDate)+4)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfMonth) = 'Sunday' THEN (DAY(@CurrentDate)+5)/7 + 1

                                         END

      

 

       SET @MonthName = DATENAME(MM, @CurrentDate)

       SET @MonthYear = SUBSTRING(DATENAME(MM, @CurrentDate), 1, 3) + '-' + CAST(YEAR(@currentDate) AS VARCHAR)

       SET @MMYYYY = SUBSTRING(CONVERT(VARCHAR, @currentDate, 112), 1, 6)

 

       SET @LastDayOfMonth = DATEADD(D,-1, DATEADD(M, 1, @FirstDayOfMonth))

       SET @FirstDayOfQuarter = CASE WHEN MONTH(@currentDate) IN (1, 2, 3) THEN CAST(YEAR(@currentDate) AS VARCHAR) + '-' + '01-01'

                                                                     WHEN MONTH(@currentDate) IN (4, 5, 6) THEN CAST(YEAR(@currentDate) AS VARCHAR) + '-' + '04-01'

                                                                     WHEN MONTH(@currentDate) IN (7, 8, 9) THEN CAST(YEAR(@currentDate) AS VARCHAR) + '-' + '07-01'

                                                                     WHEN MONTH(@currentDate) IN (10, 11, 12) THEN CAST(YEAR(@currentDate) AS VARCHAR) + '-' + '10-01'

                                                              END

       SET @LastDayOfQuarter = DATEADD(D, -1, DATEADD(MM, 3, @FirstDayOfQuarter))

             

       SET @WeekOfQuarter = CASE WHEN DATENAME(DW, @FirstDayOfQuarter) = 'Monday' THEN (DATEDIFF(D, @FirstDayOfQuarter, @CurrentDate))/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfQuarter) = 'Tuesday' THEN (DATEDIFF(D, @FirstDayOfQuarter, @CurrentDate)+1)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfQuarter) = 'Wednesday' THEN (DATEDIFF(D, @FirstDayOfQuarter, @CurrentDate)+2)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfQuarter) = 'Thursday' THEN (DATEDIFF(D, @FirstDayOfQuarter, @CurrentDate)+3)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfQuarter) = 'Friday' THEN (DATEDIFF(D, @FirstDayOfQuarter, @CurrentDate)+4)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfQuarter) = 'Saturday' THEN (DATEDIFF(D, @FirstDayOfQuarter, @CurrentDate)+5)/7 + 1

                                                WHEN DATENAME(DW, @FirstDayOfQuarter) = 'Sunday' THEN (DATEDIFF(D, @FirstDayOfQuarter, @CurrentDate)+6)/7 + 1

                                         END

       SET @WeekOfQuarter = CASE WHEN CAST(@WeekOfQuarter AS INT) <= 9 THEN '0' + CAST(CAST(@WeekOfQuarter AS INT) AS VARCHAR) ELSE @WeekOfQuarter END

 

       SET @FullDate = CONVERT(VARCHAR, @currentDate, 103)

       SET @DayOfWeek = DATEPART(DW, @CurrentDate)

      

       SET @DayOfWeekInMonth = CASE WHEN DATENAME(DW, @FirstDayOfMonth) = 'Monday'

                                                              THEN CASE

                                                                           WHEN @WeekOfMonth = 1 THEN  @WeekOfMonth + 'st ' + @dayName

                                                                           WHEN @WeekOfMonth = 2 THEN  @WeekOfMonth + 'nd ' + @dayName

                                                                           WHEN @WeekOfMonth = 3 THEN  @WeekOfMonth + 'rd ' + @dayName

                                                                           ELSE @WeekOfMonth + 'th ' + @dayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Tuesday' AND @DayOfWeek < 2

                                                              THEN CASE

                                                                           WHEN @WeekOfMonth-1 = 1 THEN  CAST(@WeekOfMonth-1 AS VARCHAR) + 'st ' + @dayName

                                                                           WHEN @WeekOfMonth-1 = 2 THEN  CAST(@WeekOfMonth-1 AS VARCHAR) + 'nd ' + @dayName

                                                                           WHEN @WeekOfMonth-1 = 3 THEN  CAST(@WeekOfMonth-1 AS VARCHAR) + 'rd ' + @dayName

                                                                           ELSE CAST(@WeekOfMonth-1 AS VARCHAR) + 'th ' + @dayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Tuesday' AND @DayOfWeek >= 2

                                                              THEN CASE

                                                                           WHEN @WeekOfMonth = 1 THEN  @WeekOfMonth + 'st ' + @dayName

                                                                           WHEN @WeekOfMonth = 2 THEN  @WeekOfMonth + 'nd ' + @dayName

                                                                           WHEN @WeekOfMonth = 3 THEN  @WeekOfMonth + 'rd ' + @dayName

                                                                           ELSE @WeekOfMonth + 'th ' + @dayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Wednesday' AND @DayOfWeek < 3

                                                              THEN CASE

                                                                           WHEN (CAST(@WeekOfMonth AS INT)-1) = 1 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'st ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 2 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'nd ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 3 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'rd ' + @DayName

                                                                           ELSE CAST(@WeekOfMonth-1 AS VARCHAR) + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Wednesday' AND @DayOfWeek >= 3

                                                              THEN CASE

                                                                           WHEN @WeekOfMonth = 1 THEN  ISNULL(@WeekOfMonth + 'st ' + @DayName, 'ZZZ')

                                                                           WHEN @WeekOfMonth = 2 THEN  @WeekOfMonth + 'nd ' + @DayName

                                                                           WHEN @WeekOfMonth = 3 THEN  @WeekOfMonth + 'rd ' + @DayName

                                                                           ELSE @WeekOfMonth + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Thursday' AND @DayOfWeek < 4

                                                              THEN CASE

                                                                           WHEN (CAST(@WeekOfMonth AS INT)-1) = 1 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'st ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 2 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'nd ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 3 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'rd ' + @DayName

                                                                           ELSE CAST(@WeekOfMonth-1 AS VARCHAR) + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Thursday' AND @DayOfWeek >= 4

                                                              THEN CASE

                                                                           WHEN @WeekOfMonth = 1 THEN  ISNULL(@WeekOfMonth + 'st ' + @DayName, 'ZZZ')

                                                                           WHEN @WeekOfMonth = 2 THEN  @WeekOfMonth + 'nd ' + @DayName

                                                                           WHEN @WeekOfMonth = 3 THEN  @WeekOfMonth + 'rd ' + @DayName

                                                                           ELSE @WeekOfMonth + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Friday' AND @DayOfWeek < 5

                                                              THEN CASE

                                                                           WHEN (CAST(@WeekOfMonth AS INT)-1) = 1 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'st ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 2 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'nd ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 3 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'rd ' + @DayName

                                                                           ELSE CAST(@WeekOfMonth-1 AS VARCHAR) + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Friday' AND @DayOfWeek >= 5

                                                              THEN CASE

                                                                           WHEN @WeekOfMonth = 1 THEN  ISNULL(@WeekOfMonth + 'st ' + @DayName, 'ZZZ')

                                                                           WHEN @WeekOfMonth = 2 THEN  @WeekOfMonth + 'nd ' + @DayName

                                                                           WHEN @WeekOfMonth = 3 THEN  @WeekOfMonth + 'rd ' + @DayName

                                                                           ELSE @WeekOfMonth + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Saturday' AND @DayOfWeek < 6

                                                              THEN CASE

                                                                           WHEN (CAST(@WeekOfMonth AS INT)-1) = 1 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'st ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 2 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'nd ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 3 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'rd ' + @DayName

                                                                           ELSE CAST(@WeekOfMonth-1 AS VARCHAR) + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Saturday' AND @DayOfWeek >= 6

                                                              THEN CASE

                                                                           WHEN @WeekOfMonth = 1 THEN  ISNULL(@WeekOfMonth + 'st ' + @DayName, 'ZZZ')

                                                                           WHEN @WeekOfMonth = 2 THEN  @WeekOfMonth + 'nd ' + @DayName

                                                                           WHEN @WeekOfMonth = 3 THEN  @WeekOfMonth + 'rd ' + @DayName

                                                                           ELSE @WeekOfMonth + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Sunday' AND @DayOfWeek < 7

                                                              THEN CASE

                                                                           WHEN (CAST(@WeekOfMonth AS INT)-1) = 1 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'st ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 2 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'nd ' + @DayName

                                                                           WHEN CAST(@WeekOfMonth AS INT)-1 = 3 THEN  CAST(CAST(@WeekOfMonth AS INT)-1 AS VARCHAR) + 'rd ' + @DayName

                                                                           ELSE CAST(@WeekOfMonth-1 AS VARCHAR) + 'th ' + @DayName END

                                                       WHEN DATENAME(DW, @FirstDayOfMonth) = 'Sunday' AND @DayOfWeek >= 7

                                                              THEN CASE

                                                                           WHEN @WeekOfMonth = 1 THEN  ISNULL(@WeekOfMonth + 'st ' + @DayName, 'ZZZ')

                                                                           WHEN @WeekOfMonth = 2 THEN  @WeekOfMonth + 'nd ' + @DayName

                                                                           WHEN @WeekOfMonth = 3 THEN  @WeekOfMonth + 'rd ' + @DayName

                                                                           ELSE @WeekOfMonth + 'th ' + @DayName END

                                                END

 

       SET @DayOfCalenderYear = DATEDIFF(D, CAST(YEAR(@currentDate) AS VARCHAR) + '-01-01', @currentDate) + 1

       SET @DayOfCalenderYear = CASE WHEN CAST(@DayOfCalenderYear AS  INT) <= 9 THEN '00' + CAST(CAST(@DayOfCalenderYear AS  INT) AS VARCHAR)

                                                       WHEN CAST(@DayOfCalenderYear AS  INT) <= 99 THEN '0' + CAST(CAST(@DayOfCalenderYear AS  INT) AS VARCHAR)

                                                       ELSE @DayOfCalenderYear END

 

       SET @WeekOfCalenderYear = DATEPART(wk, @CurrentDate)

       SET @WeekOfCalenderYear = CASE WHEN CAST(@WeekOfCalenderYear AS INT) <= 9 THEN '0' + CAST(CAST(@WeekOfCalenderYear AS INT) AS VARCHAR) ELSE @WeekOfCalenderYear END

 

       SET @CalenderMonth = MONTH(@currentDate)

       SET @CalenderMonth = CASE WHEN CAST(@CalenderMonth AS INT) <= 9 THEN '0' + CAST(CAST(@CalenderMonth AS INT) AS VARCHAR) ELSE @CalenderMonth END

 

       SET @CalenderQuarter = CASE WHEN MONTH(@currentDate) IN (1, 2, 3) THEN 1

                                                       WHEN MONTH(@currentDate) IN (4, 5, 6) THEN 2

                                                       WHEN MONTH(@currentDate) IN (7, 8, 9) THEN 3

                                                       WHEN MONTH(@currentDate) IN (10, 11, 12) THEN 4

                                                END

       SET @CalenderYear = YEAR(@currentDate)  

      

 

       SET @DayOfFinancialYear = CASE WHEN MONTH(@currentDate) IN (1, 2, 3) THEN  DATEDIFF(D, CAST((YEAR(@currentDate)-1) AS VARCHAR) + '-04-01', @currentDate) + 1

                                                              ELSE DATEDIFF(D, CAST(YEAR(@currentDate) AS VARCHAR) + '-04-01', @currentDate) + 1

                                                       END

       SET @DayOfFinancialYear = CASE WHEN CAST(@DayOfFinancialYear AS  INT) <= 9 THEN '00' + CAST(CAST(@DayOfFinancialYear AS  INT) AS VARCHAR)

                                                       WHEN CAST(@DayOfFinancialYear AS  INT) <= 99 THEN '0' + CAST(CAST(@DayOfFinancialYear AS  INT) AS VARCHAR)

                                                       ELSE @DayOfFinancialYear END

 

       SET @WeekOfFinancialYear = CASE WHEN MONTH(@CurrentDate) <= 3 THEN (DATEPART(wk, CAST((CAST(YEAR(@CurrentDate)-1 AS CHAR) + '-12-31') AS DATE)) - DATEPART(wk, CAST((CAST(YEAR(@CurrentDate)-1 AS CHAR) + '-04-01') AS DATE)) + DATEPART(wk, @CurrentDate))

                                                       ELSE (DATEPART(wk, @CurrentDate) - DATEPART(wk, CAST((CAST(YEAR(@CurrentDate) AS CHAR) + '-04-01') AS DATE)) + 1) END

       SET @WeekOfFinancialYear = CASE WHEN CAST(@WeekOfFinancialYear AS INT) <= 9 THEN '0' + CAST(CAST(@WeekOfFinancialYear AS INT) AS VARCHAR) ELSE @WeekOfFinancialYear END

 

       SET @FinancialMonth = CASE WHEN MONTH(@currentDate) IN (1, 2, 3) THEN 9+MONTH(@currentDate) ELSE MONTH(@currentDate)-3 END

       SET @FinancialMonth = CASE WHEN CAST(@FinancialMonth AS INT) <= 9 THEN '0' + CAST(CAST(@FinancialMonth AS INT) AS VARCHAR) ELSE @FinancialMonth END

 

       SET @FinancialQuarter = CASE WHEN MONTH(@currentDate) IN (1, 2, 3) THEN 4

                                                       WHEN MONTH(@currentDate) IN (4, 5, 6) THEN 1

                                                       WHEN MONTH(@currentDate) IN (7, 8, 9) THEN 2

                                                       WHEN MONTH(@currentDate) IN (10, 11, 12) THEN 3

                                                END

       SET @FinancialYear = CASE WHEN MONTH(@currentDate) IN (1, 2, 3) THEN  CAST((YEAR(@currentDate)-1) AS VARCHAR) + '/' + CAST((YEAR(@currentDate)) AS VARCHAR) ELSE CAST((YEAR(@currentDate)) AS VARCHAR) + '/' + CAST((YEAR(@currentDate)+1) AS VARCHAR) END

       SET @IsWeekday = CASE WHEN @DayOfWeek IN (6, 7) THEN 0 ELSE 1 END

      

       SET @IsHoliday = CASE WHEN EXISTS(SELECT * FROM @Holiday WHERE [Date] = @Date) THEN 1 ELSE 0 END

       SET @HolidayName = CASE WHEN @IsHoliday = 1 THEN (SELECT HolidayName FROM @Holiday WHERE [Date] = @Date) ELSE '' END

 

       INSERT INTO @DimDate

              (

              DateKey,

              [Date],

 

              [DayOfMonth],

              DayOfMonthWithSuffix,

              DayOfQuarter,

              DayOfQuarterWithSuffix,

              MonthOfQuarter,

 

              [DayName],

              DayOfWeekInMonth,

              WeekOfMonth,

              WeekOfQuarter,

              [MonthName],

              MonthYear,

              MMYYYY,

              FirstDayOfMonth,

              LastDayOfMonth,

              FirstDayOfQuarter,

              LastDayOfQuarter,

 

              FullDate,

              [DayOfWeek],

 

              DayOfCalenderYear,

              WeekOfCalenderYear,

              CalenderMonth,

              CalenderQuarter,

              CalenderYear,

 

              DayOfFinancialYear,

              WeekOfFinancialYear,

              FinancialMonth,

              FinancialQuarter,

              FinancialYear,

             

              [IsWeekday],

              IsHoliday,

              HolidayName

              )

       VALUES

              (     

              @DateKey,

              @Date,

 

              @DayOfMonth,

              @DayOfMonthWithSuffix,

              @DayOfQuarter,

              @DayOfQuarterWithSuffix,

              @MonthOfQuarter,

 

              @DayName,

              @DayOfWeekInMonth,

              @WeekOfMonth,

              @WeekOfQuarter,

              @MonthName,

              @MonthYear,

              @MMYYYY,

              @FirstDayOfMonth,

              @LastDayOfMonth,

              @FirstDayOfQuarter,

              @LastDayOfQuarter,

             

              @FullDate,

              @DayOfWeek,

 

              @DayOfCalenderYear,

              @WeekOfCalenderYear,

              @CalenderMonth,

              @CalenderQuarter,

              @CalenderYear,

 

              @DayOfFinancialYear,

              @WeekOfFinancialYear,

              @FinancialMonth,

              @FinancialQuarter,

              @FinancialYear,

             

              @IsWeekday,

              @IsHoliday,

              @HolidayName

       )

 

       SET @currentDate = DATEADD(D, 1, @currentDate)

END

 

SELECT * FROM @DimDate

 

Monday, 30 December 2013

Measures, Attributes and Hierarchies

Measures

       A measure represents a data column which contains quantifiable data, usually numeric, that can be aggregated. A measure is generally mapped to a column in a fact table.


      In SSAS measures are grouped by underlying fast tables, which called Measure Groups. Measure Groups are used to associate dimensions with measures.


      Measure are selected while creating Cube, which can later be modified for various usage like its formatting, aggregate behaviour etc. Click here to see how to modify measure for formatting. 


Attributes 


       Attribute represent one or more column in dimension table. Attribute is used to particular expect of the dimension. For example product colour  represent colour attribute of the product dimension, which can later be used for slicing/dicing/selecting the cube values. Each dimension in cube contains one Key attribute, which is generally primary key in dimension table.


     Click here to see how to define/update attribute for dimension.


Hierarchies


       As name suggest Hierarchies is used to define hierarchy in dimension. For example Product and Product Category, National and International region etc. Hierarchy can be defined in Hierarchies table in cube design.


     Click here to see how to add/delete user-defined Hierarchy.

Monday, 16 December 2013

Tool to view big data file

I have to load big data files using SSIS or SQL server Import/Export wizard. Most of the time it works fine but if it fails and I have to view the data to debug then it is simply impossible to view the data because most of the text editor cannot open file which too big (size in multiple of GBs). So  I have developed small tool which can help to read or view a big file which cannot be opened in normal text editor.  This tool can also be use full if you are developing SSIS package to load big file and want to create small sample file from big file during development.

Click here to download the tool.

Features of the tools

 - Load big file (no limit on size)
 - View data page by page. Page size is configurable.
 - Save page in different file.
 - Go to any page in file.
 - Displays line number across  pages.

Limitations.
 - It uses Temp directory to store data pages while opening file. So virtually it can occupy similar disk space as the original file.
- It can only read file line by line. So if lines are not separated by {CR}{LF} then it cannot do data paging.
- It can only work with ANSI data.


Read big data file | View big data file | Open big data file | tool to open big file | tool to read big data file | Preview big data file | Get sample data from big data file | Read specific data from big file | View specific data from big file

Dimension and Cube



Dimension
        A database dimension is a collection of attributes usually related objects, which can be used to provide elaborated information about fact data in cube(s). Typical example of attributes in a Employee dimension might be Employee name, Employee Designation, Employee Qualification, Joining Date etc. Dimensions are linked to one or more columns in one or more tables in a data source view. By default, these attributes are visible as attribute hierarchies and can be used to understand the fact data in a cube. Attributes can be organized into user-defined hierarchies that provide navigational paths to assist users when browsing the data in a cube.

Click here for step by step instruction to create dimension in using existing table in SSDT.
Click here for step by step instruction to create dimension in using Dimension wizard in SSDT.

Cube 
         A cube is a multidimensional object that contains information for analytical purposes; which contains dimensions and measures. Dimensions define the structure of the cube that is used to slice and dice over, and measures provide aggregated numerical values of interest to the end user. As a logical structure, a cube allows a client application to retrieve values, of measures, as if they were contained in cells in the cube; cells are defined for every possible summarized value. A cell, in the cube, is defined by the intersection of dimension members and contains the aggregated values of the measures at that specific intersection.

Click here for step by step instruction to create Cube in using Cube wizard in SSDT.

Friday, 29 November 2013

Data Source and Data Source View

Data Source
 
       A data source in Analysis Services specifies a direct connection to an external data source. In addition to physical location, a data source object specifies the connection string, data provider, credentials, and other properties that control connection behaviour.

      Information provided by the data source object is used during the following operations:

  • Get schema information and other metadata used to generate data source views into a model.
  • Query or load data into a model during processing.
  • Run queries against multidimensional or data mining models that use ROLAP storage mode.
  • Read or write to remote partitions.
  • Connect to linked objects, as well as synchronize from target to source.
  • Perform write back operations that update fact table data stored in a relational database.
      When building a multidimensional model from the bottom up, you start by creating the data source object, and then use it to generate the next object, a data source view. A data source view is the data abstraction layer in the model. It is typically created after the data source object, using the schema of the source database as the basis. However, you can choose other ways to build a model, including starting with cubes and dimensions, and generating the schema that best supports your design.

       Regardless of how you build it, each model requires at least one data source object that specifies a connection to source data. You can create multiple data source objects in a single model to use data from different sources or vary connection properties for specific objects.

       Data source objects can be managed independently of other objects in your model. After you create a data source, you can change its properties later, and then pre-process the model to ensure the data is retrieved correctly.

Click here for step by step instruction to create data source.

Data Source View

         A data source view (DSV) is an abstraction of a relational data source that becomes the basis of the cubes and dimensions you create in a multidimensional project. The purpose of a DSV is to give you control over the data structures used in your project, and to work independently of the underlying data sources (for example, the ability to rename or concatenate columns without directly modifying the original data source).

        You can build multiple data source views in an Analysis Services project or database on one or more data sources, and construct each one to satisfy the requirements for a different solution.

Click here for step by step instruction to create data source view.

Thursday, 28 November 2013

SSAS Tutorials : Multidimensional Modeling

SSAS Multidimensional Modelling Tutorials

  1. Data Source and Data Source View
  2. Dimension and Cube
  3. Measures, Attributes and Hierarchies
  4. Advanced Attribute and Dimension Properties
  5. Relationships Between Dimensions and Measure Groups
  6. Calculations
  7. Key Performance Indicators (KPIs)
  8. Actions
  9. Perspectives and Translations
  10. Administrative Roles

SQL Server Analysis Services (SSAS) business intelligence (BI) semantic modelling options

SQL Server Analysis Services (SSAS) provides three different options to create business intelligence semantic model. Namely those 3 options are tabular, multidimensional and PowerPivot.

  • Tabular model uses relational database modelling objects such as tables, views and relationships for modeling data, and the xVelocity in-memory analytics engine for storing and calculating data.
  • Multidimensional model uses OLAP modelling constructs such as cubes and dimensions and uses MOLAP, ROLAP, or HOLAP storage.
  • PowerPivot model is a self-service BI solution that lets business analysts build an analytical data model inside an Excel workbook using the PowerPivot for Excel add-in. PowerPivot also uses xVelocity in-memory analytics engine within Excel and on SharePoint.


Table below summarizes feature available for each modelling option.


 MultidimensionalTabularPowerPivot
ActionsYesNoNo
AggregationsYesNoNo
Calculated MeasuresYesYesYes
Custom AssembliesYesNoNo
Custom RollupsYesNoNo
Distinct CountYesYes (via DAX)Yes (via DAX)
DrillthroughYesYesYes (detail opens in separate worksheet)
HierarchiesYesYesYes
KPIsYesYesYes
Linked objectsYesNoYes (linked tables)
Many-to-many relationshipsYesNoNo
Parent-child HierarchiesYesYes (via DAX)Yes (via DAX)
PartitionsYesYesNo
PerspectivesYesYesYes
Semi-additive MeasuresYesYesYes
TranslationsYesNoNo
User-defined HierarchiesYesYesYes
WritebackYesNoNo
 
See below links to learn about each modelling option.