Search code examples
ssasinteger-overflow

Integer key to date in a named calculation


Trying to do a named calculation in SSAS from a integer key to Quarter. IntegerDateKey Below:

  SalesDateKey
   20100101
   20100101
   20100101
   20100102
   20100102
   20100102
   20100102

This doesn't work and no matter how I cast it, I can't get it to work.

  CONVERT(char(4), YEAR(SalesDateKey))+
  CASE
      WHEN MONTH(SalesDateKey) BETWEEN 1 AND 3 THEN 'Q1'
      WHEN MONTH(SalesDateKey) BETWEEN 4 AND 6 THEN 'Q2'
      WHEN MONTH(SalesDateKey) BETWEEN 7 AND 9 THEN 'Q3'
      ELSE 'Q4'
  END

Any help would be greatly appreciated. Please test your findings on a named calculation in SSAS.


Solution

  • Figured it out!

    CAST(DATEPART(YEAR, CAST(CAST(SalesDateKey AS char(8)) AS DateTime)) AS 
    VARCHAR(4)) + ' - ' +
    CASE
       WHEN Month(CAST(CAST(SalesDateKey AS CHAR(8)) AS DATE)) BETWEEN 1 AND 3 
       THEN 'Q1'
       WHEN Month(CAST(CAST(SalesDateKey AS CHAR(8)) AS DATE)) BETWEEN 4 AND 6 
       THEN 'Q2'
       WHEN Month(CAST(CAST(SalesDateKey AS CHAR(8)) AS DATE)) BETWEEN 7 AND 9 
       THEN 'Q3'
       ELSE 'Q4'
    END