Forum Discussion

rmelvin's avatar
rmelvin
Frequent Visitor
6 years ago
Solved

RANKX or TOPN based on two fields

I have a summarized dataset I am using to create a scorecard for different products/categories.

 

The main data table is a list of products, the # of units sold, and the start date and end date of the month

 

Product     # of Units Sold       Start Date       End Date

A1               50                           Jan 1, 2019      Jan 31, 2019

B1               25                           Jan 1, 2019      Jan 31, 2019

A1               60                           Feb 1, 2019      Feb 28, 2019

B1               35                           Feb 1, 2019      Feb 28, 2019

 

The measure that summarizes the # of units sold is a rolling six month average, using the filter function, and [Latest Score Date] and [Earliest Score Date] measures from my calendar table.  These measures define the six month window.

Avg. Monthly # of Legs - Rolling Average:= CALCULATE(SUM('Product Scorecard KPI'[# of Units])/[# of Months - Total Score], FILTER('Product Scorecard KPI',
'Product Scorecard KPI'[Month End Date] <= [Latest Score Date] &&
'Product Scorecard KPI'[Month End Date] >= [Earliest Score Date])

 

This measure essentially returns the total # of units between Jan 1, 2019 - Jun 30, 2019, when filtered for July 1, 2019. (Feb 1 to Jul 31 when filtered for Aug 1, 2019, etc....)

 

I want to create a rankx measure that ranks these products based on two conditions

1. The product category (every product in the table above has a specific category it is grouped under)

2. The six month window defined in the measure above (based on the [latest score date] and [earliest score date])

 

I have seen rankx measures that are based on multiple criteria, but those criteria are always in the same table, and with a 1to1 relationship.

The product table contains the category, and the calendar table contains the dates, so they are different tables.

With each score dynamically being the rolling six month average, i also am not sure how to handle that.

 

All I am looking for really, is for my report that has 12 months showing for a bunch of different measures, I want some formula/filter i can apply to the page to narrow in on the top 15 products under each category in each month. Rankx and topn are both concepts i simply have not been able to wrap my head around, and I feel this example is a bit more complicated than average.

 

Any assistance would be appreciated.  If any additional information is needed, I'll try and provide it.

 

  • rmelvin's avatar
    rmelvin
    6 years ago

    The link you provided was actually the best solution I could find online as well.  It seemed to be exactly what I wanted at first. 

    The issue is that the formula they create uses the ALL function to create the criteria table, and the all function can only reference fields in the same table. I want my rankx to be based on Year-Month (in the calendar table) and Product Category (in the product table). I don't have a direct relationship from the KPI table and the calendar table, so it has to be dynamic.

     

    I ended up building a ranking table in SQL, added it to the data model, and joined it to the product and calendar tables. I changed my measures to reference that table, and show as blank if the rank of that product/month was >15, and did a sumx.  so not perfect, but is working quite well for my hardcoded six month windows.  If i ever need it to be dynamic, and rerank the top 15 for whatever timeframe i pick, this solution wouldn't work.  But for now this does.

     

    Thanks for the quick responses though.

4 Replies