A Power BI developer creates a star schema with a fact table Sales and dimension tables Customer, Product, Date. The relationship between Sales and Date is active. The developer wants to create a measure that calculates the total sales for the previous month relative to any selected month. Which DAX expression should the developer use?
Trap 1: CALCULATE(SUM(Sales[Amount]), DATESMTD(Date[Date]))
Incorrect. DATESMTD returns a set of dates from the start of the month to the last date in the filter context, not the full previous month. This calculates month-to-date sales rather than total sales for the previous month.
Trap 2: CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Date[Date]))
Incorrect. SAMEPERIODLASTYEAR returns dates from the same period in the previous year, not the previous month. This would calculate sales from the same month last year, not the previous month.
Trap 3: CALCULATE(SUM(Sales[Amount]), DATEADD(Date[Date], -1, MONTH))
Incorrect. While DATEADD with -1 month shifts dates back one month, it can produce incorrect totals when the current filter context does not represent a full month (e.g., a range of dates within a month). PREVIOUSMONTH is more reliable for returning the entire prior month regardless of the current selection.
- A
CALCULATE(SUM(Sales[Amount]), DATESMTD(Date[Date]))
Why wrong: Incorrect. DATESMTD returns a set of dates from the start of the month to the last date in the filter context, not the full previous month. This calculates month-to-date sales rather than total sales for the previous month.
- B
CALCULATE(SUM(Sales[Amount]), SAMEPERIODLASTYEAR(Date[Date]))
Why wrong: Incorrect. SAMEPERIODLASTYEAR returns dates from the same period in the previous year, not the previous month. This would calculate sales from the same month last year, not the previous month.
- C
CALCULATE(SUM(Sales[Amount]), DATEADD(Date[Date], -1, MONTH))
Why wrong: Incorrect. While DATEADD with -1 month shifts dates back one month, it can produce incorrect totals when the current filter context does not represent a full month (e.g., a range of dates within a month). PREVIOUSMONTH is more reliable for returning the entire prior month regardless of the current selection.
- D
CALCULATE(SUM(Sales[Amount]), PREVIOUSMONTH(Date[Date]))
Correct. PREVIOUSMONTH(Date[Date]) returns a set of dates for the entire previous month relative to the last date in the filter context. When used in CALCULATE, it changes the filter on the Date table to that month, yielding total sales for the previous month.