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:
Continuing from there, below are the Calculation for each field:
- [DATE_PKEY] AS (CONVERT([int],CONVERT([char](8),[DATE],(112))))
- [DAY_OF_MONTH] AS (datepart(day,[DATE]))
- [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)
- [DAY_NAME] AS (datename(weekday,[DATE]))
- [DAY_OF_WEEK] AS (datepart(weekday,[DATE]))
- [DAY_OF_QUARTER] AS (datediff(day,dateadd(quarter,datediff(quarter,(0),[DATE]),(0)),[DATE])+(1))
- [DAY_OF_YEAR] AS (datepart(dayofyear,[DATE]))
- [WEEK_OF_MONTH] AS ((datepart(week,[DATE])-datepart(week,dateadd(month,datediff(month,(0),[DATE]),(0))))+(1)),
- [WEEK_OF_QUARTER] AS (datediff(week,dateadd(quarter,datediff(quarter,(0),[DATE]),(0)),[DATE])+(1))
- [WEEK_OF_YEAR] AS (datepart(week,[DATE]))
- [MONTH] AS (datepart(month,[DATE]))
- [MONTH_NAME] AS (datename(month,[DATE])),
- [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),
- [YEAR] AS (datepart(year,[DATE])),
- [YEAR_NAME] AS ('CY '+CONVERT([varchar],datepart(year,[DATE]))),
- [MONTH_YEAR] AS ((left(datename(month,[DATE]),(3))+'-')+CONVERT([varchar],datepart(year,[DATE])))
- [MMYYYY] AS (right('0'+CONVERT([varchar],datepart(month,[DATE])),(2))+CONVERT([varchar],datepart(year,[DATE])))
- [FIRST_DAY_OF_MONTH] AS (CONVERT([date],dateadd(day, -(datepart(day,[DATE])-(1)),[DATE])))
- [LAST_DAY_OF_MONTH] AS (CONVERT([date],dateadd(day, -datepart(day,dateadd(month,(1),[DATE])),dateadd(month,(1),[DATE]))))
- [FIRST_DAY_OF_QUARTER] AS (CONVERT([date],dateadd(quarter,datediff(quarter,(0),[DATE]),(0))))
- [LAST_DAY_OF_QUARTER] AS (CONVERT([date],dateadd(quarter,datediff(quarter,(-1),[DATE]),(-1))))
- [FIRST_DAY_OF_YEAR] AS (CONVERT([date],dateadd(year,datediff(year,(0),[DATE]),(0))))
- [LAST_DAY_OF_YEAR] AS (CONVERT([date],dateadd(year,datediff(year,(0),[DATE])+(1),(-1))))
- [CALENDER_YR_QUARTER] AS (datepart(quarter,[DATE]))
- [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)
- [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