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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
HassanAshas
Helper V
Helper V

How to get the Maximum Value for a Month from the Selected Months

Hi, I am trying to label the "highest" value of the month from all the Months selected in my Table/Bar Chart. 

To do this, I wrote this DAX code, 

 

Overall Max Value Month = 
var _highest = 
    MAXX( 
    ALLSELECTED(
        Calender[Month]), 
        [TotalVisitors] 
    )

 Return 
 _highest 

 

As far as my understanding is, this code should work and should return the highest value from all the selected months. But for some reason, it is not working. 

 

and to my surprise, this same code does work if I apply it to Year or to Quarter. For example, following code works for me, 

 

Overall Max Value Quarter = 
var _highest = 
    MAXX( 
    ALLSELECTED(
        Calender[Quarter]), 
        [TotalVisitors] 
    )

 Return 
 _highest 

 

But the same thing doesn't work for Month or for Month-Year. 

As an example, please check the below results I am getting with these Measures, 

 

HassanAshas_0-1690138071761.png

 

Can anyone help me out in figuring what is the problem here?

 

If you would like to download the Sample Power BI File, you may do so from here: https://drive.google.com/file/d/13WsVDSb-2zD1WJIedP-Bx9Rzh6tOolW3/view?usp=sharing

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @HassanAshas 

The unexpected results are due to "sort by" columns defined for certain columns, in this case Month and Month Year.

 

When a sort by column is defined for a particular column, it is automatically included as a group-by column within the DAX query generated by Power BI visuals.

 

For example, in your visual grouped by 'Calender'[Year Month], the DAX query includes both 'Calender'[Year Month] and 'Calender'[Year Month Number].

 

Because of this, I would recommend this pattern as the safest way to write these measures:

Overall Max Value Month = 
CALCULATE (
        MAXX ( 
            VALUES ( Calender[Month] ),
            [TotalVisitors]
        ),
        ALLSELECTED ( Calender )
    )

Overall Max Value Quarter = 
CALCULATE (
        MAXX ( 
            VALUES ( Calender[Quarter] ),
            [TotalVisitors]
        ),
        ALLSELECTED ( Calender )
    )

// etc.

In this pattern, ALLSELECTED ( Calender ) ensures that the "overall" filter context for all columns of the Calender table is restored, which will include the sort-by columns.

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi @HassanAshas 

The unexpected results are due to "sort by" columns defined for certain columns, in this case Month and Month Year.

 

When a sort by column is defined for a particular column, it is automatically included as a group-by column within the DAX query generated by Power BI visuals.

 

For example, in your visual grouped by 'Calender'[Year Month], the DAX query includes both 'Calender'[Year Month] and 'Calender'[Year Month Number].

 

Because of this, I would recommend this pattern as the safest way to write these measures:

Overall Max Value Month = 
CALCULATE (
        MAXX ( 
            VALUES ( Calender[Month] ),
            [TotalVisitors]
        ),
        ALLSELECTED ( Calender )
    )

Overall Max Value Quarter = 
CALCULATE (
        MAXX ( 
            VALUES ( Calender[Quarter] ),
            [TotalVisitors]
        ),
        ALLSELECTED ( Calender )
    )

// etc.

In this pattern, ALLSELECTED ( Calender ) ensures that the "overall" filter context for all columns of the Calender table is restored, which will include the sort-by columns.

 

Regards


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Thank you so much for the clarification! This worked like a charm! 

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.