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
arpost
Advocate V
Advocate V

DAX Help Needed: How do you group by in a measure and get the max of the group using DAX?

I have a situation where I have a table like this:

 

YearNameValue
2019Facility A10
2019Facility B20
2019Facility C15
2020Facility B5
2020Facility C1
2021Facility A2
2021Facility B-25
2021Facility C75
2018Facility A10
2018Facility B5
2018Facility C18

 

I want to make it so that a person can select "their location" and see the average yearly values and max values for other facilities (excluding their own) like so:

arpost_2-1645816568555.png

 

 

To do this, I need to come up with a measure that

  1. Filters out a specific name (i.e., "Facility B") that changes dynamically based on a filter (e.g., today I filter out Facility B, tomorrow Facility C)
  2. Groups by year
  3. Averages the values for that year
  4. Gives the max average.

Here is an example of what the output would be like in a table format (but isn't dynamic based on filters):

arpost_0-1645815969099.png

In this example, the MAX would be 38.5.

 

I thought I could do this in a measure using a variation on code like this, but I can't get to a specific column in the group:

arpost_1-1645816240755.png

 

Here's the DAX:

 

 

MaxNonSelectedFacility = 
var Value1 = GroupBY(Filter('Table','Table'[Name] <> SelectedValue(Facilities[Name]) ),'Table'[Year],"AverageVal",Average('Table'[Val]))

RETURN 

Max(Value1[AverageVal])

 

 

 

Anyone have ideas?

1 ACCEPTED SOLUTION

Sorry, try modifying it as follows:

Max Average for Others = 

VAR SelectedFacility = SELECTEDVALUE ( Facility[Name] )

VAR Result =
        MAXX (
            VALUES( 'Year'[Year] ),
            CALCULATE (
                AVERAGE ( ValueTable[Value] ),
                REMOVEFILTERS( Facility ),          
                NOT Facility[Name] = SelectedFacility
            )
        )

RETURN Result


Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

View solution in original post

5 REPLIES 5
arpost
Advocate V
Advocate V

@bcdobbs, works beautifully! Mank thanks.

I've just spent a good 20 minutes scratching my head wondering why my original measure didn't work. If you're interested the following correction sorts it out (Notice the extra calculate inside MAXX)

 

Max Average for Others Wrong = 

VAR SelectedFacility = SELECTEDVALUE ( Facility[Name] )

VAR Result =
    CALCULATE (
        MAXX (
            VALUES ('Year'[Year] ),
            CALCULATE ( AVERAGE ( ValueTable[Value] ) )
        ),
    REMOVEFILTERS( Facility ),
    NOT Facility[Name] = SelectedFacility
    )

RETURN Result

 

Without the extra CALCULATE inside the MAXX you don't get a context transition. So on the total row there is no year in the filter context and even though you're iterating over each year in the MAXX that year isn't being moved into the filter context. So it returns the average of all the years



Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!
bcdobbs
Super User
Super User

See if this works for you: Demo 

 

I created a Facility and a Year dimension table with the following DAX:

 

Facility = DISTINCT ( ValueTable[Name] )

Year = DISTINCT ( ValueTable[Year] )

 

And used these to form a star schema:

bcdobbs_0-1645821435578.png

 

Basic measure for the selected Facility:

Selected Average = AVERAGE ( ValueTable[Value] )

 

I then used MAXX to iterate over the years and calculate an average for each return the max. The CALCULATE allows me to reverse the selection:

 

Max Average for Others = 

VAR SelectedFacility = SELECTEDVALUE ( Facility[Name] )

VAR Result =
    CALCULATE (
        MAXX (
            VALUES ('Year'[Year] ),
            AVERAGE ( ValueTable[Value] )
        ),
    REMOVEFILTERS( Facility ),
    NOT Facility[Name] = SelectedFacility
    )

RETURN Result


Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

@bcdobbs, thanks for the reply! This is definitely close. The one issue I am running into is that this works when scoped to a given year but shows the "overall" average when not given a year context. In this image, you can see that the Avg. in the card is showing the average of the averages instead of 25, which is the max value for all the years.

 

arpost_1-1646062424381.png

 

Can this be modified to show the largest value?

Sorry, try modifying it as follows:

Max Average for Others = 

VAR SelectedFacility = SELECTEDVALUE ( Facility[Name] )

VAR Result =
        MAXX (
            VALUES( 'Year'[Year] ),
            CALCULATE (
                AVERAGE ( ValueTable[Value] ),
                REMOVEFILTERS( Facility ),          
                NOT Facility[Name] = SelectedFacility
            )
        )

RETURN Result


Ben Dobbs

LinkedIn | Twitter | Blog

Did I answer your question? Mark my post as a solution! This will help others on the forum!
Appreciate your Kudos!!

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.