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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Elisa_Costanza
Frequent Visitor

New Measure with Rolling 12 Month

 

Good afternoon everyone,

I need your support to calculate a DAX measure in a Power BI report.
I have to create a line chart in Power BI, where:

  1. On the X-axis I need to place the time interval;

  2. On the Y-axis I need to place my measure F100.

    F100 is defined by the following DAX measure:


    F100 (%) = VAR DefectCount = DISTINCTCOUNT(assunta_datamodel_fact[key_fail]) VAR VINCount = SUM(assunta_datamodel_vehicles_calendar[activity_rate]) RETURN DIVIDE(DefectCount, VINCount, BLANK())
     

    My calendar is structured as shown in the attached image. 

    Elisa_Costanza_0-1743784908462.png

     

    I need to create a measure that, when a specific Month/Year is selected in the filter, the trend dynamically adapts by considering the MAX date from the rolling 12-month filter.
    So, for example, if I select Month/Year = April 2024 in the filter, the chart should display a trend from April 2023 to April 2024.

    I tried the following DAX formula, but the filter doesn't seem to work correctly:


    F100 Rolling 12M (%) = VAR SelectedYM = MAX(cv_datamodel_calendar[YearMonthDate]) -- <-- new column of type Date VAR MinYM = EDATE(SelectedYM, -11) VAR DefectCount = CALCULATE( DISTINCTCOUNT(assunta_datamodel_fact[key_fail]), cv_datamodel_calendar[YearMonthDate] >= MinYM, cv_datamodel_calendar[YearMonthDate] <= SelectedYM ) VAR VINCount = CALCULATE( SUM(assunta_datamodel_vehicles_calendar[activity_rate]), cv_datamodel_calendar[YearMonthDate] >= MinYM, cv_datamodel_calendar[YearMonthDate] <= SelectedYM ) RETURN DIVIDE(DefectCount, VINCount, BLANK())
     

    What am I doing wrong? Can you help me? 

1 ACCEPTED SOLUTION
v-pagayam-msft
Community Support
Community Support

Hi @Elisa_Costanza ,
Thank you @rohit1991  and @rajendraongole1  for the helpful response!

Upon reviewing the provided information,I tried to recreate it locally with the sample data.

1.Create Disconnected Slicer Table using below:

Disconnected_Calendar =
SELECTCOLUMNS(
    Calendar,
    "YearMonthDate", [YearMonthDate]
)



2.Create the F100 Rolling 12M Measure using below:

F100 Rolling 12M (%) =
VAR CurrentYM = MAX(Calendar[YearMonthDate]) -- <- this now uses the X-axis value
VAR MinYM = EDATE(CurrentYM, -11)

VAR SelectedMaxYM = MAX(Disconnected_Calendar[YearMonthDate]) -- from slicer

RETURN
IF (
    CurrentYM <= SelectedMaxYM && CurrentYM >= MinYM,
    VAR DefectCount =
        CALCULATE(
            DISTINCTCOUNT(Fact[key_fail]),
            FILTER(
                ALL(Calendar),
                Calendar[YearMonthDate] >= EDATE(CurrentYM, -11) &&
                Calendar[YearMonthDate] <= CurrentYM
            )
        )

    VAR VINCount =
        CALCULATE(
            SUM(Fact[activity_rate]),
            FILTER(
                ALL(Calendar),
                Calendar[YearMonthDate] >= EDATE(CurrentYM, -11) &&
                Calendar[YearMonthDate] <= CurrentYM
            )
        )

    RETURN DIVIDE(DefectCount, VINCount, BLANK()),
    BLANK()
)

Please refer the attached sceenshot and the file for your reference.
vpagayammsft_0-1744372248813.png


If this solution meets your requiremnet,consider accept it as solution.

Thank you .

Regards,
Pallavi.

View solution in original post

9 REPLIES 9
v-pagayam-msft
Community Support
Community Support

Hi @Elisa_Costanza ,
I wanted to check in on your situation regarding the issue. Have you resolved it? If you have, please consider marking the reply that helped you or sharing your solution. It would be greatly appreciated by others in the community who may have the same question.
Thank you.

v-pagayam-msft
Community Support
Community Support

Hi @Elisa_Costanza ,
Thank you @rohit1991  and @rajendraongole1  for the helpful response!

Upon reviewing the provided information,I tried to recreate it locally with the sample data.

1.Create Disconnected Slicer Table using below:

