Forum Discussion
SAMEPERIODLASTYEAR()
- 5 years ago
Hi auxilio99357 ,
The previous code I supplied for DAX and Power Query M were both intended to be new columns in your calendar table, not measures.
For your specific scenario i.e. sales only go up to two months ago, I would recommend adding a relative month column into your calendar table, something like this:
DAX
_relativeMonth = (YEAR(DimDates[Date]) * 12 + MONTH(DimDates[Date])) - (YEAR(TODAY()) * 12 + MONTH(TODAY()))PQ M
(Date.Year([Date]) * 12 + Date.Month([Date])) - (Date.Year(DateTime.LocalNow()) * 12 + Date.Month(DateTime.LocalNow()))The usage of this in a measure would look something like this:
LYTD = CALCULATE( SUM(Sales[U_Liq]), SAMEPERIODLASTYEAR(DimDates[Date]), DimDates[relativeMonth] <= -2 )Pete
Here is one way to do it without time intelligence.
LYTD =
VAR vMaxDate =
MAX ( DimDate[Date] )
RETURN
CALCULATE (
SUM ( Sales[U_Liq] ),
FILTER (
ALL ( DimDate[Date] ),
DimDate[Date]
<= EOMONTH (
vMaxDate,
-12
)
&& DimDate[Date]
>= DATE ( YEAR ( vMaxDate ) - 1, 1, 1 )
)
)
Pat