Forum Discussion

ewkcoder's avatar
ewkcoder
Frequent Visitor
4 years ago

Custom Table - Slicer For Date Periods - Calendar function can not be Blank value

I have a custom table that builds date slicer options for four periods (Year to date, Quarter to date, Month to date, Last 30 days).  The DAX below has been running fine but today it failed.  The month to date is the one causing it to fail.  Today is the first day of the month and I have no data yet and am getting the following error "The start date or end date in Calendar function can not be Blank value.".

 

I would like to prevent this error by checking if no records exist then hide the Month to date slicer option.  How would I do this with the DAX below?

 

 

calendar_mtd_qtd_ytd =
VAR TodayDate = TODAY()
VAR YearStart = CALCULATE(STARTOFYEAR(xxx[Created Date]), YEAR(xxx[Created Date]) = YEAR(TodayDate))
VAR QuarterStart = CALCULATE(STARTOFQUARTER(xxx[Created Date]), YEAR(xxx[Created Date]) = YEAR(TodayDate), QUARTER(xxx[Created Date]) = QUARTER(TodayDate))
VAR MonthStart = CALCULATE(STARTOFMONTH(xxx[Created Date]), YEAR(xxx[Created Date]) = YEAR(TodayDate), MONTH(xxx[Created Date]) = Month(TodayDate))
VAR Result =
UNION (
ADDCOLUMNS (
CALENDAR(YearStart, TodayDate),
"Selection", "YTD"
),
ADDCOLUMNS (
CALENDAR(QuarterStart, TodayDate),
"Selection", "QTD"
),
ADDCOLUMNS (
CALENDAR(MonthStart, TodayDate),
"Selection", "MTD"
),
ADDCOLUMNS (
CALENDAR(TODAY()-31, TodayDate-1),
"Selection", "Last 30 Days"
)
)
RETURN
Result

5 Replies