Dates = VAR StartDate = DATE(2015, 1, 1) VAR EndDate = DATE(2031, 12, 31) VAR FinancialYearStartMonth = 4 VAR TodayDate = TODAY() -------------------------------- -- STEP 1: Base date table -------------------------------- VAR BaseCalendar = ADDCOLUMNS ( CALENDAR(StartDate, EndDate), // Base components "Year", YEAR([Date]), "Month Number", MONTH([Date]), "Month Name", FORMAT([Date], "MMMM"), "Month Short", FORMAT([Date], "MMM"), "Day", DAY([Date]), "Day of Year", DATEDIFF(DATE(YEAR([Date]), 1, 1), [Date], DAY) + 1, // Week-based logic "Week Number", WEEKNUM([Date], 2), // ISO Week Number "ISO Week Number", VAR jan4 = DATE(YEAR([Date]), 1, 4) VAR jan4Weekday = WEEKDAY(jan4, 2) VAR firstThursday = jan4 - jan4Weekday + 4 RETURN INT(( [Date] - firstThursday + 10 ) / 7), "Weekday Number", WEEKDAY([Date], 2), "Weekday Name", FORMAT([Date], "dddd"), "Weekday Short", FORMAT([Date], "ddd"), "Is Weekend", IF(WEEKDAY([Date], 2) > 5, TRUE(), FALSE()), // Quarters and sort keys "Quarter", "Q" & FORMAT([Date], "Q"), "Quarter Number", QUARTER([Date]), "Month Year", FORMAT([Date], "MMM YYYY"), "Year-Month", FORMAT([Date], "YYYY-MM"), // Start/end of periods "Start of Month", DATE(YEAR([Date]), MONTH([Date]), 1), "End of Month", EOMONTH([Date], 0), "Start of Week", [Date] - WEEKDAY([Date], 2) + 1, "End of Week", [Date] + (7 - WEEKDAY([Date], 2)), // Season (UK Meteorological) "Season", SWITCH (TRUE(), MONTH([Date]) IN {3, 4, 5}, "Spring", MONTH([Date]) IN {6, 7, 8}, "Summer", MONTH([Date]) IN {9, 10, 11}, "Autumn", "Winter" ), // Financial Year Number "Financial Year Number", VAR m = MONTH([Date]) VAR y = YEAR([Date]) RETURN IF(m >= FinancialYearStartMonth, y, y - 1), // Financial Year text format "Financial Year", VAR m = MONTH([Date]) VAR y = YEAR([Date]) RETURN IF(m >= FinancialYearStartMonth, FORMAT(y, "0000") & "/" & FORMAT(y + 1, "0000"), FORMAT(y - 1, "0000") & "/" & FORMAT(y, "0000") ), // Financial Month "Financial Month", MOD(MONTH([Date]) - FinancialYearStartMonth + 12, 12) + 1, // Financial Quarter "Financial Quarter", VAR fMonth = MOD(MONTH([Date]) - FinancialYearStartMonth + 12, 12) + 1 RETURN "Q" & INT((fMonth + 2) / 3), // Week of Month "Week of Month", INT((DAY([Date]) - 1) / 7) + 1, // Sorting helpers "Month Sort", MONTH([Date]), "Day of Week Sort", WEEKDAY([Date], 2), "Quarter Sort", QUARTER([Date]) ) -------------------------------- -- STEP 2: Dependent columns + Relative logic -------------------------------- VAR FinalTable = ADDCOLUMNS ( BaseCalendar, // Financial Week Number "Financial Week Number", VAR fyStart = IF( MONTH([Date]) >= FinancialYearStartMonth, DATE(YEAR([Date]), FinancialYearStartMonth, 1), DATE(YEAR([Date]) - 1, FinancialYearStartMonth, 1) ) RETURN DATEDIFF(fyStart, [Date], WEEK) + 1, // Current flags "Is Today", IF([Date] = TodayDate, TRUE(), FALSE()), "Is Current Month", IF(YEAR([Date]) = YEAR(TodayDate) && MONTH([Date]) = MONTH(TodayDate), TRUE(), FALSE()), "Is Current Year", IF(YEAR([Date]) = YEAR(TodayDate), TRUE(), FALSE()), "Is Current Financial Year", VAR CurrentFY = IF(MONTH(TodayDate) >= FinancialYearStartMonth, YEAR(TodayDate), YEAR(TodayDate) - 1) RETURN [Financial Year Number] = CurrentFY, -------------------------------- -- Relative Period Flags -------------------------------- "Relative Days", SWITCH(TRUE(), [Date] = TodayDate, "Today", [Date] = TodayDate - 1, "Yesterday", [Date] = TodayDate + 1, "Tomorrow", [Date] >= TodayDate - 7 && [Date] < TodayDate, "Last 7 Days", [Date] > TodayDate && [Date] <= TodayDate + 7, "Next 7 Days", BLANK() ), "Relative Months", SWITCH(TRUE(), YEAR([Date]) = YEAR(TodayDate) && MONTH([Date]) = MONTH(TodayDate), "Current Month", EOMONTH([Date], 0) = EOMONTH(TodayDate, -1), "Last Month", EOMONTH([Date], 0) = EOMONTH(TodayDate, 1), "Next Month", BLANK() ), "Relative Quarters", VAR CurrQtr = QUARTER(TodayDate) VAR CurrYr = YEAR(TodayDate) VAR RowQtr = QUARTER([Date]) VAR RowYr = YEAR([Date]) RETURN SWITCH(TRUE(), RowYr = CurrYr && RowQtr = CurrQtr, "Current Quarter", (RowYr = CurrYr && RowQtr = CurrQtr - 1) || (RowYr = CurrYr - 1 && CurrQtr = 1 && RowQtr = 4), "Last Quarter", (RowYr = CurrYr && RowQtr = CurrQtr + 1) || (RowYr = CurrYr + 1 && CurrQtr = 4 && RowQtr = 1), "Next Quarter", BLANK() ), "Relative Years", SWITCH(TRUE(), YEAR([Date]) = YEAR(TodayDate), "Current Year", YEAR([Date]) = YEAR(TodayDate) - 1, "Last Year", YEAR([Date]) = YEAR(TodayDate) + 1, "Next Year", BLANK() ), -------------------------------- -- Rolling Period Flags -------------------------------- "Rolling Period", SWITCH(TRUE(), [Date] >= TodayDate - 30 && [Date] <= TodayDate, "Last 30 Days", [Date] >= TodayDate - 90 && [Date] <= TodayDate, "Last 90 Days", [Date] >= EDATE(TodayDate, -12) && [Date] <= TodayDate, "Last 12 Months", BLANK() ), -------------------------------- -- Relative Indexes (Numeric) -------------------------------- // Days difference from today "Relative Day Index", DATEDIFF(TodayDate, [Date], DAY), // Months difference from current month "Relative Month Index", (YEAR([Date]) - YEAR(TodayDate)) * 12 + (MONTH([Date]) - MONTH(TodayDate)), // Quarters difference from current quarter "Relative Quarter Index", ((YEAR([Date]) - YEAR(TodayDate)) * 4) + (QUARTER([Date]) - QUARTER(TodayDate)), // Years difference from current year "Relative Year Index", YEAR([Date]) - YEAR(TodayDate) ) RETURN FinalTable