Chapter 9: Date & Time Functions 2/2: Time Intelligence Functions 269
Analysis
The second formula shows sales of the previous year. The third formula expresses the
current year as a ratio of the previous year. The fourth formula returns a blank instead
of a division-by-zero error. Please note that the third parameter here for DATEADD()
is YEAR and not YEAR(). The last formula is quite complex. You may want to break it
down into separate measures as follows:
=SUM(FactSales[SalesAmount])
=CALCULATE([Measure 1],DATEADD(DimDate[Datekey],-1,YEAR))
=IF([Measure 2],[Measure 1]/[Measure 2],BLANK())
You can further simplify the CALCULATE() function by using the measure itself as a
function. If you try this, the SetFilter argument must be in parentheses and there is
no comma after the measure. Here’s an example:
[Measure 1](DATEADD(DimDate[Datekey],-1,YEAR))
DATESBETWEEN()
Sales Amount from the Order Details table has been added to the Values drop-
zone before creating the measures. You will also need the Year, Quarter, Month, and
OrderDate columns from the Orders table. DATESBETWEEN() returns a range of dates.
The first parameter is the date column. The second and third parameters are the start
and end dates. The range of dates returned is inclusive of the start and end dates.
The examples are measures on the Order Details table.
Syntax
=CALCULATE(SUM('Order Details'[Sales Amount]),
DATESBETWEEN(Orders[OrderDate],DATE(1997,12,1),DATE(1997,12,3)))
=CALCULATE(SUM('Order Details'[Sales Amount]),
DATESBETWEEN(Orders[OrderDate],DATE(1998,5,4),BLANK()))
=CALCULATE(SUM('Order Details'[Sales Amount]),
DATESBETWEEN(Orders[OrderDate],BLANK(),DATE(1996,7,5)))