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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
abanar2
Helper I
Helper I

Need help with my measure

Dear All,

 

I’m working with a Calendar table that includes the following columns:

DayDateYearQuerter_NumQuarter_TextYear-QuarterWeek_of_the_Year_NumYearWeek_TextMonth_NumStart_of_QuarterEnd_of_Quarter
3-Jan-1020101Q12010-Q112010- W111-Jan-1031-Mar-10
2-Jan-1020101Q12010-Q112010- W111-Jan-1031-Mar-10
1-Jan-1020101Q12010-Q112010- W111-Jan-1031-Mar-10
4-Jan-1020101Q12010-Q122010- W211-Jan-1031-Mar-10

 

I’m using the Year-Quarter column as a slicer on my report page.

 

There is an active relationship between the Calendar[DayDate] and Item_Exp[Date_closed] columns.

 

I want to create a dynamic line chart that shows the values of a measure ( _m_cnt_closed_item ) over the last 5 quarters, ending with the quarter selected in the slicer.

So, if the user selects 2023-Q4, the chart should display data from 2022-Q4 to 2023-Q4.

 

Currently, when I use the Year-Quarter slicer, the line chart only shows data for the selected quarter, and exclude the previous 4 quarters.

 

This is my measure:

_m_cnt_closed_last_5_quarter =
VAR MaxDate = MAX('99999_Calendar'[DayDate])
VAR MinDate = EOMONTH(MaxDate, -15)

RETURN
CALCULATE(
    [_m_cnt_closed_item],
    'Item_Exp'[Date_closed] > MinDate,
    'Item_Exp'[Date_closed] <= MaxDate,
    REMOVEFILTERS( 'Item_Exp'[Date_closed] )
)

How can I modify this measure (or the slicer logic) so that the line chart dynamically shows the last 5 quarters based on the selected quarter in the slicer?
1 ACCEPTED SOLUTION

Hello @abanar2 

 

Try this approch

Extract Selected Quarter Info

We'll extract selected year and quarter from the slicer selection.

VAR SelectedQuarterText = SELECTEDVALUE('99999_Calendar'[Year-Quarter])

VAR SelectedYear = VALUE(LEFT(SelectedQuarterText, 4))

VAR SelectedQtrNum = VALUE(RIGHT(SelectedQuarterText, 1))

 

 Generate Last 5 Quarters List

Use logic to generate year-quarter values of the last 5 quarters.

VAR Last5Quarters =

    ADDCOLUMNS(

        GENERATESERIES(0, 4, 1),

        "QtrOffset", [Value],

        "Year", SelectedYear - INT((SelectedQtrNum - [Value]) < 1),

        "Quarter", MOD(SelectedQtrNum - [Value] - 1 + 4, 4) + 1

    )

 

VAR Last5QuarterText =

    SELECTCOLUMNS(

        Last5Quarters,

        "Year-Quarter", FORMAT([Year], "0000") & "-Q" & [Quarter]

    )

 

Final Measure

_m_cnt_closed_last_5_quarter =

VAR SelectedQuarterText = SELECTEDVALUE('99999_Calendar'[Year-Quarter])

VAR SelectedYear = VALUE(LEFT(SelectedQuarterText, 4))

VAR SelectedQtrNum = VALUE(RIGHT(SelectedQuarterText, 1))

 

VAR Last5Quarters =

    ADDCOLUMNS(

        GENERATESERIES(0, 4, 1),

        "QtrOffset", [Value],

        "Year", SelectedYear - INT((SelectedQtrNum - [Value]) < 1),

        "Quarter", MOD(SelectedQtrNum - [Value] - 1 + 4, 4) + 1

    )

 

VAR Last5QuarterText =

    SELECTCOLUMNS(

        Last5Quarters,

        "Year-Quarter", FORMAT([Year], "0000") & "-Q" & [Quarter]

    )

 

RETURN

