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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
BI_Data_Analyst
Frequent Visitor

Get last 4 months period of data on a bar chart from the selected value in a filter

Hello Folks,

I need your urgent help on the dax where I get to see last 4 Period (months) of data in a bar chart

 

For example.

If I choose "Football" from the "Game" slicer then I get to see data from February 2024 to May 2024.

 

BI_Data_Analyst_0-1714564653167.png

 

But If I choose "Snooker" from the Game slicer  I get to see only March 2024 & May 2024 Data

BI_Data_Analyst_2-1714564832184.png

 

"I would like to get exactly like explained below"

 

Whereas, when I choose "snooker" from the Game Slicer I would like to see July 2023, August 2023, March 2024 & May 2024 data becuase there is no data in the month of February 2024 and April 2024.

 

BI_Data_Analyst_3-1714564915151.png

 


The DAX measure that I have used is this:

%_Yes_4_Months =
VAR _maxdate = MAX('Calendar'[Date])
VAR _lastperiods = DATESINPERIOD(New_Calendar[Date],
_maxdate, -4, month)
Return
calculate(
    [%_Yes],
    REMOVEFILTERS('Calendar'[Date]),
    KEEPFILTERS(_lastperiods),
    USERELATIONSHIP('Calendar'[Date], New_Calendar[Date]))



Please urgently help me on getting this right.

 

 

Here is the link to access the pbix file and dataset

 

 

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @BI_Data_Analyst ,
Here some steps that I want to share, you can check them if they suitable for your requirement.
Here is my test data:

vheqmsft_0-1714638039034.png

Create  COLUMN in power query

=Date.ToText([Date],[Format = "yyyyMM"])

vheqmsft_1-1714638121030.png

Create a calculated column

Rank = 
RANKX(
    CALCULATETABLE(
        'Table',
        ALLEXCEPT(
            'Table',
            'Table'[Game]
        )
    ),
    [Custom],,
    ASC,
    Dense
)

Create measures

Measure = 
VAR _maxRank = 
CALCULATE(
    MAX('Table'[Rank]),
    ALLEXCEPT('Table','Table'[Game])
)
RETURN
IF(
    SELECTEDVALUE('Table'[Rank]) > _maxRank - 4,
    1,
    0
)
Divide = 
DIVIDE(
    CALCULATE(
        COUNTROWS('Table'),
        FILTER(
            'Table',
            'Table'[Response] = "Yes"
        )
    ),
    COUNTROWS('Table')
)

Use measure as filter on this visual

Fianl output

vheqmsft_2-1714638240744.png

vheqmsft_3-1714638255816.png

Best regards,

Albert He

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 



 

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous ,

Can you please help me with the above workaround as explained by @BI_Data_Analyst 
Its really urgent for me.

 

Thanks in advannce! 

 

 

Kind regards,

Ash

Anonymous
Not applicable

Hi @BI_Data_Analyst ,
Here some steps that I want to share, you can check them if they suitable for your requirement.
Here is my test data:

vheqmsft_0-1714638039034.png

Create  COLUMN in power query

=Date.ToText([Date],[Format = "yyyyMM"])

vheqmsft_1-1714638121030.png

Create a calculated column

Rank = 
RANKX(
    CALCULATETABLE(
        'Table',
        ALLEXCEPT(
            'Table',
            'Table'[Game]
        )
    ),
    [Custom],,
    ASC,
    Dense
)

Create measures

Measure = 
VAR _maxRank = 
CALCULATE(
    MAX('Table'[Rank]),
    ALLEXCEPT('Table','Table'[Game])
)
RETURN
IF(
    SELECTEDVALUE('Table'[Rank]) > _maxRank - 4,
    1,
    0
)
Divide = 
DIVIDE(
    CALCULATE(
        COUNTROWS('Table'),
        FILTER(
            'Table',
            'Table'[Response] = "Yes"
        )
    ),
    COUNTROWS('Table')
)

Use measure as filter on this visual

Fianl output

vheqmsft_2-1714638240744.png

vheqmsft_3-1714638255816.png

Best regards,

Albert He

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 



 

 

Hi @Anonymous ,

Thank you so much for your support.

(1)Just wondering if we can select "Ireland" from "Game Origin" filter and yet we are able to see Baseball Chart.
(Because Baseball game is a part of Ireland)

(2) and when nothing is selected from any of the filters we get last 4 months "Divide" of the overall data.

BI_Data_Analyst_0-1716186006080.png

Kind regards,

Ash.



Anonymous
Not applicable

Hi @v-heq-msft ,

Can you please help me with the above workaround as explained by @BI_Data_Analyst 
Its really urgent for me.

 

Thanks in advannce! 

 

 

Kind regards,

Ash

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