Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
StuartPQY
Frequent Visitor

Dynamic Time comparison across multiple years

Hello

 

I am hoping someone can help with this.  I have searched for several days and what ive seen is not quite right.

 

Scenario, I am creating an "Archive" set of data for expenses that will eventually span 2023 to 2031.  My data as now is only 2024 and 2025.

 

I wish to create a a view that will compare YTD or MTD across the years.  The code snippet below works perfectly for the current year only.

 

Value_CurrentPeriod = VAR PeriodSelector =
    SELECTEDVALUE(SEL_PERIOD[Period])

VAR LastDaySelected =
    MAX('DIM_Date'[Date])
VAR MonthSelectedDate =
    MONTH(LastDaySelected)
VAR YearSelectedDate =
    YEAR(LastDaySelected)
VAR QuarterSelectedDate =
    QUARTER(LastDaySelected)

VAR FilterDatesLTM =
        DATESINPERIOD('DIM_Date'[Date],LastDaySelected, -12, MONTH)

VAR MonthStartFinancialYear = 13
VAR YearFinancialYear =
    IF(MonthStartFinancialYear>MonthSelectedDate,YearSelectedDate-1, YearSelectedDate)
VAR FinancialStartDate =
    DATE(YearFinancialYear,MonthStartFinancialYear,1)
VAR FilterDatesYTD =
    DATESBETWEEN('DIM_Date'[Date],FinancialStartDate,LastDaySelected)

VAR Units =
    SELECTEDVALUE(SEL_UNITS[UnitsDivider])

VAR Result = SWITCH(PeriodSelector,
                        "MTD", CALCULATE(SUM('ArchiveData'[Value])),
                        "LTM", CALCULATE(SUM('ArchiveData'[Value]),FilterDatesLTM),
                        "YTD", CALCULATE(SUM('ArchiveData'[Value]),FilterDatesYTD))

VAR ResultUnits =
    Divide (Result,Units)
Return ResultUnits
 
Where the SEL tables that are mentioned are additional slicers.
 
I may be over complicating this to start as the code above is my start position.  (I use this on the in current year reports for Actual vs Budget)
 
My objective is similar code to the above that will dynamically calculate across the years as new years are added, i.e. I dont have to create a new measure each January.
 
I have seen commands such as SAMEPRIODLASTYEAR but this not good as I have several years.
 
I am hoping someone can provide some guidance on the sort of a solution I need.
1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @StuartPQY ,

 

To achieve dynamic MTD, YTD, and LTM comparisons across multiple years without rewriting the measure each January, you can modify your current DAX logic to be context-aware. Rather than focusing on a single selected year, the new measure should calculate values based on the row context of the visual. This approach allows comparisons across all years in your dataset without relying on hardcoded logic or limited functions like SAMEPERIODLASTYEAR.

Here’s a revised version of your measure that works dynamically across years. It respects slicer selections such as SEL_PERIOD and SEL_UNITS, and adapts to visual context, enabling you to drop it into a matrix with Year and Month and see side-by-side values for each year.

Value_Period_By_Year = 
VAR PeriodSelector = SELECTEDVALUE(SEL_PERIOD[Period])
VAR Units = SELECTEDVALUE(SEL_UNITS[UnitsDivider], 1)

VAR LastDateVisible = MAX('DIM_Date'[Date])
VAR ContextYear = YEAR(LastDateVisible)
VAR ContextMonth = MONTH(LastDateVisible)

VAR MonthStartFinancialYear = 13
VAR YearFinancialYear = IF(ContextMonth < MonthStartFinancialYear, ContextYear - 1, ContextYear)
VAR FinancialStartDate = DATE(YearFinancialYear, MonthStartFinancialYear, 1)

VAR MTD_Filter = DATESMTD('DIM_Date'[Date])
VAR LTM_Filter = DATESINPERIOD('DIM_Date'[Date], LastDateVisible, -12, MONTH)
VAR YTD_Filter = DATESBETWEEN('DIM_Date'[Date], FinancialStartDate, LastDateVisible)

VAR Result = 
    SWITCH(
        TRUE(),
        PeriodSelector = "MTD", CALCULATE(SUM('ArchiveData'[Value]), MTD_Filter),
        PeriodSelector = "YTD", CALCULATE(SUM('ArchiveData'[Value]), YTD_Filter),
        PeriodSelector = "LTM", CALCULATE(SUM('ArchiveData'[Value]), LTM_Filter)
    )

RETURN DIVIDE(Result, Units)

This version uses MAX('DIM_Date'[Date]) to pick up the latest visible date in the visual context and adjusts calculations accordingly. Since it doesn't filter to a specific year, it can be used in visuals that include multiple years without additional modification. Simply add your date hierarchy (e.g., Year and Month) to a matrix or line chart, and this measure will compute MTD, YTD, and LTM values for each year dynamically.

 

Best regards,

View solution in original post

1 REPLY 1
DataNinja777
Super User
Super User

Hi @StuartPQY ,

 

To achieve dynamic MTD, YTD, and LTM comparisons across multiple years without rewriting the measure each January, you can modify your current DAX logic to be context-aware. Rather than focusing on a single selected year, the new measure should calculate values based on the row context of the visual. This approach allows comparisons across all years in your dataset without relying on hardcoded logic or limited functions like SAMEPERIODLASTYEAR.

Here’s a revised version of your measure that works dynamically across years. It respects slicer selections such as SEL_PERIOD and SEL_UNITS, and adapts to visual context, enabling you to drop it into a matrix with Year and Month and see side-by-side values for each year.

Value_Period_By_Year = 
VAR PeriodSelector = SELECTEDVALUE(SEL_PERIOD[Period])
VAR Units = SELECTEDVALUE(SEL_UNITS[UnitsDivider], 1)

VAR LastDateVisible = MAX('DIM_Date'[Date])
VAR ContextYear = YEAR(LastDateVisible)
VAR ContextMonth = MONTH(LastDateVisible)

VAR MonthStartFinancialYear = 13
VAR YearFinancialYear = IF(ContextMonth < MonthStartFinancialYear, ContextYear - 1, ContextYear)
VAR FinancialStartDate = DATE(YearFinancialYear, MonthStartFinancialYear, 1)

VAR MTD_Filter = DATESMTD('DIM_Date'[Date])
VAR LTM_Filter = DATESINPERIOD('DIM_Date'[Date], LastDateVisible, -12, MONTH)
VAR YTD_Filter = DATESBETWEEN('DIM_Date'[Date], FinancialStartDate, LastDateVisible)

VAR Result = 
    SWITCH(
        TRUE(),
        PeriodSelector = "MTD", CALCULATE(SUM('ArchiveData'[Value]), MTD_Filter),
        PeriodSelector = "YTD", CALCULATE(SUM('ArchiveData'[Value]), YTD_Filter),
        PeriodSelector = "LTM", CALCULATE(SUM('ArchiveData'[Value]), LTM_Filter)
    )

RETURN DIVIDE(Result, Units)

This version uses MAX('DIM_Date'[Date]) to pick up the latest visible date in the visual context and adjusts calculations accordingly. Since it doesn't filter to a specific year, it can be used in visuals that include multiple years without additional modification. Simply add your date hierarchy (e.g., Year and Month) to a matrix or line chart, and this measure will compute MTD, YTD, and LTM values for each year dynamically.

 

Best regards,

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.