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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
avidthinker
Frequent Visitor

POWER BI DAX Query for DISTINCT ROWS in A GROUP to get the MAX of a GROUP where the key is not

Wondering if anyone can assist

 

 

Have a table with values of keys, count and value 

avidthinker_1-1662461606487.png

 

Need to group by Key and determine the distinct count and get the max value for each

avidthinker_0-1662461488136.png

Once done, except the the highest Max and if 0, determine the average of all values above based on the Key

 

Wondering how to come up with a DAX Query for this in POWER BI

2 REPLIES 2
v-kkf-msft
Community Support
Community Support

Hi @avidthinker ,

 

Please try the measure.

 

Measure = 
VAR maxvalue =
    CALCULATE ( MAX ( 'Table'[Value] ), ALLEXCEPT ( 'Table', 'Table'[Key] ) )
VAR maxvalue_count =
    CALCULATE ( MIN ( 'Table'[Count] ), ALL ( 'Table' ), 'Table'[Value] = maxvalue )
RETURN
    CALCULATE (
        AVERAGE ( 'Table'[Value] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Key] = MAX ( 'Table'[Key] )
                && 'Table'[Value] <> 0
                && 'Table'[Count] <> maxvalue_count
        )
    )

vkkfmsft_0-1662517598381.png

 

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

Hi v-kkf-msft,

 

I tried your calculations, but does not seem quite right.

 

To simplify,this is what is required, ignore Count 0 and Value 0 and only consider for now AAA once correct, need to work out for original with multiple Key Values.

 

KeyCountValue
AAA00
AAA11000
AAA11100
AAA11200
AAA22000
AAA22100
AAA22200
AAA33000
AAA33100

 

For AAA, the count is 1,2 values max 1200,2200

max of 2, max total of 3400

average will be 3400/2=1700

 

Hope this helps

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.