'Lookup value in Query

I'm trying to replace the local variables i've declared below to capture them in calculated columns.

So the [YTD] column would look up Today's Date - 1 and return the [FiscalDayOfYear] making everything in that column less than that "YTD" and anything greater "NA"

DECLARE @ytd FLOAT = 124
DECLARE @monthly FLOAT = 4

SELECT *
FROM(
 SELECT [Date]
      ,[FiscalDayOfYear]
      ,[FiscalYear]
      ,[FiscalMonthNumber]
      ,[FiscalWeekNumber]
      ,[YTD] = IIF([FiscalDayOfYear] <= @ytd, 'YTD','NA')
      ,[MTD] = IIF(([FiscalDayOfYear] <= @ytd) AND ([FiscalMonthNumber] = @monthly), 'MTD','NA')
  FROM [DL].[Date]
 WHERE  CONVERT(date,Date) >= '2018-01-01') AS DT
UNPIVOT 
(
  [FLAG] FOR FLAGS IN ([YTD], [MTD])
) AS pivoted_tbl
WHERE [FLAG] != 'NA'

Because of our fiscal calendar I can't just use the Date column I've got to compare to day of year in our calendar.

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source