Forum Discussion

lokosrio's avatar
lokosrio
Helper II
2 years ago
Solved

Rank top 3 by differed categories

hi, I have a table with data:

CarCountrySalesGrowth
BMWGermany10025
MercedesGermany20030
VWGermany30020
AudiGermany40010
ToyotaJapan10010
CitroenFrance20015
Aston MartinGreat Britain30020
SeatSpain40025
FordUSA50030

 

Table has many more columns, which are not used in visualization.

 

I want to create a visual (table), where I will show:

- top 3 cars from germany by sales

- top 3 cars from rest of the countries by sales (and is not Toyota)

- Toyota

- top 1 car by growth (is not top 3 from germany, is not top 3 from rest of countries and is not Toyota) 

 

Result should be (ideally in this order):

CarSales
Audi400
VW300
Mercedes200
Ford500
Seat400
Aston Martin300
Toyota100
BMW100

 

I tried with the code below (without top 1 car by growth), but it is not working:

 

 

var _germany = TOPN (
            3,
            FILTER (
            ALLSELECTED ( Table[Car] ),
            SELECTEDVALUE ( Table[Car] ) <> "Toyota" && SELECTEDVALUE ( Table[Country] ) = "Germany"
        ),
            [Sales]
        )
var _nongermany = TOPN (
            3,
            FILTER (
            ALLSELECTED ( Table[Car] ),
            SELECTEDVALUE ( Table[Car] ) <> "Toyota" && SELECTEDVALUE ( Table[Country] ) <> "Germany"
        ),
            [Sales]
        )
var _toyota = FILTER (
            ALL ( Table[Car] ),
            SELECTEDVALUE ( Table[RetailBrandCode] ) = "Toyota"
        )
return
CALCULATE (
    [Growth Drivers - Custom Group Current Whole Number],
        UNION (
        _germany,
        _nongermany 
        _toyota 
        )
    ,VALUES ( Table[Car] )
)

 

 

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi lokosrio ,

    Based on the information you have provided, you can follow the steps below to solve your problem:
    1. Add a column to sort the sales.

     

    Rank_Sales = 
    IF (
        'Table'[Car] = "Toyota",
        BLANK (),
        IF (
            'Table'[Country] = "Germany",
            RANKX (
                FILTER ( 'Table', 'Table'[Country] = EARLIER ( 'Table'[Country] ) ),
                'Table'[Sales]
            ),
            RANKX ( FILTER ( 'Table', 'Table'[Country] <> "Germany" ), 'Table'[Sales] )
        )
    )
    

     

    2.Add a flag column, filter results with flag=1.

     

    Flag = 
    VAR _rank_growth =
        IF (
            'Table'[Rank_Sales] > 3
                && 'Table'[Car] <> "Toyota",
            RANKX (
                FILTER ( 'Table', 'Table'[Rank_Sales] > 3 && 'Table'[Car] <> "Toyota" ),
                'Table'[Growth]
            ),
            BLANK ()
        )
    RETURN
        IF (
            'Table'[Rank_Sales] <= 3
                || _rank_growth = 1
                || 'Table'[Car] = "Toyota",
            1,
            0
        )
    

     

    3.Add result column.

     

    Rank_Result = 
    VAR _1 =
        IF ( 'Table'[Rank_Sales] <= 3, 'Table'[Rank_Sales], 0 )
    VAR _2 =
        IF ( 'Table'[Country] = "Germany", _1, _1 + 3 )
    VAR _3 =
        IF ( 'Table'[Country] <> "Germany" && _2 = 3, 7, _2 )
    RETURN
        IF ( _3 = 0, 8, _3 )
    

     

    Final output:

    How to Get Your Question Answered Quickly - Microsoft Fabric Community

    If it does not help, please provide more details with your desired out put and pbix file without privacy information.

     

    Best Regards,

    Ada Wang

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

     

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi lokosrio ,

    Based on the information you have provided, you can follow the steps below to solve your problem:
    1. Add a column to sort the sales.

     

    Rank_Sales = 
    IF (
        'Table'[Car] = "Toyota",
        BLANK (),
        IF (
            'Table'[Country] = "Germany",
            RANKX (
                FILTER ( 'Table', 'Table'[Country] = EARLIER ( 'Table'[Country] ) ),
                'Table'[Sales]
            ),
            RANKX ( FILTER ( 'Table', 'Table'[Country] <> "Germany" ), 'Table'[Sales] )
        )
    )
    

     

    2.Add a flag column, filter results with flag=1.

     

    Flag = 
    VAR _rank_growth =
        IF (
            'Table'[Rank_Sales] > 3
                && 'Table'[Car] <> "Toyota",
            RANKX (
                FILTER ( 'Table', 'Table'[Rank_Sales] > 3 && 'Table'[Car] <> "Toyota" ),
                'Table'[Growth]
            ),
            BLANK ()
        )
    RETURN
        IF (
            'Table'[Rank_Sales] <= 3
                || _rank_growth = 1
                || 'Table'[Car] = "Toyota",
            1,
            0
        )
    

     

    3.Add result column.

     

    Rank_Result = 
    VAR _1 =
        IF ( 'Table'[Rank_Sales] <= 3, 'Table'[Rank_Sales], 0 )
    VAR _2 =
        IF ( 'Table'[Country] = "Germany", _1, _1 + 3 )
    VAR _3 =
        IF ( 'Table'[Country] <> "Germany" && _2 = 3, 7, _2 )
    RETURN
        IF ( _3 = 0, 8, _3 )
    

     

    Final output:

    How to Get Your Question Answered Quickly - Microsoft Fabric Community

    If it does not help, please provide more details with your desired out put and pbix file without privacy information.

     

    Best Regards,

    Ada Wang

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