Disconnected_Calendar =
SELECTCOLUMNS(
    Calendar,
    "YearMonthDate", [YearMonthDate]
)



2.Create the F100 Rolling 12M Measure using below:

F100 Rolling 12M (%) =
VAR CurrentYM = MAX(Calendar[YearMonthDate]) -- <- this now uses the X-axis value
VAR MinYM = EDATE(CurrentYM, -11)

VAR SelectedMaxYM = MAX(Disconnected_Calendar[YearMonthDate]) -- from slicer

RETURN
IF (
    CurrentYM <= SelectedMaxYM && CurrentYM >= MinYM,
    VAR DefectCount =
        CALCULATE(
            DISTINCTCOUNT(Fact[key_fail]),
            FILTER(
                ALL(Calendar),
                Calendar[YearMonthDate] >= EDATE(CurrentYM, -11) &&
                Calendar[YearMonthDate] <= CurrentYM
            )
        )

    VAR VINCount =
        CALCULATE(
            SUM(Fact[activity_rate]),
            FILTER(
                ALL(Calendar),
                Calendar[YearMonthDate] >= EDATE(CurrentYM, -11) &&
                Calendar[YearMonthDate] <= CurrentYM
            )
        )

    RETURN DIVIDE(DefectCount, VINCount, BLANK()),
    BLANK()
)

Please refer the attached sceenshot and the file for your reference.
vpagayammsft_0-1744372248813.png


If this solution meets your requiremnet,consider accept it as solution.

Thank you .

Regards,
Pallavi.

rohit1991
Super User
Super User

Hi @Elisa_Costanza ,
To fix your rolling 12-month measure, you need to make sure that the date filter is applied correctly using the FILTER function inside CALCULATE, and that any existing filter context on the date field is removed using ALL. This ensures that the measure always calculates the values over the correct rolling period, regardless of any slicer selections. Below is a revised DAX formula that should provide the correct dynamic behavior when filtering by Month/Year.

F100 Rolling 12M (%) =
VAR SelectedDate = MAX(cv_datamodel_calendar[DAY_DT])
VAR MinDate = EDATE(SelectedDate, -11)
VAR DefectCount =
    CALCULATE(
        DISTINCTCOUNT(assunta_datamodel_fact[key_fail]),
        FILTER(
            ALL(cv_datamodel_calendar),
            cv_datamodel_calendar[DAY_DT] >= MinDate &&
            cv_datamodel_calendar[DAY_DT] <= SelectedDate
        )
    )
VAR VINCount =
    CALCULATE(
        SUM(assunta_datamodel_vehicles_calendar[activity_rate]),
        FILTER(
            ALL(cv_datamodel_calendar),
            cv_datamodel_calendar[DAY_DT] >= MinDate &&
            cv_datamodel_calendar[DAY_DT] <= SelectedDate
        )
    )
RETURN
    DIVIDE(DefectCount, VINCount, BLANK())

Make sure that your cv_datamodel_calendar[DAY_DT] column is marked as a date type and that the table is set as a proper date table in Power BI. This revised measure should now correctly return the rolling 12-month trend for F100 based on the latest date selected in your report.

 

Passionate about leveraging data analytics to drive strategic decision-making and foster business growth.

Connect with me on LinkedIn: Rohit Kumar.

Thank you @rohit1991 for your feedback. But, using your measure I'm always at the same situation:

 

Elisa_Costanza_0-1743862228973.png

Choosing 2025-02 from the slicer, the trend line shows only one value. 

 

rajendraongole1
Super User
Super User

Hi @Elisa_Costanza  - i guess the filter context isn't correctly applied. can you try below measure:

 

F100 Rolling 12M (%) =
VAR SelectedYM = MAX(cv_datamodel_calendar[YearMonthDate])
VAR MinYM = EDATE(SelectedYM, -11)

VAR DefectCount =
CALCULATE(
DISTINCTCOUNT(assunta_datamodel_fact[key_fail]),
FILTER(
ALL(cv_datamodel_calendar),
cv_datamodel_calendar[YearMonthDate] >= MinYM &&
cv_datamodel_calendar[YearMonthDate] <= SelectedYM
)
)

VAR VINCount =
CALCULATE(
SUM(assunta_datamodel_vehicles_calendar[activity_rate]),
FILTER(
ALL(cv_datamodel_calendar),
cv_datamodel_calendar[YearMonthDate] >= MinYM &&
cv_datamodel_calendar[YearMonthDate] <= SelectedYM
)
)

