I am looking forward to SQL Server 2016, which includes an update to DAX which will generate dates for you, but since it hasn’t been released yet, the need still exists to create a date Table. Since I am creating a Tabular Analysis Service instance, an identified date dimension is needed for time functions. There are a number of different ways to create a date dimension, here I am going to take advantage of recu There are a number of ways you can create a date dimension, here I am going to describe a neat way of creating one using a recursive CTE, which decreases the SQL code required to generate one.
Date Dimension Fields
There are a number of fields which are pretty standard for date tables. The fields can vary if you need to add things like Fiscal years or Month End dates which have business specific rules like last Friday of the month. The most common fields needed for a Date Dimension are listed here.
- DateKey – Integer key representing the date, for example 20150524
- CalendarDate – Date time field
- CalendarYear – Four digit year
- QuarterNumber – Number from 1 to 4
- QuarterName – First Quarter, Second Quarter, 1 Qtr 2015, First Quarter 2015 are some of the ways the name is listed. Pick whichever format people would like to see.
- MonthNumber – 1 for January, 2 for February, 3 for March etc.
- NameMonth – January, February, March etc.
- MonthDayNumber – July 15 would have the value of 15 here
- WeekDayNumber – Date of the Month. July 4 would have a 4 here.
- CalendarWeekofMonth – Counts the weeks within each month
- NameDay – Monday, Tuesday etc.
- DayNumber – Whether you choose 1 for Sunday or 1 for Monday is a business rule you need to find out first. This example shows 1 for Sunday, 2 for Monday etc.
- YearDay – Sometimes referred to as the Julian number this counts the days from 1- 365 and can be useful in some calculations
- YearWeek – Counts the weeks from 1 -52
As a best practice, remember reserved words such as DATE or Index or any other word which shows up as blue or pink in SQL Server Management Studio, should never be the names of any columns. Always select a name other than a reserved word when creating tables. Also friendly names containing spaces are great for exposing to users, but they are annoying in SQL Server, so leave the spaces out of the column names. Evaluate what dates you are going to be needing in the table so that you don’t have to go back and redo it. I am creating a smaller table here, but that is just because it is an example. Look at the dates you will be storing in your data warehouse when determining your starting dates, and set the end dates for probably about five years longer than you think the data warehouse will still be in use.
Using a CTE to Generate a Date Dimension
CTEs, which Microsoft added in 2005, is a great way to generate a date table by harnessing the power of computer to spin through your code, decreasing the need to write a lot of code. I am using a recursive CTE method here first published by Itzik Ben-Gan to generate the number table being used in the code below.
CREATE TABLE [dbo].[DimDate](
[DateKey] int NULL,
[CalendarDate] [smalldatetime] NULL,
[CalendarYear] [int] NULL,
[QuarterNumber] [int] NULL,
[QuarterName] [varchar](14) NULL,
[MonthNumber] [int] NULL,
[NameMonth] [nvarchar](30) NULL,
[MonthDayNumber] [int] NULL,
[CalendarWeekOfMonth] [int] NULL,
[NameDay] [nvarchar](30) NULL,
[DayNumber] [int] NULL,
[YearDay] [int] NULL,
[YearWeek] [int] NULL
) ON [PRIMARY]
/*Make sure you change the start and end dates listed here to the dates you wish to use in your table*/
DECLARE @StartDate smalldatetime = '01/01/2014'
DECLARE @EndDate smalldatetime = '12/31/2016' ; /* don't forget the semi-colon or you will get an error*/
/*This CTE is used to create a list of numbers used to generate the calendar*/
A00(N) AS (SELECT 1 UNION ALL SELECT 1),
A02(N) AS (SELECT 1 FROM A00 a, A00 b),
A04(N) AS (SELECT 1 FROM A02 a, A02 b),
A08(N) AS (SELECT 1 FROM A04 a, A04 b),
A16(N) AS (SELECT 1 FROM A08 a, A08 b),
A32(N) AS (SELECT 1 FROM A16 a, A16 b),
cteTally(N) AS (SELECT ROW_NUMBER() OVER (ORDER BY N) FROM A32),
/*Calendar dates are created here*/
CalendarBase as (
DateKey = n
, CalendarDate = DATEADD(day, n - 1, @StartDate )
WHERE N <= DATEDIFF(day, @StartDate , @EndDate +1)
/*Using the list of dates created above will populate your date table here*/
insert into dbo.DimDate(DateKey
DateKey = CONVERT(char(8), CalendarDate, 112)
, CalendarYear = YEAR(CalendarDate)
, QuarterNumber = (DATEPART(QUARTER,CalendarDate) )
, QuarterName = 'Quarter ' + cast((DATEPART(QUARTER,CalendarDate) ) as char(1)) +' ' + cast(YEAR(CalendarDate) as char(4))
, MonthNumber = MONTH(CalendarDate)
, NameMonth = DATENAME(Month, CalendarDate)
, WeekDayNumber = DATEPART(DAY, CalendarDate)
, CalendarWeekOfMonth = DATEDIFF(week, DATEADD(day,1, CalendarDate - DAY(CalendarDate) + 1) -1, CalendarDate) +1
, NameDay = DATENAME (Weekday,CalendarDate )
, DayNumber = DATEPART(Dw, CalendarDate)
, YearDay = DATEPART(DAYOFYEAR, CalendarDate)
, YearWeek = DATEPART(WEEK, CalendarDate)
After running this code you will have a date table created and loaded.
Data aficionado et SQL Raconteur