Time Intelligence in DAX - Alberto Ferrari PDF
Time Intelligence in DAX - Alberto Ferrari PDF
com
alberto.ferrari@sqlbi.com
a brand of
1
What is Time Intelligence? What do you need to know?
• Many different topics in one name • Not really much
o Year To Date o DAX basics
o Quarter To Date o CALCULATE basics
o Running Total • If not, add 100 to the session level
o Same period previous year
o Working days computation • Topic is DAX, we are going to use PowerPivot
o Fiscal Year
• Well…seems to be anything related with time,
handled in an intelligent way
2
Mark as Date Table Set Sorting Options
• In Tabular, use Mark as Date Table • Month names do not sort alphabetically
• Set the column containing the date o April is not the first month of the year
• Use Sort By Column
• Needed to make time intelligence works
• Set all sorting options in the proper way
• Used by Power View as metadata information
• Beware of sorting granularity
• Many tables can be marked as date table
o 1:1 between names and sort keys
3
Year To Date: the DAX way Running Total
• DATESYTD: Returns a set of dates, from the start of the year up to • Running total, as most of the more complex time intelligence
the parameter date aggregations, needs the CALCULATE version, because there is no
syntax sugaring here
• CALCULATE: Creates a filter context and performs the SUM
operation SalesAmountRT :=
CALCULATE (
SalesAmountYTD := SUM (Sales[Amount]),
FILTER (
CALCULATE ( ALL (Calendar),
Calendar[FullDate] <= MAX (Calendar[FullDate])
SUM (Sales[SalesAmount]), )
DATESYTD (Calendar[Date]) )
)
CALCULATE(
SalesSPLY :=
SUM (Sales[SalesAmount]),
DATEADD (Calendar[FullDate], -1, YEAR)
CALCULATE (
)
SUM (Sales[SalesAmount]),
SAMEPERIODLASTYEAR (Calendar[FullDate])
)
4
PARALLELPERIOD Period Table Approach
Returns a FULL period of dates shifted in time
• Many Time Intelligence Aggregations
The whole period is returned, regardless dates in the first parameter,
very useful to compute percentages
o Many measures
o User Experience might not be the best
o Use many, only if needed by the model • Handles any date range
o Try to use only one table • Works on any periods
o Many calendars leads to confusion
o And issues when slicing • No separation between fact tables and
dimensions in Tabular
• Use proper naming convention
5
Check Delayed Orders Handling DateTime
• How many orders were delayed? • If time is a useful information
o Easy: «ShipDate Greater Than DueDate» • Separate Date from Time
• How many working days of delay? o Date part Calendar Table
o New Calculated Column o Time part Time Table
o WorkingDayNumber • Reduces distinct values
• Incremental value from the start of table
• Delta can be computed as difference • Makes analysis much easier
6
SemiAdditive Measures Moving Annual Total
CALCULATE: to set the filter Moving window from the current date back one year
LASTDATE: to find the last child
LastBalance := CALCULATE(
SUM( SalesOrderDetail[LineTotal] ),
DATESBETWEEN(
CALCULATE ( OrderDate[Date],
SUM (Balances[Balance]), NEXTDAY(
LASTDATE (Date[Date]) SAMEPERIODLASTYEAR(
) LASTDATE( OrderDate[Date] )
)
),
LASTDATE( OrderDate[Date] )
)
)
DEFINE DEFINE
MEASURE 'Internet Sales'[PY Sales] = MEASURE 'Internet Sales'[PY Sales] =
CALCULATE( CALCULATE(
[Internet Total Sales], [Internet Total Sales],
SAMEPERIODLASTYEAR( 'Date'[Date] )
SAMEPERIODLASTYEAR( 'Date'[Date] ) )
) EVALUATE
ADDCOLUMNS(
EVALUATE FILTER(
SUMMARIZE(
SUMMARIZE( 'Date',
'Internet Sales', 'Date'[Calendar Year],
'Date'[Month],
'Date'[Calendar Year], 'Date'[Month Name],
'Date'[Month], "Sales", [Internet Total Sales]
'Date'[Month Name], ),
"Sales", [Internet Total Sales], [Sales] <> 0
"PY Sales", [PY Sales] ),
) "PY Sales", [PY Sales]
)
7
Conclusions
• Time Intelligence is pretty easy
o Many built-in functions
Thank you!
o Calendar table is very important Check daily our new articles on