DP-600: implementing time measures with time functions in DAX
I will show how to implement time intelligence measures in DAX (e.g., Year-to-Date, MoM, YoY) using time functions. This skill is fundamental for business reports, semantic model optimization and is frequently assessed in DP-600 skills. This article focuses on best practices, concrete examples and performance tips so measures behave predictably in dashboards with filters and large data volumes.
What you need to know
Time intelligence measures in DAX allow comparing values over time: running totals, previous period comparison, percent change, moving average, among others. For them to work correctly, you need a well-defined date table and correct relationships between that table and the fact tables. The main functions are: CALCULATE, DATEADD, PARALLELPERIOD, SAMEPERIODLASTYEAR, TOTALYTD, DATESBETWEEN, and filter-context functions like ALL and ALLSELECTED.
Practical concept: in a fact table Vendas with columns [OrderDate] and [SalesAmount], use a date table Dates with [Date] and additional columns like Year, MonthNumber, MonthName, Quarter. A simple base measure is:
Total Sales = SUM(Vendas[SalesAmount])
To compare with the same period last year we use, for example:
Total Sales LY = CALCULATE([Total Sales], SAMEPERIODLASTYEAR(Dates[Date]))
Without a marked date table and correct relationships, functions like SAMEPERIODLASTYEAR return inconsistent results.
How it works in practice
Step-by-step to create robust and performant time measures, with simple numeric examples:
Ensure the Date Table: create a continuous date table that covers the whole period of the data. For example, if you have sales between 2018-01-01 and 2024-09-30, create the date table with that range. In DAX, you can start with:
Dates = CALENDAR(MIN(Vendas[OrderDate]), MAX(Vendas[OrderDate])) Dates[Year] = YEAR(Dates[Date]) Dates[MonthNumber] = MONTH(Dates[Date]) Dates[MonthName] = FORMAT(Dates[Date], "MMMM") Dates[Quarter] = "Q" & INT((MONTH(Dates[Date]) - 1) / 3) + 1Mark this table as a Date Table in the model so time functions know which column to use.
Correct relationships: link Dates[Date] to Vendas[OrderDate] in a one-to-many relationship (single direction). In a typical star schema, each fact table should connect to the date table. If there are multiple dates (OrderDate, ShipDate), consider creating additional relationships and using USERELATIONSHIP when necessary.
Base measure: always define a synthetic, reusable measure. This eases debugging and performance:
Total Sales = SUM(Vendas[SalesAmount])In large datasets (for example 1–5 million rows), base measures should be calculated with simple aggregations that the VertiPaq engine can compress efficiently.
Common time measures — practical examples with illustrative numbers:
- Year-to-date (YTD):
Total Sales YTD = TOTALYTD([Total Sales], Dates[Date])If in 2024 you have monthly sales like Jan=10,000€, Feb=12,000€, Mar=8,000€, the YTD in March will be 30,000€.
- Previous period (previous month):
Total Sales Prev Month = CALCULATE([Total Sales], DATEADD(Dates[Date], -1, MONTH))If April had 50,000€ and March 45,000€, this measure returns 45,000€ when the report context is April.
- Same period last year:
Total Sales LY = CALCULATE([Total Sales], SAMEPERIODLASTYEAR(Dates[Date]))Comparing May 2024 with May 2023: if 2023 had 40,000€ and 2024 48,000€, the difference is 8,000€.
- Year-over-year percent change:
YoY % = DIVIDE([Total Sales] - [Total Sales LY], [Total Sales LY])With the examples above, YoY % = DIVIDE(48,000 - 40,000, 40,000) = 0.20 → 20%.
Filter context and selections: for dashboards with slicers (e.g., year, campaign), use ALLSELECTED when you want to respect the user's selections but ignore intrinsic visual filters:
Sales Selected Period = CALCULATE([Total Sales], ALLSELECTED(Dates))Practical difference: ALL removes all filters, while ALLSELECTED keeps what the user chose at the report level or in slicers, preserving the expected interactivity.
Performance: in DirectQuery, functions that materialize large tables can degrade performance. Avoid FILTER over entire tables and prefer specific functions (TOTALYTD, SAMEPERIODLASTYEAR). For models with 10M+ rows, consider aggregations at the database level or using Import mode with pre-calculated aggregations. In tests, TOTALYTD in Import is typically 5–20x faster than the same logic implemented with FILTER in DirectQuery.
Common errors
Not using a marked date table: without marking, time functions can return wrong dates or not work. This is a frequent cause of discrepancies in financial reports.
Measures dependent on incorrect context: using ALL instead of ALLSELECTED can make a visual stop responding to user slicers. For example, a KPI that shows total sales for the full year even when the user filters to a quarter is a sign of improper use of ALL.
Using DATEADD with missing dates: DATEADD assumes continuity; if a month has no records, DATEADD can shift to an empty set. In scenarios with months without sales, PARALLELPERIOD or SAMEPERIODLASTYEAR are often safer.
How to practice
Practice in Power BI Desktop or in Fabric with a sales dataset. Create the date table, implement the example measures and test with slicers (year, quarter, month) and with hierarchies. Try scenarios with missing data (e.g., remove all records for a month) to see how each function reacts. For practical assessment and official practice, use the Microsoft official Practice Assessment (free) and the Microsoft study guide for DP-600 — these are official resources that help validate skills without relying on unauthorized material.
In summary
- Create and always mark a complete date table to ensure time functions work correctly.
- Define a base measure and build time measures with CALCULATE + time functions (TOTALYTD, DATEADD, SAMEPERIODLASTYEAR).
- Use ALLSELECTED when you need to respect user selections and avoid accidentally removing essential filters.
- Optimize for performance: avoid unnecessary iterators in DirectQuery and prefer built-in time functions; consider aggregations when working with millions of rows.