Sql-server – How to emulate performant date dimension for SSAS model in tabular mode / direct query

performancesql serverssasssas-2016tabular-model

I have an indexed datetime column – lets call it "CreatedDateUTC" in table "Entity" in MSSQL db. Column contains UTC datetime. User connecting to db has known timezone offset. I have another table – DateDim with primary key column Date. This is to support date dimension for SSAS/Tabular model/Direct query scenario.

So on model level "Entity" is defined as SQL query

SELECT CAST(fn_AdjustCurrentTimezoneOffset(CreatedDateUTC) as Date) as CreatedDate 
FROM dbo.Entity

The SQL query featuring DateDim from report then looks like

SELECT * FROM [Entity] e 
JOIN [DateDim] d on CAST(fn_AdjustCurrentTimezoneOffset(e.CreatedDateUTC) as Date) = d.Date

which is unbearably slow when [Entity] table grows large. Even if i resign on adjusting timezone (which I don't want to), it doesn't get much better – i.e. this query is slow as well:

SELECT * FROM [Entity] e 
JOIN [DateDim] d on CAST(e.CreatedDateUTC as Date) = d.Date

My feeling is the issue here is with CreatedDateUTC datetime – it needs to be converted to indexed CreatedDateDayUTC date upfront (not a problem since I query special reporting db). My problem is with the timezone – since users have different timezone offset I cannot simply precalculate this.

On SQL level I would be able to get around this by this ugly hack – precalculate three dates (Date1=DATEADD(day,-1,CreatedDateDayUTC)/Date2=CreatedDateDayUTC/Date3=DATEADD(day,1,CreatedDateDayUTC)) on Entity table and adjusting join to something like:

SELECT * FROM [Entity] e 
JOIN [DateDim] d on CAST(e.CreatedDateUTC as Date) = d.Date 
  and (e.Date1 = d.Date or e.Date2 = d.Date and e.Date3 = d.Date)

However this can't be translated to query defining "Entity" on model level (there has to be just one column which can create relationship do DateDim Date column).

Any idea how to get around this ?

Best Answer

Thanks to @GregGalloway to set me to right track. I ended up with following schema:

1) I've added new mapping table [DateMap] that materialize mapping between UTC datetime (in those weird quarter-hours) to local time zone (again in quarter-hours)

CREATE TABLE [dbo].[DateMap] (
  -- time zone code (set as context info for given user)
  [TimeZoneCode] smallint NOT NULL,
  -- quarter hours means datetime trimmed to xx:00, xx:15, xx:30, xx:45
  [UTC_QuarterHours] smalldatetime NOT NULL,
  -- local time zone
  [LTZ_QuarterHours] smalldatetime NOT NULL,
  CONSTRAINT [PK_tmp_DateMap] PRIMARY KEY ([TimeZoneCode] ASC, [UTC_QuarterHours] ASC)
)

2) [DimDate] has primary key set to LTZ_QuarterHours

CREATE TABLE [dbo].[DimDates](
  [LTZ_QuarterHours] smalldatetime NOT NULL,
  [Date] date NOT NULL,
  [Year] int NOT NULL,
  [Month] int NOT NULL,
  [Quarter] int NOT NULL,
  CONSTRAINT PK_tmp_DimDates PRIMARY KEY ([LTZ_QuarterHours] ASC)
)

3) [Entity] table has new column [CreatedDateUTC_QuarterHours] - which means original utc date trimmed to quarter hours

CREATE TABLE [dbo].[Entity] (
  [EntityID] int NOT NULL PRIMARY KEY,
  [CreatedDateUTC] datetime,
  [CreatedDateUTC_QuarterHours] smalldatetime
)

4) Entity in model is defined as SQL query like

SELECT e.*, m.[LTZ_QuarterHours] FROM [Entity] e
JOIN [DateMap] m on e.CreatedDateUTC_QuarterHours = m.UTC_QuarterHours and 
  m.TimeZoneCode = fn_GetCurrentUserTimeZoneCode()

Which is pretty fast.