DAX, Calculating Average of a measure within a hierarchy

87 Views Asked by At

I need to solve an issue that I am having with DAX, but I cant find the right query. To give you some context: I have different regions in my data that have the values T100, T200, T300 and so on. They can be found in the column RegionLevel2 in the table Geography. Then , in the same table there is a column called TerrLevel1 that has the territories. Each region has multiple territories. So for example the territories for region T100, are T101, T102, T103 and so on. For each of these territories I have calculated a Score, based on their individual sales. It is a measure called Score1 and working fine. Now I need to calculate the score for the regions. This score is based on the average of the sales of a regions territories. So in the case of T100, its score would be the average of the Score1 of T101, T102, T103 and so on. I have tried these DAX formulas :

AVERAGEX(Geography, [Score1])

AVERAGEX(all(Geography[TerrLevel1]), [Score1])

AVERAGEX(VALUES(Geography[TerrLevel1]), [Score1])

CALCULATE( AVERAGEX(Geography, [Score1]),ALL(Geography[TerrLevel1]))

but it is not giving me the right result. I guess i need to filter somehow to group the right territories with their region but I cant figure out how.

I would appreciate any kind of help. Thank!

For Promethee: first column are the territories (they are as G0101 and such instead of T0101 as described above), second table is the score of each territory (the measure that should be used for the average) and last column is the result I get from your query. temporay result

1

There are 1 best solutions below

11
Promethee On

If you have no territory context, your original measure (that I'll name "Avg") should work. Now assuming you have a context with a territory:

  • you have to cancel the territory context with ALL;
  • then this could depend on whether or not you have an explicit region context:

If region context is explicit, this should be sufficient:

=IF(HASONEVALUE(Geography[RegionLevel2]),
    CALCULATE([Avg],
              ALL(Geography[TerrLevel1])),
    BLANK())

but I suggest you to consider that region context might not always be explicit and thus use the following (safer) measure:

=IF(HASONEVALUE(Geography[RegionLevel2]),
    CALCULATE([Avg],
              Geography[RegionLevel2] = VALUES(Geography[RegionLevel2]),
              ALL(Geography[TerrLevel1])),
    BLANK())

The disadvantage of this measure is that you'll get no value (blank) if several regions belong to the context (typ. in totals or subtotals) but I don't know your exact needs.

For Promethee: first column are the territories (they are as G0101 and such instead of T0101 as described above), second column is the score of each territory (the measure that should be used for the average) and last column is the result I get from your query. temporary result