Forum Discussion

badger123's avatar
badger123
Resolver I
7 years ago
Solved

Rank, count rank and average

I have a table that looks something like this: Country Category Term Brand Value UK category a category a term one brand one brand one 20 UK category a category a term two brand t...
  • AlB's avatar
    7 years ago

    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'.