Wednesday, July 11, 2018

Generic DateTime dimension - Part 2

In Part 1 of this post blog i talked about a generic dimension for Date, which is pretty simple and effective.

Continuing from there, below are the Calculation for each field:


  1. [DATE_PKEY]  AS (CONVERT([int],CONVERT([char](8),[DATE],(112))))
  2. [DAY_OF_MONTH]  AS (datepart(day,[DATE]))
  3. [DAY_SUFFIX]  AS (case when datepart(day,[DATE])=(13) OR datepart(day,[DATE])=(12) OR datepart(day,[DATE])=(11) then CONVERT([varchar],datepart(day,[DATE]))+'th' when right(datepart(day,[DATE]),(1))=(1) then CONVERT([varchar],datepart(day,[DATE]))+'st' when right(datepart(day,[DATE]),(1))=(2) then CONVERT([varchar],datepart(day,[DATE]))+'nd' when right(datepart(day,[DATE]),(1))=(3) then CONVERT([varchar],datepart(day,[DATE]))+'rd' else CONVERT([varchar],datepart(day,[DATE]))+'th' end)
  4. [DAY_NAME]  AS (datename(weekday,[DATE]))
  5. [DAY_OF_WEEK]  AS (datepart(weekday,[DATE]))
  6. [DAY_OF_QUARTER]  AS (datediff(day,dateadd(quarter,datediff(quarter,(0),[DATE]),(0)),[DATE])+(1))
  7. [DAY_OF_YEAR]  AS (datepart(dayofyear,[DATE]))
  8. [WEEK_OF_MONTH]  AS ((datepart(week,[DATE])-datepart(week,dateadd(month,datediff(month,(0),[DATE]),(0))))+(1)),
  9. [WEEK_OF_QUARTER]  AS (datediff(week,dateadd(quarter,datediff(quarter,(0),[DATE]),(0)),[DATE])+(1))
  10. [WEEK_OF_YEAR]  AS (datepart(week,[DATE]))
  11. [MONTH]  AS (datepart(month,[DATE]))
  12. [MONTH_NAME]  AS (datename(month,[DATE])),
  13. [MONTH_OF_QUARTER]  AS (case when datepart(month,[DATE])=(10) OR datepart(month,[DATE])=(7) OR datepart(month,[DATE])=(4) OR datepart(month,[DATE])=(1) then (1) when datepart(month,[DATE])=(11) OR datepart(month,[DATE])=(8) OR datepart(month,[DATE])=(5) OR datepart(month,[DATE])=(2) then (2) when datepart(month,[DATE])=(12) OR datepart(month,[DATE])=(9) OR datepart(month,[DATE])=(6) OR datepart(month,[DATE])=(3) then (3)  end),
  14. [YEAR]  AS (datepart(year,[DATE])),
  15. [YEAR_NAME]  AS ('CY '+CONVERT([varchar],datepart(year,[DATE]))),
  16. [MONTH_YEAR]  AS ((left(datename(month,[DATE]),(3))+'-')+CONVERT([varchar],datepart(year,[DATE])))
  17. [MMYYYY]  AS (right('0'+CONVERT([varchar],datepart(month,[DATE])),(2))+CONVERT([varchar],datepart(year,[DATE])))
  18. [FIRST_DAY_OF_MONTH]  AS (CONVERT([date],dateadd(day, -(datepart(day,[DATE])-(1)),[DATE])))
  19. [LAST_DAY_OF_MONTH]  AS (CONVERT([date],dateadd(day, -datepart(day,dateadd(month,(1),[DATE])),dateadd(month,(1),[DATE]))))
  20. [FIRST_DAY_OF_QUARTER]  AS (CONVERT([date],dateadd(quarter,datediff(quarter,(0),[DATE]),(0))))
  21. [LAST_DAY_OF_QUARTER]  AS (CONVERT([date],dateadd(quarter,datediff(quarter,(-1),[DATE]),(-1))))
  22. [FIRST_DAY_OF_YEAR]  AS (CONVERT([date],dateadd(year,datediff(year,(0),[DATE]),(0))))
  23. [LAST_DAY_OF_YEAR]  AS (CONVERT([date],dateadd(year,datediff(year,(0),[DATE])+(1),(-1))))
  24. [CALENDER_YR_QUARTER]  AS (datepart(quarter,[DATE]))
  25. [FINANCIAL_YR_QUARTER]  AS (case datepart(quarter,[DATE]) when (1) then (4) when (2) then (1) when (3) then (2) when (4) then (3)  end)
  26. [FINANCIAL_YEAR]  AS (case datepart(quarter,[DATE]) when (1) then (CONVERT([varchar],datepart(year,[DATE])-(1))+'-')+CONVERT([varchar],datepart(year,[DATE])) when (2) then (CONVERT([varchar],datepart(year,[DATE]))+'-')+CONVERT([varchar],datepart(year,[DATE])+(1)) when (3) then (CONVERT([varchar],datepart(year,[DATE]))+'-')+CONVERT([varchar],datepart(year,[DATE])+(1)) when (4) then (CONVERT([varchar],datepart(year,[DATE]))+'-')+CONVERT([varchar],datepart(year,[DATE])+(1))  end)

No comments:

Post a Comment

If you liked the post, please share it.
Subscribe to your email and receive new articles on your email