CALCULATE(

    [_m_cnt_closed_item],

    KEEPFILTERS(TREATAS(Last5QuarterText, '99999_Calendar'[Year-Quarter]))

Thanks

 Pankaj Namekar | LinkedIn

If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.

 

View solution in original post

6 REPLIES 6
v-sdhruv
Community Support
Community Support

Hi @abanar2 ,

Just wanted to check if you had the opportunity to review the suggestions provided and able to resolve the issue?
If you still have any questions, feel free to reach out.

Thank you

v-sdhruv
Community Support
Community Support

Hi @abanar2 ,

Just wanted to check if you had the opportunity to review the suggestions provided and able to resolve the issue?

v-sdhruv
Community Support
Community Support

Hi @abanar2 ,

Just wanted to check if you had the opportunity to review the suggestions provided?
Were you able to resolve the issue?

 

wardy912
Memorable Member
Memorable Member

Hi @abanar2 

 

 You need to create a new table for the slicer, do not relate it to the calendar

Slicer_YearQuarter = DISTINCT('99999_Calendar'[Year-Quarter])

 

Next, create a measure to get the max quarter selected

 

Selected_Quarter = 
MAX('Slicer_YearQuarter'[Year-Quarter])

 

Modify your measure as follows

 

_m_cnt_closed_last_5_quarter =
VAR SelectedQtr = MAX('Slicer_YearQuarter'[Year-Quarter])
VAR SelectedYear = VALUE(LEFT(SelectedQtr, 4))
VAR SelectedQtrNum = VALUE(RIGHT(SelectedQtr, 1))

-- Generate a list of last 5 quarters
VAR Last5Quarters =
    ADDCOLUMNS(
        GENERATESERIES(0, 4, 1),
        "Year", SelectedYear - INT((SelectedQtrNum - [Value]) < 1),
        "Quarter", MOD(SelectedQtrNum - [Value] - 1 + 4, 4) + 1
    )

-- Convert to Year-Quarter format
VAR Last5QtrText =
    SELECTCOLUMNS(
        Last5Quarters,
        "Year-Quarter", FORMAT([Year], "0000") & "-Q" & [Quarter]
    )

RETURN
CALCULATE(
    [_m_cnt_closed_item],
    TREATAS(Last5QtrText, '99999_Calendar'[Year-Quarter])
)

 

This setup ensures that the slicer only drives the logic of the measure, not the visual filter context, allowing the chart to show the last 5 quarters dynamically.

 

Please give a thumbs up and mark as solved if this helps, thanks!

Hello,

The Copilot recommended me the something similar, but I am not convinced about its performance.

May I ask if you have different ideas?

 

Hello @abanar2 

 

Try this approch

Extract Selected Quarter Info

We'll extract selected year and quarter from the slicer selection.

VAR SelectedQuarterText = SELECTEDVALUE('99999_Calendar'[Year-Quarter])

VAR SelectedYear = VALUE(LEFT(SelectedQuarterText, 4))

VAR SelectedQtrNum = VALUE(RIGHT(SelectedQuarterText, 1))

 

 Generate Last 5 Quarters List

Use logic to generate year-quarter values of the last 5 quarters.

VAR Last5Quarters =

    ADDCOLUMNS(

        GENERATESERIES(0, 4, 1),

        "QtrOffset", [Value],

        "Year", SelectedYear - INT((SelectedQtrNum - [Value]) < 1),

        "Quarter", MOD(SelectedQtrNum - [Value] - 1 + 4, 4) + 1

    )

 

VAR Last5QuarterText =

    SELECTCOLUMNS(

        Last5Quarters,

        "Year-Quarter", FORMAT([Year], "0000") & "-Q" & [Quarter]

    )

 

Final Measure

_m_cnt_closed_last_5_quarter =

VAR SelectedQuarterText = SELECTEDVALUE('99999_Calendar'[Year-Quarter])

VAR SelectedYear = VALUE(LEFT(SelectedQuarterText, 4))

VAR SelectedQtrNum = VALUE(RIGHT(SelectedQuarterText, 1))

 

VAR Last5Quarters =

    ADDCOLUMNS(

        GENERATESERIES(0, 4, 1),

        "QtrOffset", [Value],

        "Year", SelectedYear - INT((SelectedQtrNum - [Value]) < 1),

        "Quarter", MOD(SelectedQtrNum - [Value] - 1 + 4, 4) + 1

    )

 

VAR Last5QuarterText =

    SELECTCOLUMNS(

        Last5Quarters,

        "Year-Quarter", FORMAT([Year], "0000") & "-Q" & [Quarter]

    )

 

RETURN

CALCULATE(

    [_m_cnt_closed_item],

    KEEPFILTERS(TREATAS(Last5QuarterText, '99999_Calendar'[Year-Quarter]))

Thanks

 Pankaj Namekar | LinkedIn

If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.

 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors