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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Calculating the average for the top 25% within region, brand and month

Hi all, 

 

I am very new to DAX/PowerBI so any and all help is welcomed. This is also my first post so apologies in advance if I am not able to explain my problem in a clear manner. 

 

I have a table with 4 columns: Brand, Region, Month and Sales. I want to calculate the average of the top 25% of sales and use it as a benchmark. However, I need to calculate this exclusively within region and brand and during the specific month.

 

I found this similar post except it is not filtering by multiple categories: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Calculate-Average-of-scores-within-top-percen....

 

Currently, I am creating a column to calculate the rank of each row with RANKX like this: 

 

P1FR_toprank = RANKX(
ALLEXCEPT(Table,Table[Region],Table[Month],Table[Brand]),
Parity[Sales], ,ASC)
but I get the error: A circular dependency was detected: Parity[P1FR_toprank].</pi>
 
MY next step is calculating the average for each top25% using something similar to this solution found at https://www.burningsuit.co.uk/find-top-or-bottom-n-percent-dax/
 
Top 10 Percent =
VAR Rank_To_Find = COUNTROWS ( ALL ( Customers) ) * 0.1
RETURN
CALCULATE ( [_Total Sales] ,
FILTER ( Customers,Customers[Customers Ranked Top]<= Rank_To_Find))
 
Thanks in advance!
1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

I will get same error when I create a rankd calcualted column by ALLEXCEPT function.

RicoZhou_0-1651036731740.png

You can try this code to create a rank column.

P1FR_toprank = 
RANKX (
    FILTER (
        'Table',
        'Table'[Region] = EARLIER ( 'Table'[Region] )
            && 'Table'[Brand] = EARLIER ( 'Table'[Brand] )
            && 'Table'[Month] = EARLIER ( 'Table'[Month] )
    ),
    'Table'[Sales],
    ,
    DESC,
    DENSE
)

Then create a measure.

Top 25% Average by Region, Brand and Month =
VAR _CountRow =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        ALLEXCEPT ( 'Table', 'Table'[Region], 'Table'[Brand], 'Table'[Month] )
    )
VAR _FIND_RANK = _CountRow * 0.25
RETURN
    CALCULATE (
        AVERAGE ( 'Table'[Sales] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Region], 'Table'[Brand], 'Table'[Month] ),
            'Table'[P1FR_toprank] <= _FIND_RANK
        )
    )

 

Best Regards,
Rico Zhou

 

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

4 REPLIES 4
vojtechsima
Memorable Member
Memorable Member

Hi, @Anonymous 
This is a kinda cool question, can you please give me sample data so I can test my hypothesis?
Please share all the tables you will use with the same structure but random numbers (copyable so I can paste it to my Power BI model).
Thanks

Anonymous
Not applicable

Here are 50 lines of data (sorry is big but I needed to make sure the combinations were there). I also realized there is one more layer of complexity. I need to not take in considerations the lines that show 0 sales. I am still trying to figure it out an I will post any updated code I generate. 

 

Thanks!

 

RegionBrandMonthSales
NAMB2021/1181.03
NAMB2021/0394.16
BRAD2021/03100
NAMB2021/1141.03
NAMB2021/1085.56
ANDB2021/0688.89
NAMB2021/1167.44
NAMB2021/0394.12
ANDB2020/1293.55
NAMB2020/0795.52
NAMB2021/0590.12
NAMB2021/0285
NAMB2021/0286.31
ANDB2020/07100
BRAD2020/1282.61
BRAD2020/11100
NAMB2020/0591.09
ANDB2021/08100
BRAB2020/0997.62
BRAD2021/0997.44
NAMB2021/0892.51
MEXB2021/08100
NAMB2020/0693.42
NAMD2021/120
ANDB2020/10100
BRAD2021/03100
MEXB2021/11100
BRAB2022/030
NAMB2021/0194.12
NAMB2021/0391.54
BRAD2022/020
ANDB2022/030
NAMB2021/0396.97
MEXB2021/0198.51
ANDB2021/1228.57
BRAD2021/06100
NAMB2022/010
BRAD2022/030
BRAB2021/1088.24
NAMD2020/090
ANDB2021/1266.67
BRAB2022/020
NAMB2020/120
NAMD2022/030
ANDB2021/08100
BRAD2020/070
NAMB2020/040
MEXB2021/1240
BRAB2020/0876.92
BRAB2020/110

Hi @Anonymous ,

 

I will get same error when I create a rankd calcualted column by ALLEXCEPT function.

RicoZhou_0-1651036731740.png

You can try this code to create a rank column.

P1FR_toprank = 
RANKX (
    FILTER (
        'Table',
        'Table'[Region] = EARLIER ( 'Table'[Region] )
            && 'Table'[Brand] = EARLIER ( 'Table'[Brand] )
            && 'Table'[Month] = EARLIER ( 'Table'[Month] )
    ),
    'Table'[Sales],
    ,
    DESC,
    DENSE
)

Then create a measure.

Top 25% Average by Region, Brand and Month =
VAR _CountRow =
    CALCULATE (
        COUNTROWS ( 'Table' ),
        ALLEXCEPT ( 'Table', 'Table'[Region], 'Table'[Brand], 'Table'[Month] )
    )
VAR _FIND_RANK = _CountRow * 0.25
RETURN
    CALCULATE (
        AVERAGE ( 'Table'[Sales] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Region], 'Table'[Brand], 'Table'[Month] ),
            'Table'[P1FR_toprank] <= _FIND_RANK
        )
    )

 

Best Regards,
Rico Zhou

 

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

Anonymous
Not applicable

Thanks Rico, it works!! I used the same code to calculate the bottom 25% by using ASC instead of DESC on the rank function.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.