Combining COUNTA() and AVERAGEX() in DAX / Power BI

297 views Asked by At

I have a simple data set with training sessions for some athletes. Let's say I want to visualize how many training sessions are done as an average of the number of athletes, either in total or divided by the clubs that exist. I hope the data set is somewhat self-describing.

Input data

To norm the number of activities by the number of athletes I use two measures:

TotalSessions = COUNTA(Tab_Sessions[Session key])

AvgAthlete = AVERAGEX(VALUES(Tab_Sessions[Athlete]),[TotalSessions])

I give AvgAthlete as the desired value in both visuals shown below. If I make a filter on the clubs the values are as expected, but with no filter applied I get some strange values

Visuals in Power BI

What I guess happens is that since Athlete B doesn't do any strength, Athlete B is not included in the norming factor for strength. Is there a DAX function that can solve this? If I didn't have the training sessions as a hierarchy (Type-Intensity), it would be pretty straightforward to do some kind of workaround with a calculated column, but it won't work with hierarchical categories. The expected results calculated in excel are shown below: Ground truth excel

Data set as csv:

Session key;Club;Athlete;Type;Intensity
001;Fast runners;A;Cardio;High
002;Fast runners;A;Strength;Low
003;Fast runners;B;Cardio;Low
004;Fast runners;B;Cardio;High
005;Fast runners;B;Cardio;High
006;Brutal boxers;C;Cardio;High
007;Brutal boxers;C;Strength;High
1

There are 1 answers

0
Marcus On

If you specifically want to aggregate this across whatever choice you have made in your Club selection, then you simply write out a simple measure that does that:

AvgAthlete = 
VAR _athletes = 
    CALCULATE ( 
        DISTINCTCOUNT ( 'Table'[Athlete] ) , 
        ALLEXCEPT ( 'Table' , 'Table'[Club] )
    )
RETURN
DIVIDE ( 
    [Sessions] , 
    _athletes
)

Here we use a distinct count of values in the Athlete column, with all filters removed apart from on the Club column. This is, as far as I interpret your question, the denominator you are after.

Divide the total number of sessions on this number of athletes. Here is the result:

enter image description here