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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
BI_Data_Analyst
Regular 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
v-heq-msft
Community Support
Community Support

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

1 REPLY 1
v-heq-msft
Community Support
Community Support

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

 



 

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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