RETURN
DIVIDE(DefectCount, VINCount, BLANK())

 

I hope this helps 





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

Proud to be a Super User!





Thank you @rajendraongole1 for your suggestion. But, when selecting a Month/Year from the slicer, the chart only reflects the single selected date rather than showing the full trend from Feb-24 to Feb-25: 

Elisa_Costanza_0-1743785954800.png

 

Hi @Elisa_Costanza  - Try modifying your measure to ignore the slicer selection on YearMonthDate while still considering the selected max Month/Year dynamically.

 

F100 Rolling 12M (%) =
VAR SelectedYM = MAX(cv_datamodel_calendar[YearMonthDate])
VAR MinYM = EDATE(SelectedYM, -11)

VAR DefectCount =
CALCULATE(
DISTINCTCOUNT(assunta_datamodel_fact[key_fail]),
FILTER(
ALL(cv_datamodel_calendar), -- Ignores slicer filter
cv_datamodel_calendar[YearMonthDate] >= MinYM &&
cv_datamodel_calendar[YearMonthDate] <= SelectedYM
)
)

VAR VINCount =
CALCULATE(
SUM(assunta_datamodel_vehicles_calendar[activity_rate]),
FILTER(
ALL(cv_datamodel_calendar), -- Ignores slicer filter
cv_datamodel_calendar[YearMonthDate] >= MinYM &&
cv_datamodel_calendar[YearMonthDate] <= SelectedYM
)
)

RETURN
DIVIDE(DefectCount, VINCount, BLANK())

 

Please check and let know





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

Proud to be a Super User!





Thank you @rajendraongole1  for your interest. However, this doesn't work correctly in my chart — I’m in confusing. Additionally, I calculated a measure called 'SelectedMaxDate' to check the max date selected in the slicer, and that seems to work

Elisa_Costanza_0-1743788331845.png

 

In addition, this is my model data with two table:

1. Fact_Table : assunta_datamodel_fact

2. Dim_Calendar : cv_datamodel_calendar 

Elisa_Costanza_1-1743788999167.png

 

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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

