Forum Discussion

nish18_1990's avatar
nish18_1990
Icon for Helper III rankHelper III
1 year ago
Solved

Top N values based on the total count in matrix

 
 

Hi ,

 

I need a top N parameter based on the total count of Sales .  If i select top 2 then it should show me top 2 subcategory based on the total Sales which is displayed on the right side .

 

 

 

 

Data:

 

CategorySub CategorySalesQuantity Q2country
FurnitureChairs10010China
FurnitureTables10020China
AutomobileCars10030China
AutomobileBikes70040China
FurnitureChairs10013India
FurnitureTables50022India
AutomobileCars10054India
AutomobileBikes10060India
FurnitureChairs10030China
FurnitureTables30029China
AutomobileCars20088China
AutomobileBikes10022China
FurnitureChairs50021India
FurnitureTables10033India
AutomobileCars60054India
AutomobileBikes10060India
FurnitureChairs10030China
FurnitureTables10029China
FurnitureSofa10088China
FurnitureBed10022China
FurnitureDining10021India
FurnitureTables10033India
FurnitureTables10054India
FurnitureSofa10060India
AutomobileCars100054India
AutomobileBikes10060India
FurnitureChairs10030China
FurnitureTables20029China
FurnitureSofa10088China
FurnitureBed10022China
FurnitureDining10021India
FurnitureTables40033India
FurnitureTables80054India
FurnitureSofa10060India

 

  • nish18_1990 - then you have NO OPTION other than to use a Table visual (not a Matrix). It cannot be a Matrix because a Matrix does not supply the Row context for Sub Category that is needed for the rank. 

     

    If you want to break ties by Sub Category YOU NEED TO USE A TABLE. 

     

    I have attached a final version of this file. Page 1 has the measure Rank 2 which uses the DAX below to rank in the following order:

     

    1. Count of Quantity

    2. Sum of sales (to break any ties above)

    3. Sub Category A-Z (to further break ties from above)

    4. Country A-Z (to break the final ties)

     

    VAR _rank =
        RANK (
            DENSE,
            ALLSELECTED ( 'Table'[Sub Category], 'Table'[country] ),
            ORDERBY (
                CALCULATE ( COUNT ( 'Table'[Quantity Q2] ) ), DESC,
                CALCULATE ( SUM ( 'Table'[Sales] ) ), DESC,
                'Table'[Sub Category], ASC,
                'Table'[country], ASC
            ),
            LAST
        )
    RETURN
        IF ( _rank <= [Parameter Value], _rank )

     

    The table also contains two measures for Count of QTY and Sum of Sales that contain logic to return blank when the rank returns blank. This happens when the parameter value is set to show only a certain number of results. - Which has been your requirement all along, and can only be achieved using a TABLE. 

     

    I've now provided this solution multiple times and fine tuned it as much as I can, I'd appreciate it if you could mark it as the solution. 

     

    If I answered your question please mark my post as the solution, it helps others with the same challenge find the answer!

12 Replies

  • m4ni's avatar
    m4ni
    Icon for Resolver I rankResolver I

    Hi nish18_1990 

    If I understand correctly you should be able to acheive this from the filter pane.

     

    Select TOP N from on the Sub category and then enter 2.

    I would create a measure for the count of sales and then select that measure as the By Value in the filter pane.

    Please see screenshot unsing your data.

     

     

    Hope this is what you mean.  Otherwise please explain further.

    Thanks

     

    • nish18_1990's avatar
      nish18_1990
      Icon for Helper III rankHelper III

      No I dont want it in filter tab . I need a dynamic parameter for top N . If somebody choose 5 on parameter it should be top 5 categories . I did achieved it partially by rank .

      Var Ranks:
      CALCULATE(
          RANKX(
          ALL('My_Main_Table'),
          CALCULATE(
          [#sales],
              ALLEXCEPT('My_Main_Table',My_Main_Table[Subcategory])),,
          DESC,
         Dense
              )
          )

       

      But the issue  i am getting  is same rank for same count , because of that if i select 10 on paramater , I am getting more than 10 sub categories :

       

       

      • nish18_1990's avatar
        nish18_1990
        Icon for Helper III rankHelper III

        what i need is :

         

        Count      Var rank

        38                1

        32                2

        11                3

        11                4

        9                  5

        5                  6

        5                   7

        5                  8

         

         

  • Hi nish18_1990 ,
    I tried to implement the solution for the problem. Please check the pbix file.
    Top N values based on the total count in matrix.pbix
    I checked in a table visual only. Please let me know if there is any questions..

    If this reply helped solve your problem, please consider clicking "Accept as Solution" so others can benefit too. And if you found it useful, a quick "Kudos" is always appreciated, thanks! 

     

    Best Regards, 

    Maruthi 

    LinkedIn - http://www.linkedin.com/in/maruthi-siva-prasad/ 

    X            -  Maruthi Siva Prasad - (@MaruthiSP) / X