Forum Discussion

ar_46's avatar
ar_46
Regular Visitor
8 years ago
Solved

Aggregate by more than one column with added filter

  I have a data set like this with 5 columns:   Group,  Color, Serial Number, Month, count     I have to find the SUM and AVERAGE , grouped by Group and Color :  Group A - Color Group A-...
  • ar_46's avatar
    ar_46
    8 years ago

    HI v-xjiin-msft,

    Thank you for trying to help me. I was finally able to resolve my issue as specified below but can you please help optimize the same ?

     

    Basically, my problem was that I have 3 filters with a group by on two of them. 

    I was having a hard time making the Group By filters work with the 3rd filter while computing average.

     

    The problem occurs when I try to print to print the average at the row level.

     

    Grouping by : Model Group and Print Mode

    Another filter is : Month/Year

     

    I was able to resolve this using this using the below DAX measure. 

     

    AvgValue= AVERAGEX(ALLSELECTED(TestData),CALCULATE(AVERAGE([Print count]),GROUPBY(TestData,TestData[Model Group],TestData[Print Mode])))

    Any help in ptimizing would be very helpful.

     

     

     

  • v-xjiin-msft's avatar
    v-xjiin-msft
    8 years ago

    Hi ar_46,

     

    I'm glad to hear that you have resolved your issue. And your solution is great. 

     

    Then here's another method to get the average value without using GROUPBY(). It is hard to say which one is better, you can just make a reference.

     

    AvgValue without GroupBy =
    CALCULATE (
        AVERAGE ( TestData[Print Count] ),
        FILTER (
            ALLSELECTED ( TestData ),
            TestData[Model Group] = MAX ( TestData[Model Group] )
                && TestData[Print Mode] = MAX ( TestData[Print Mode] )
        )
    )

     

    Thanks,
    Xi Jin.