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 September 15. Request your voucher.

Reply
DineshArivu
Helper I
Helper I

Need to create DAX for last 3 month visible in clustered column chart

Hi Experts,

 

I want to create a dax to display the last 3 months when we select any month from the slicer. if not selected then it will display all month data in clustered column chart.
Monthname is a slicer (MonthName = FORMAT([Date],"MMMM"))   //text type
Monthyear is a X axis in visual (
MonthYear = FORMAT([Date]"MMM-yyyy"))     //text type

 

DineshArivu_0-1757423920655.png

 

 

DineshArivu_1-1757423920665.png

 

Tried this measure but no luck:

Last3Months =
VAR HasMonthSelected =
    ISFILTERED ( 'CustomCalendar'[MonthName] )
VAR EndDate =
    CALCULATE ( MAX ( 'CustomCalendar'[Date] ), ALLSELECTED ( 'CustomCalendar' ) )
VAR SelectedDate =
    MAX ( 'CustomCalendar'[Date] )
VAR Result =
    SWITCH (
        TRUE (),
        HasMonthSelected
            && SelectedDate IN DATESINPERIOD ( 'CustomCalendar'[Date], EndDate, -3, MONTH ), 1,
        HasMonthSelected, 0,
        1
    )
RETURN
    Result
 

Please help me on this.

 

Thanks

DK

3 REPLIES 3
v-saisrao-msft
Community Support
Community Support

Hi @DineshArivu,

Have you had a chance to review the solution we shared by @Jihwan_Kim? If the issue persists, feel free to reply so we can help further.

 

Thank you.

 

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

Please check the below picture and the attached pbix file.

One of ways is to create calculation group, and then put the calculation item into the visual level filter pane.

 

Create calculation groups in Power BI - Power BI | Microsoft Learn

 

Jihwan_Kim_1-1757472667305.png

 

 

Jihwan_Kim_0-1757472634851.png

 

_cg_filter = 
VAR _slicerselect =
    MAX ( 'calendar slicer'[Month-Year sort] )
VAR _t =
    FILTER (
        ALL ( 'calendar'[Month-Year], 'calendar'[Month-Year sort] ),
        'calendar'[Month-Year sort] <= _slicerselect
    )
VAR _window =
    WINDOW ( 1, ABS, 3, ABS, _t, ORDERBY ( 'calendar'[Month-Year sort], DESC ) )
RETURN
    SWITCH (
        TRUE (),
        NOT ISFILTERED ( 'calendar slicer'[Month name] ), SELECTEDMEASURE (),
        ISFILTERED ( 'calendar slicer'[Month name] ), CALCULATE ( SELECTEDMEASURE (), KEEPFILTERS ( _window ) ),
        SELECTEDMEASURE ()
    )

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

@Jihwan_Kim  thanks for your solution.
few points from my side:
1. We are not using any columns directly like sales,values for the visual X axis (all 5 measures used)

2. as we are using measures, there is no relationship created.
3. I cannot able to apply your measure into visual filter , like the filtering type is different (is,not equals,is blank,no blank).

If i use the same dax as calculated column, then it prompts error 

DineshArivu_0-1757506060820.png

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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