Gomaa Intelligence.Data Hub
عربي

Foundations › Power BI › DAX: Calculate Smartly

DAX: Calculate Smartly

Date Table and Time Intelligence: YTD, YoY and the «Why Did Profit Fall?» Investigation

Lesson 10 / 14· 12 min

The manager asks: «our sales grew 20%, so why did profit fall?». To answer you must compare this year with last year, month by month and category by category. That is time intelligence, and it needs one thing before any formula: a proper date table.

The date table

A separate table with one row per day and no gaps, whose date column has the Date type and is related to the date column of the fact table. Then you mark it as the Date table. Without that, functions like SAMEPERIODLASTYEAR will not behave.

DAX
Date = CALENDAR(DATE(2024, 1, 1), DATE(2025, 12, 31))
Year = YEAR('Date'[Date])
MonthNum = MONTH('Date'[Date])

That is how you build a date table with New table in Power BI Desktop. In our model DimDate already follows the same idea.

▶ Try DAX
Total Sales = SUM(FactSales[Sales])
Orders = COUNTROWS(FactSales)

First: sales month by month in 2025. Every row is a month context.

YTD, previous year and YoY %

▶ Try DAX
Total Sales = SUM(FactSales[Sales])
Sales YTD = TOTALYTD([Total Sales], DimDate[Date])
Sales LY = CALCULATE([Total Sales], SAMEPERIODLASTYEAR(DimDate[Date]))
YoY % = DIVIDE([Total Sales] - [Sales LY], [Sales LY])

Sales YTD accumulates from the start of the year. Sales LY returns the same month of last year. YoY % compares them. Notice YoY % is simple because it builds on ready measures.

❓ The current filter is March 2025. What does SAMEPERIODLASTYEAR(DimDate[Date]) return?

Exercise 1 — Write a measure named Answer that returns sales of the same period last year.

Exercise 2 — Write a measure named Answer for year-over-year growth YoY %: (sales − last year's sales) ÷ last year's sales.

Exercise 3 — Write a measure named Answer for year-to-date cumulative sales (YTD).

The investigation: sales rose, profit fell

Step one: compare the totals year over year. Step two: drop to category level. Step three: ask whether cost grew faster or prices fell.

▶ Try DAX
Total Sales = SUM(FactSales[Sales])
Total Cost = SUM(FactSales[Cost])
Profit = [Total Sales] - [Total Cost]
Margin % = DIVIDE([Profit], [Total Sales])
Sales LY = CALCULATE([Total Sales], SAMEPERIODLASTYEAR(DimDate[Date]))
Cost LY = CALCULATE([Total Cost], SAMEPERIODLASTYEAR(DimDate[Date]))
Sales YoY % = DIVIDE([Total Sales] - [Sales LY], [Sales LY])
Cost YoY % = DIVIDE([Total Cost] - [Cost LY], [Cost LY])

For 2025 as a whole: sales grew about 20% while cost grew about 31%. That gap is why profit fell.

▶ Try DAX
Total Sales = SUM(FactSales[Sales])
Total Cost = SUM(FactSales[Cost])
Profit = [Total Sales] - [Total Cost]
Margin % = DIVIDE([Profit], [Total Sales])
Margin LY = CALCULATE([Margin %], SAMEPERIODLASTYEAR(DimDate[Date]))
Margin Change = [Margin %] - [Margin LY]

At category level: Electronics lost far more margin than Home and Fashion. The problem is not everywhere: it is the cost of one specific category.

❓ The Electronics margin fell more than the rest. What is the best next step in the investigation?

Exercise 4 — Write a measure named Answer for the change in profit versus the same period last year (current profit − last year's profit).

Try it in real Power BI Desktop

Summary
  • Time intelligence needs a complete date table that is related and marked as the Date table.
  • TOTALYTD accumulates from the start of the year; SAMEPERIODLASTYEAR returns the same period last year.
  • YoY % = (current − previous) ÷ previous using DIVIDE.
  • A good investigation goes from total to category to detail, comparing sales, cost and margin.

Need a real Power BI dashboard for your business?

I design a data model and an executive dashboard fitted to your business and your real data.

Request an analytics service ↗
Get your data analyzed by an expert ↗