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
badger123
Resolver I
Resolver I

Rank, count rank and average

I have a table that looks something like this:

CountryCategoryTermBrandValue
UKcategory acategory a term one brand onebrand one20
UKcategory acategory a term two brand twobrand two10
UKcategory acategory a term threeN/A30
UKcategory bcategory b term one brand onebrand one20
UKcategory bcategory b term two brand threebrand three100
UKcategory ccategory c term one brand onebrand one10
UKcategory ccategory c term two brand twobrand two20
UKcategory ccategory c term three brand threebrand three30
UKcategory ccategory c term four brand onebrand one30

 

And I'm trying to figure out, for each brand in each country, how many categories it ranks within the top 2 and how many it ranks as the top 1, based on the sum of values for a given category. So in this case, it would be:

CountryBrandRanks in top 2 for X categoriesRanks in top 1 for X categories
UKbrand one32
UKbrand two10
UKbrand three21

 

What I then want to do is calculate the average of all brands ranking in the top 2 (so 3+1+2 / 3 = 2) and the average of all brands ranking as the top 1 (2+0+1 / 3 = 1).

 

Please help! I just cannot figure this out. 

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @badger123 

Try this:

1. Place Table1[Country] and Table1[Brand] in a table visual

2. Create these two measures and place them in the table visual:

Measure1 = 
SUMX (
    ADDCOLUMNS (
        DISTINCT ( Table1[Category] );
        "Ranks_in_Top1"; 1
            * (
                COUNTROWS (
                    FILTER (
                        TOPN (
                            1;
                            CALCULATETABLE (
                                SUMMARIZE ( Table1; Table1[Country]; Table1[Category]; Table1[Brand] );
                                ALL ( Table1[Brand] )
                            );
                            CALCULATE ( SUM ( Table1[Value] ) ); DESC
                        );
                        Table1[Brand] = SELECTEDVALUE ( Table1[Brand] )
                    )
                ) > 0
            )
    );
    [Ranks_in_Top1]
)

 

 

Measure2 =
SUMX (
    ADDCOLUMNS (
        DISTINCT ( Table1[Category] );
        "Ranks_in_Top2"; 1
            * (
                COUNTROWS (
                    FILTER (
                        TOPN (
                            2;
                            CALCULATETABLE (
                                SUMMARIZE ( Table1; Table1[Country]; Table1[Category]; Table1[Brand] );
                                ALL ( Table1[Brand] )
                            );
                            CALCULATE ( SUM ( Table1[Value] ) ); DESC
                        );
                        Table1[Brand] = SELECTEDVALUE ( Table1[Brand] )
                    )
                ) > 0
            )
    );
    [Ranks_in_Top2]
)

Take into account that this will show "N/A" as brand (you haven't in your example) and it sums all items of a brand. For instance, under category C, there are two entries for 'brand one'. The code above considers the 30+10 as value for 'brand one'.  

 

View solution in original post

5 REPLIES 5
AlB
Super User
Super User

Hi @badger123 

Try this:

1. Place Table1[Country] and Table1[Brand] in a table visual

2. Create these two measures and place them in the table visual:

Measure1 = 
SUMX (
    ADDCOLUMNS (
        DISTINCT ( Table1[Category] );
        "Ranks_in_Top1"; 1
            * (
                COUNTROWS (
                    FILTER (
                        TOPN (
                            1;
                            CALCULATETABLE (
                                SUMMARIZE ( Table1; Table1[Country]; Table1[Category]; Table1[Brand] );
                                ALL ( Table1[Brand] )
                            );
                            CALCULATE ( SUM ( Table1[Value] ) ); DESC
                        );
                        Table1[Brand] = SELECTEDVALUE ( Table1[Brand] )
                    )
                ) > 0
            )
    );
    [Ranks_in_Top1]
)

 

 

Measure2 =
SUMX (
    ADDCOLUMNS (
        DISTINCT ( Table1[Category] );
        "Ranks_in_Top2"; 1
            * (
                COUNTROWS (
                    FILTER (
                        TOPN (
                            2;
                            CALCULATETABLE (
                                SUMMARIZE ( Table1; Table1[Country]; Table1[Category]; Table1[Brand] );
                                ALL ( Table1[Brand] )
                            );
                            CALCULATE ( SUM ( Table1[Value] ) ); DESC
                        );
                        Table1[Brand] = SELECTEDVALUE ( Table1[Brand] )
                    )
                ) > 0
            )
    );
    [Ranks_in_Top2]
)

Take into account that this will show "N/A" as brand (you haven't in your example) and it sums all items of a brand. For instance, under category C, there are two entries for 'brand one'. The code above considers the 30+10 as value for 'brand one'.  

 

Thanks @AlB  ! That solves my first problem 🙂

Do you know how to solve the second part?

 

What I then want to do is calculate the average of all brands ranking in the top 2 (so 3+1+2 / 3 = 2) and the average of all brands ranking as the top 1 (2+0+1 / 3 = 1).

You can do this:

Measure2_Total =
AVERAGEX (
    ADDCOLUMNS (
        SUMMARIZE ( Table1; Table1[Country]; Table1[Brand] );
        "Aux"; CALCULATE (
            SUMX (
                ADDCOLUMNS (
                    DISTINCT ( Table1[Category] );
                    "Ranks_in_Top2"; 1
                        * (
                            COUNTROWS (
                                FILTER (
                                    TOPN (
                                        2;
                                        CALCULATETABLE (
                                            SUMMARIZE ( Table1; Table1[Country]; Table1[Category]; Table1[Brand] );
                                            ALL ( Table1[Brand] )
                                        );
                                        CALCULATE ( SUM ( Table1[Value] ) ); DESC
                                    );
                                    Table1[Brand] = SELECTEDVALUE ( Table1[Brand] )
                                )
                            ) > 0
                        )
                );
                [Ranks_in_Top2]
            )
        )
    );
    [Aux]
)

and change 2 for 1 in the first argument of the TOPN for the other measure 

or if you want it a bit more readable, reutilize the measured we created initially:

Measure2_Total_V2 =
AVERAGEX (
    ADDCOLUMNS (
        SUMMARIZE ( Table1; Table1[Country]; Table1[Brand] );
        "Aux"; [Measure2]
    );
    [Aux]
)

and change [Measure2] for [Measure1] in the code for the other measure

Amazing, thanks @AlB . One more question, if I have two slicers on the page (Country and Brand), and I wanted to calculate the average as below for all Brands (ignoring the Brand slicer) in the Country (selected in slicer), how can I do this? 

 

I can't figure out where to put ALL() to make it work!! 

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.