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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
felipereyes_
Frequent Visitor

Calculating Average of categories in DAX

Hi everyone,

 

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 

AVERAGEX(Geography[Score1])

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

IF(HASONEVALUE(Geography[RegionLevel2]), CALCULATE(AVERAGEX(VALUES(Geography[TerrLevel1]), [Score1]), Geography[RegionLevel2] = VALUES(Geography[RegionLevel2]), ALL(Geography[TerrLevel1])), BLANK())
 
    SUMMARIZE(
    Geography,
[RegionLevel2],
    Geography[TerrLevel1],
        "MEASURE",[Score1])
return
 AVERAGEX(table_,  [MEASURE])

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. Also the measure Score1 is based on several other measures that have filters like Geography[Region_LINE] = "T" (its to filter for the regions that start with T only) and CALCULATE([Product2 40%], REMOVEFILTERS(GEOGRAPHY[DES_TERRNIVEL1_FILTER], GEOGRAPHY[DES_TERRNIVEL2_FILTER]), REMOVEFILTERS(DIM_GEO_NO_RLS[DES_GEONIVEL3_FILTER], DIM_GEO_NO_RLS[DES_GEONIVEL4_FILTER]) . Maybe I have to include them? I could not find a way to make it work though. 
 
I would appreciate any kind of help. Thank!
1 ACCEPTED SOLUTION
v-heq-msft
Community Support
Community Support

Hi @felipereyes_ ,
Based on your description, I created the following test data:

vheqmsft_0-1710826425325.png

Create a measure

RegionScore = 
CALCULATE(
    AVERAGE(Geography[Sale]),
    ALLEXCEPT(Geography,Geography[Region])
)

Final output

vheqmsft_1-1710826476298.png

You can try the above test as I can't tell what your other filters are, and you can follow the above logic to write the code that works for you.

Best regards,

Albert He

 

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

 

View solution in original post

1 REPLY 1
v-heq-msft
Community Support
Community Support

Hi @felipereyes_ ,
Based on your description, I created the following test data:

vheqmsft_0-1710826425325.png

Create a measure

RegionScore = 
CALCULATE(
    AVERAGE(Geography[Sale]),
    ALLEXCEPT(Geography,Geography[Region])
)

Final output

vheqmsft_1-1710826476298.png

You can try the above test as I can't tell what your other filters are, and you can follow the above logic to write the code that works for you.

Best regards,

Albert He

 

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

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.