Forum Discussion

murrayb8's avatar
murrayb8
Icon for Helper I rankHelper I
8 years ago
Solved

Showing Top N values per category based on a value

Hi I was wondering if someone could help me with a roadblock im hitting. I have a large data set that has multiple categories and I need to filter to the top N number of entries based on a value for ...
  • murrayb8's avatar
    8 years ago

    I managed to fins a way to do this thankfully:

     

    Step #1: I went into query editor and sorted my table high to low by the value column and then added an index column called Index and applied changes.

     

    Step #2: I added a calculated column called rank using this formula: Rank = RANKX(FILTER('mytable','myTable'[uniqueProductCode]=EARLIER('myTable'[uniqueProductCode])),'myTable'[Index],,ASC)

     

    Step #3: I selected the modeling tab, clicked New Table and used the following formula for my new calculated table: 

    Top 1000 Products =
    SELECTCOLUMNS (
    FILTER ( 'myTable', 'myTable'[Rank] <= 1000 ),
    "Product", 'myTable'[Product],
    "Phrase", 'myTable'[Phrase],
    "UniqueProductCode", 'myTable'[uniqueProductCode],
    "Value", [Sum of Value],
    "Rank", 'myTable'[Rank]
    )

    I can then adjust the table as needed by changing "<= 1000" to represent the top N values per category. Hope this helps others if they are looking for the same solution.