Forum Discussion
RANKX Always Returns 1
- 9 years ago
If you do want to use a Rank Measure in the Visual Filter you have to adjust how the sum is calculated like this...
Rank Product = RANKX ( ALL(MyData[Product] ), CALCULATE ( SUM ( MyData[Quantity] ), ALLEXCEPT(MyData, MyData[Product] ) ) )
See below...
Hope this helps! :smileyhappy:
Basically I need a dynamic top 5 filter by quantity of product defects for my stacked column chart.
I've created a simplified data set called MyData including rows of claim ID, Name, ProductLine, Product, Category, Region, Date, Quantity, and Cost. I made ProductLine and Category slicers and a stacked column chart with Product on the axis, Date in the legend, and Cost as the value then I sort by Cost. Quantity allows for multiple defective Products on one claim (data set of 100 claims with 124 total quantity). All of this works nicely.
Now I add the following Measure for total quantity:
TotalQuantity = CALCULATE(SUM(MyData[Quantity]), ALLEXCEPT(MyData, MyData[ProductLine], MyData[Category]))
I added a card for TotalQuantity which updates correctly when I toggle the slicer options.
For rank, first I tried the following Column in attempt to get a 1-N rank of my Products so that later I can simplify my stacked column chart to top X:
QuantityRankColumn = RANKX(ALL(MyData[Product]), [TotalQuantity], , DESC)
Then I added a column chart for QuantityRankColumn by Product and this results in the count of claims (rows) for each Product, summing to 100 (total number of rows).
I thought it was odd doing ALL(MyData[Product]) by not including Quantity so I changed it to ALL(MyData[Product], MyData[Quantity]) but that didn't change anything.
Next, I tried the following rank Measure to see if I had better luck here than with my above rank Column:
QuantityRankMeasure = IF(HASONEVALUE(MyData[Product]), RANKX(ALL(MyData[Product]), CALCULATE(SUM(MyData[Quantity]), ALLEXCEPT(MyData, MyData[ProductLine], MyData[Category]))))
This results in a rank of 1 for all Products. I also tried removing the IF statement then including the Quantity column in the ALL function but again no change away from all 1's.
I've used other visualization tools and this method for Power BI seems like a lot of overcomplicated work to simply see only the top bars in a chart. I'm open to suggestions.
Can you upload this sample file you've created to OneDrive, DropBox or similar service?
- amilecki9 years agoFrequent Visitor
Increased data set to 500 claims and more products and uploaded to DropBox here:
https://www.dropbox.com/s/vpajtr7dvpum2e5/PowerBI_SampleData_RANKX.xlsx?dl=0
Running a quick Excel pivot table, I expect to see the following:
Product Quantity Rank
P06 40 1
P25 37 2
P22 35 3
P01 30 4
P04 28 5
...
This way I can simplify a stacked column chart from 25 Products down to just the top 5 using Rank.
Thank you both for your time so far.
- Sean9 years ago
Community Champion
How do you get Quantity for P06 to be 40 ? and not 18?
- amilecki9 years agoFrequent Visitor
You are right, sorry about that. I originally had a generator spreadsheet which dynamically changed after I copy-pasted the data values into the DropBox spreadsheet. The correct expected top 5 is:
Prod Qty Rank
P17 37 1
P07 36 2
P12 34 3
P03 34 3
P15 34 3