"); $(".slidesjs-pagination" ).prependTo(".pagination_sec"); $(".slidesjs-pagination" ).append("
"); $(".slidesjs-play.slidesjs-navigation").appendTo(".playpause_sec"); $(".slidesjs-stop.slidesjs-navigation").appendTo(".playpause_sec"); $(".slidesjs-pagination" ).append(""); $(".slidesjs-pagination" ).append(""); } catch(e){ } /* End: This code is added by iTalent as part of iTrack COMPL-455 */ $(".slidesjs-previous.slidesjs-navigation").attr('tabindex', '0'); $(".slidesjs-next.slidesjs-navigation").attr('tabindex', '0'); /* start: This code is added by iTalent as part of iTrack 1859082 */ $('.slidesjs-play.slidesjs-navigation').attr('id','playtitle'); $('.slidesjs-stop.slidesjs-navigation').attr('id','stoptitle'); $('.slidesjs-play.slidesjs-navigation').attr('role','tab'); $('.slidesjs-stop.slidesjs-navigation').attr('role','tab'); $('.slidesjs-play.slidesjs-navigation').attr('aria-describedby','tip1'); $('.slidesjs-stop.slidesjs-navigation').attr('aria-describedby','tip2'); /* End: This code is added by iTalent as part of iTrack 1859082 */ }); $(document).ready(function() { if($("#slides .item").length < 2 ) { /* Fixing Single Slide click issue (commented following code)*/ // $(".item").css("left","0px"); $(".item.slidesjs-slide").attr('style', 'left:0px !important'); $(".slidesjs-stop.slidesjs-navigation").trigger('click'); $(".slidesjs-previous").css("display", "none"); $(".slidesjs-next").css("display", "none"); } var items_length = $(".item.slidesjs-slide").length; $(".slidesjs-pagination-item > button").attr("aria-setsize",items_length); $(".slidesjs-next, .slidesjs-pagination-item button").attr("tabindex","-1"); $(".slidesjs-pagination-item button").attr("role", "tab"); $(".slidesjs-previous").attr("tabindex","-1"); $(".slidesjs-next").attr("aria-hidden","true"); $(".slidesjs-previous").attr("aria-hidden","true"); $(".slidesjs-next").attr("aria-label","Next"); $(".slidesjs-previous").attr("aria-label","Previous"); //$(".slidesjs-stop.slidesjs-navigation").attr("role","button"); //$(".slidesjs-play.slidesjs-navigation").attr("role","button"); $(".slidesjs-pagination").attr("role","tablist").attr("aria-busy","true"); $("li.slidesjs-pagination-item").attr("role","list"); $(".item.slidesjs-slide").attr("tabindex","-1"); $(".item.slidesjs-slide").attr("aria-label","item"); /*$(".slidesjs-stop.slidesjs-navigation").on('click', function() { var itemNumber = parseInt($('.slidesjs-pagination-item > a.active').attr('data-slidesjs-item')); $($('.item.slidesjs-slide')[itemNumber]).find('.c-call-to-action').attr('tabindex', '0'); });*/ $(".slidesjs-stop.slidesjs-navigation, .slidesjs-pagination-item > button").on('click keydown', function() { $.each($('.item.slidesjs-slide'),function(i,el){ $(el).find('.c-call-to-action').attr('tabindex', '-1'); }); var itemNumber = parseInt($('.slidesjs-pagination-item > button.active').attr('data-slidesjs-item')); $($('.item.slidesjs-slide')[itemNumber]).find('.c-call-to-action').attr('tabindex', '0'); }); $(".slidesjs-play.slidesjs-navigation").on('click', function() { $.each($('.item.slidesjs-slide'),function(i,el){ $(el).find('.c-call-to-action').attr('tabindex', '-1'); }); }); $(".slidesjs-pagination-item button").keyup(function(e){ var keyCode = e.keyCode || e.which; if (keyCode == 9) { e.preventDefault(); $(".slidesjs-stop.slidesjs-navigation").trigger('click').blur(); $("button.active").focus(); } }); $(".slidesjs-play").on("click",function (event) { if (event.handleObj.type === "click") { $(".slidesjs-stop").focus(); } else if(event.handleObj.type === "keydown"){ if (event.which === 13 && $(event.target).hasClass("slidesjs-play")) { $(".slidesjs-stop").focus(); } } }); $(".slidesjs-stop").on("click",function (event) { if (event.handleObj.type === "click") { $(".slidesjs-play").focus(); } else if(event.handleObj.type === "keydown"){ if (event.which === 13 && $(event.target).hasClass("slidesjs-stop")) { $(".slidesjs-play").focus(); } } }); $(".slidesjs-pagination-item").keydown(function(e){ switch (e.which){ case 37: //left arrow key $(".slidesjs-previous.slidesjs-navigation").trigger('click'); e.preventDefault(); break; case 39: //right arrow key $(".slidesjs-next.slidesjs-navigation").trigger('click'); e.preventDefault(); break; default: return; } $(".slidesjs-pagination-item button.active").focus(); }); }); // Start This code is added by iTalent as part of iTrack 1859082 $(document).ready(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); $("#tip2").attr("aria-hidden","true").addClass("hidden"); $(".slidesjs-stop.slidesjs-navigation, .slidesjs-play.slidesjs-navigation").attr('title', ''); $("a#playtitle").focus(function(){ $("#tip1").attr("aria-hidden","false").removeClass("hidden"); }); $("a#playtitle").mouseover(function(){ $("#tip1").attr("aria-hidden","false").removeClass("hidden"); }); $("a#playtitle").blur(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); }); $("a#playtitle").mouseleave(function(){ $("#tip1").attr("aria-hidden","true").addClass("hidden"); }); $("a#play").keydown(function(ev){ if (ev.which ==27) { $("#tip1").attr("aria-hidden","true").addClass("hidden"); ev.preventDefault(); return false; } }); $("a#stoptitle").focus(function(){ $("#tip2").attr("aria-hidden","false").removeClass("hidden"); }); $("a#stoptitle").mouseover(function(){ $("#tip2").attr("aria-hidden","false").removeClass("hidden"); }); $("a#stoptitle").blur(function(){ $("#tip2").attr("aria-hidden","true").addClass("hidden"); }); $("a#stoptitle").mouseleave(function(){ $("#tip2").attr("aria-hidden","true").addClass("hidden"); }); $("a#stoptitle").keydown(function(ev){ if (ev.which ==27) { $("#tip2").attr("aria-hidden","true").addClass("hidden"); ev.preventDefault(); return false; } }); }); // End This code is added by iTalent as part of iTrack 1859082
Top Solution Authors
Top Kudoed Authors