Date dimensions are very useful. With a date dimension / date table you can look up for any date which week it falls in, which year, which month, whether it is a public holiday, etc. This article shows what you can do with a date dimension. You can also download a script to create a date (table) / dimension yourself.

A date dimension is nothing more or less than a date table. Every date in the year is a separate row. Next to it are a number of columns that show which week that day falls in, the month, the quarter and a lot more. There is also a column that shows whether a date is a public holiday. That way you can quickly count public holidays. A date dimension works well in a data warehouse, but of course you can also simply create it in any database, such as Exact or Microsoft Dynamics.

Note: this is an example dimension. You can of course improve it yourself with fields that matter to you!

Date dimension example

If we now create the date dimension in the AdventureWorks database (so not the AdventureWorks dwh), you can see its power. We join the date dimension to the OrderDate. Then we can easily find several properties for this date!

SQL
SELECT TOP 100
SOH.SalesOrderID,
SOH.OrderDate,
DD.DatumId,
DD.WeekNummer,
DD.MaandNummerVanJaar,
DD.Kwartaal445Code,
DD.Kwartaal445Nummer
FROM Sales.SalesOrderHeader AS SOH
-- Hier zetten we de OrderDate om naar een integer en joinen hem met de Datum-dimensie DimDatum
LEFT OUTER JOIN DimDatum AS DD ON YEAR(SOH.OrderDate) * 10000 + MONTH(SOH.OrderDate) * 100 + DAY(SOH.OrderDate) = DD.DatumId

Also useful for Excel PowerPivot: load the date table as a tab. Then you can also use it in Excel PowerPivot.

Another example, give me all public holidays in SQL Server for 2013:

Public holidays 2013

If you have company-specific holidays and you know them in advance, you can also mark them in this table. This is useful when you want to run certain calculations and take public holidays into account.

Attached to this article is a script to create the date dimension table. After that comes an MS SQL Server stored procedure to fill the table.

If you like, I can also write a script that creates the table and inserts the data right away

Download

Frequently asked questions

What is a date dimension in SQL Server?

A date dimension is a date table in which every date is a separate row. Columns give the week, month, quarter of each day, and whether it is a public holiday. You use it in a data warehouse or in any other database.

How do I create a date table in SQL Server?

You download a script that creates the date dimension table. Then you fill the table with a stored procedure for MS SQL Server. You can extend the table with fields that matter to you.

How do I join a date dimension to OrderDate in AdventureWorks?

Convert OrderDate with YEAR(OrderDate) * 10000 + MONTH(OrderDate) * 100 + DAY(OrderDate) and join it on DimDatum.DatumId. You then get WeekNummer, MaandNummerVanJaar and Kwartaal445Code, among others.

How do I count public holidays in SQL Server with a date table?

The date dimension has a column that shows whether a date is a public holiday. With it you quickly count holidays, for example all public holidays of 2013. You can add company-specific holidays to the table yourself.

Can I also use the date table in Excel PowerPivot?

Yes. Load the date table as a tab in Excel. Then you can also use it in PowerPivot.

9 comments

Your email address is not shown. A photo comes from Gravatar, if you have one.

  1. Nice Ronald!

    Heb je dit ook in MySQL of in csv voor 2011/2012?

    Bvd,

  2. Ronald KraijesteijnAuthor

    Hierbij een CSV file met een datum dimensie vanaf 2002 t/m 2020.

  3. Super handig, Ronald! Komt goed van pas bij mijn eerste PowerPivot experimenten. Bedankt!

  4. Ronald KraijesteijnAuthor

    Zeker handig, eigenlijk kan je niet zonder bij power pivot.

  5. Mooie post, komt altijd van pas.

  6. Hoi, het script draait nu al 2 uur, voor 2010 tot 2020, lijkt me een beetje te lang, dus ik zal dan maar het csv-file importeren.

  7. Dat is niet normaal, normaal duurt het ongeveer 20 seconden. Misschien is er iets mis met je database. Je kunt het ook in stapjes vullen, je maakt dan eerst de tabel aan en voert daarna stukjes van de procedure uit. Succes :-)

  8. ah, het is gelukt met een aantal aanpassinkjes!
    het Engels_vullen script gebruikt andere namen voor de functions dan het nederlandse vullen script. Het nederlandse script werkt wel mits ik de databasenaam DWH_NL_REF aanpas naar eigen db en het DWH_NL_ISA stuk uitcomment
    thanks :)