Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

www.sqlbi.

com

[email protected]

Time Intelligence in DAX

a brand of

Who’s that guy? Who’s that guy?


• BI Expert and Consultant • BI Expert and Consultant
• Founder of www.sqlbi.com • Founder of www.sqlbi.com
o Problem Solving o Problem Solving
o Complex Project Assistance o Complex Project Assistance
o Data Warehouse Assessments and Development o Data Warehouse Assessments and Development
o Courses, Trainings and Workshops o Courses, Trainings and Workshops
• Book Writer • Book Writer
• Microsoft Business Intelligence Partner • Microsoft Business Intelligence Partner
• SSAS Maestros – MVP – MCP • SSAS Maestros – MVP – MCP

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 

Calendar Table Year To Date: the easy way


TOTALYTD: life is easy
• First of all we need some data
• And a calendar table
SalesAmountYTD :=
o Built in Excel
o Or in a SQL Table TOTALYTD(
SUM (Sales[SalesAmount]),
• Calendar table properties Calendar[FullDate]
)
o All dates should be present
o No holes
o Otherwise time intelligence will not work

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

Use The Right Parameter Handling Fiscal Year


The parameter is the Date column in the Calendar Table The last, optional, parameter is the end of the fiscal year
Not the SalesOrderHeader[OrderDate] Default: 12-31 (or 31/12 - locale dependent)
Otherwise, you get wrong results
SalesAmountYTD :=
LineTotalYTD := TOTALYTD (
SUM (Sales[SalesAmount]),
TOTALYTD( Calendar[FullDate],
SUM (Sales[SalesAmount]), "06-30"
Sales[OrderDate] )
)

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]) )
)

Same Period Last Year DATEADD


Same period in previous year, CALCULATE is needed Similar to SAMEPERIODLASTYEAR, used to calculate different offsets
Specialized version of DATEADD

No hierarchies, here, different technique when compared with


Multidimensional SalesAmountlSPLY :=

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

SalesPPLY := • Tool Table (tool dimension in UDM)


CALCULATE(
o Reduces the number of measures
SUM (Sales [LineTotal]), o Base measures can be hidden
PARALLELPERIOD (Calendar[FullDate], -1, YEAR)
) • Drawbacks
o The code is harder to debug

Multiple Calendar Tables Counting Working Days


• Calendar is often a role dimension • How many working days in a date range?
o Many roles for a date o Easily solved with Calendar table
o Many calendar tables o Define a new column «WorkingDays»
• How many calendar tables? o Aggregate with SUM

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

Semi Additive Measures Current Account Balance


• Additive Measure
o SUM over all dimensions
• Non Additive Measure
o Different function over all dimensions
o Example: average of the sale price
• Semi Additive Measure
o SUM over some dimensions
o Different function over other dimensions
o Time is the standard exception for aggregations • Month level correct
o Examples • Quarter level wrong
• Warehouse stocking
• Current account balance • Year level wrong

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] )
)
)

Querying with Time Intelligence Querying with Time Intelligence


Using SUMMARIZE on the fact table yiedls an error, because dates in The solution is to use the calendar table as the primary one, changing
the fact table usually contain holes the shape of the query.

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

• Based on DAX formulas www.sqlbi.com


o Not no hierarchies
• Formulas are sometime complex
• But you can author very powerful calculations
• Thus… learn DAX 

Oh, just another thing…

Are you sure to get the best out of


your analytical environment?

Ask to SQLBI, we offer many services to help you:

Consulting Assessment Outsourcing Technical


Fellowship

Find out more on


www.sqlbi.com/consulting

You might also like