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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Zosy
Helper II
Helper II

MTD measure for the MAX year and month selected in slicer

Hi,

How can I dinamically calculate the month to date value for the last month selected on a slicer? At the moment I have it set for the current year only, but I would like to be able to calculate it based on the slicer selection, where there are different years.

MONTH TO DATE = CALCULATE(TOTALMTD(SUM('Orders'[Amount]),'Date'[Date]),FILTER('Date', 'Date'[Year]= YEAR(TODAY())))



I have a date table

Date = 

VAR StartDate = DATE ( YEAR ( MIN ('Orders'[Date]) ), 1,1)
VAR EndDate = DATE ( YEAR ( MAX ('Orders'[Date]) ), MONTH( MAX ('Orders'[Date])), DAY( MAX ('Orders'[Date])))

return
ADDCOLUMNS(CALENDAR(
        StartDate,
        EndDate), 

        "DateAsInteger", FORMAT ( [Date], "YYYYMMDD" ), 
        "Year", YEAR ( [Date] ), 
        "MonthNo", FORMAT ( [Date], "MM" ), 
        "YearMonthNo", FORMAT ( [Date], "YYYY/MM" ), 
        "YearMonth", FORMAT ( [Date], "YYYY/mmm" ), 
        "MonthShort", FORMAT ( [Date], "mmm" ), 
        "MonthLong", FORMAT ( [Date], "mmmm" ), 
        "WeekNo", WEEKDAY ( [Date] ), 
        "WeekDay", FORMAT ( [Date], "dddd" ), 
        "WeekDayShort", FORMAT ( [Date], "dddd" ), 
        "Quarter", "Q" & FORMAT ( [Date], "Q" ), 
        "YearQuarter", FORMAT ( [Date], "YYYY" ) & "/Q" & FORMAT ( [Date],"Q" )
)

 Thank you

1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @Zosy ,

 

You can adjust your MONTH TO DATE measure to calculate the value dynamically based on the last month selected in the slicer, regardless of the year. The key is to capture the most recent date from the slicer context and filter your calculation accordingly.

Here’s the revised measure:

MONTH TO DATE =
VAR LastSelectedDate = MAX('Date'[Date])
VAR LastSelectedMonth = MONTH(LastSelectedDate)
VAR LastSelectedYear = YEAR(LastSelectedDate)
RETURN
CALCULATE(
    TOTALMTD(
        SUM('Orders'[Amount]),
        'Date'[Date]
    ),
    'Date'[Year] = LastSelectedYear,
    'Date'[MonthNo] = LastSelectedMonth
)

This version calculates the month-to-date total for the latest selected month and year. The measure works by first identifying the most recent date selected in the slicer using MAX('Date'[Date]), then extracting the month and year from that date. The CALCULATE function applies the TOTALMTD calculation filtered to match the extracted year and month.

If your slicer setup is more complex and you want to ensure it handles all slicer filters properly, you can use ALLSELECTED instead of MAX to get the last selected date in a broader context:

MONTH TO DATE =
VAR LastSelectedDate = MAXX(ALLSELECTED('Date'), 'Date'[Date])
VAR LastSelectedMonth = MONTH(LastSelectedDate)
VAR LastSelectedYear = YEAR(LastSelectedDate)
RETURN
CALCULATE(
    TOTALMTD(
        SUM('Orders'[Amount]),
        'Date'[Date]
    ),
    'Date'[Year] = LastSelectedYear,
    'Date'[MonthNo] = LastSelectedMonth
)

This approach ensures that your measure dynamically adapts to the slicer selection and calculates the month-to-date value for the most recent month available in the slicer, regardless of the year.

 

Best regards,

View solution in original post

3 REPLIES 3
danextian
Super User
Super User

Hi @Zosy 

 

Wether you use DATESMTD or TOTALMTD, both return the MTD value of the max date in your calendar table. So if in the current context there is no specific month such as in the case of the grand total row, the MTD value will be that of the latest month-yr. Please see screenshot below and the attached sample pbix.

danextian_0-1736164421418.png

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.
DataNinja777
Super User
Super User

Hi @Zosy ,

 

You can adjust your MONTH TO DATE measure to calculate the value dynamically based on the last month selected in the slicer, regardless of the year. The key is to capture the most recent date from the slicer context and filter your calculation accordingly.

Here’s the revised measure:

MONTH TO DATE =
VAR LastSelectedDate = MAX('Date'[Date])
VAR LastSelectedMonth = MONTH(LastSelectedDate)
VAR LastSelectedYear = YEAR(LastSelectedDate)
RETURN
CALCULATE(
    TOTALMTD(
        SUM('Orders'[Amount]),
        'Date'[Date]
    ),
    'Date'[Year] = LastSelectedYear,
    'Date'[MonthNo] = LastSelectedMonth
)

This version calculates the month-to-date total for the latest selected month and year. The measure works by first identifying the most recent date selected in the slicer using MAX('Date'[Date]), then extracting the month and year from that date. The CALCULATE function applies the TOTALMTD calculation filtered to match the extracted year and month.

If your slicer setup is more complex and you want to ensure it handles all slicer filters properly, you can use ALLSELECTED instead of MAX to get the last selected date in a broader context:

MONTH TO DATE =
VAR LastSelectedDate = MAXX(ALLSELECTED('Date'), 'Date'[Date])
VAR LastSelectedMonth = MONTH(LastSelectedDate)
VAR LastSelectedYear = YEAR(LastSelectedDate)
RETURN
CALCULATE(
    TOTALMTD(
        SUM('Orders'[Amount]),
        'Date'[Date]
    ),
    'Date'[Year] = LastSelectedYear,
    'Date'[MonthNo] = LastSelectedMonth
)

This approach ensures that your measure dynamically adapts to the slicer selection and calculates the month-to-date value for the most recent month available in the slicer, regardless of the year.

 

Best regards,

Thank you for explaining it. It worked!

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.