Forum Discussion

avidthinker's avatar
avidthinker
Frequent Visitor
4 years ago

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 

 

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

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

  • v-kkf-msft's avatar
    v-kkf-msft
    Icon for Community Support rankCommunity 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
            )
        )

     

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

    • avidthinker's avatar
      avidthinker
      Frequent Visitor

      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