cancel
Showing results for
Did you mean:
Highlighted
Helper I

How to output a text column based on a ranking measure and aggregate the values?

Hey,

If I have 5 animals for example with different values. I want to be able to rank the top three and then output the top 3 names and aggregte the remaining animals that didn't make it to the top 3.

 Animal Value Rank Horses 9 1 Chickens 8 2 Frogs 7 3 Dogs 4 4 Cats 1 5 Desired Output Animal Value Horses 9 Chickens 8 Frogs 7 Other 5

I ranked using a measure:

RankTop3 =

Var RankAnimal = RANKX(ALLNOBLANKROW('Table'[Animal]),[Value],
,
DESC,
Dense
)
RETURN IF(RankAnimal <=3,[Value],BLANK())

However, this gives me the top 5 values when I put the column Animals and the measure RankTop3 together and not my desired output.

I was thinking I may need to use a calculated column as I need an IF statement that works with text but I don't know how to use it in conjunction with rank which is a measure.

Does anyone have any ideas?

Lauren.
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Impactful Individual

Re: How to output a text column based on a ranking measure and aggregate the values?

Here is an example using Contoso dataset, but the idea is same. first create a rank column in your table based on the amount or anything.

``````ProductRank =
RANKX (
ALL ( Products[Brand] ),
CALCULATE ( [Total Sales], ALLEXCEPT ( Products, Products[Brand] ) ),
,
ASC
)``````

Now we create a new column to group the brands based on the ranking

``````NewProductCategory =
VAR ProductRank = Products[ProductRank]
VAR ProductBrand = Products[Brand]
VAR Result =
IF ( ProductRank IN { 1, 2, 3 }, ProductBrand, "Others" )
RETURN
Result``````

Now the final step is the put everything in a visualization

``Total Sales = SUMX ( Sales, Sales[Quantity] * Sales[Net Price] )``
``````Rank on Sales =
IF (
HASONEVALUE ( Products[NewProductCategory] ),
RANKX ( ALL ( Products[NewProductCategory] ), [Total Sales] )
)``````

Thank you,
Antriksh Sharma
4 REPLIES 4
Highlighted
Super User IV

Re: How to output a text column based on a ranking measure and aggregate the values?

Try this,

``````Top 3 and Others =
IF(
RANKX( 'Table', 'Table'[Value] ) <= 3,
'Table'[Animal],
"Others"
)``````

the rank formula will need to be adjusted if you have Animals duplicated in your table.

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.

Highlighted
Impactful Individual

Re: How to output a text column based on a ranking measure and aggregate the values?

Here is an example using Contoso dataset, but the idea is same. first create a rank column in your table based on the amount or anything.

``````ProductRank =
RANKX (
ALL ( Products[Brand] ),
CALCULATE ( [Total Sales], ALLEXCEPT ( Products, Products[Brand] ) ),
,
ASC
)``````

Now we create a new column to group the brands based on the ranking

``````NewProductCategory =
VAR ProductRank = Products[ProductRank]
VAR ProductBrand = Products[Brand]
VAR Result =
IF ( ProductRank IN { 1, 2, 3 }, ProductBrand, "Others" )
RETURN
Result``````

Now the final step is the put everything in a visualization

``Total Sales = SUMX ( Sales, Sales[Quantity] * Sales[Net Price] )``
``````Rank on Sales =
IF (
HASONEVALUE ( Products[NewProductCategory] ),
RANKX ( ALL ( Products[NewProductCategory] ), [Total Sales] )
)``````

Thank you,
Antriksh Sharma
Highlighted
Helper I

Re: How to output a text column based on a ranking measure and aggregate the values?

You are a legend! Very much appreciated, thank you. 🙂

Highlighted
Impactful Individual

Re: How to output a text column based on a ranking measure and aggregate the values?

Thank you,
Antriksh Sharma

Announcements

August 2020 Community Challenge: Can You Solve These?

We're excited to announce our first cross-community 'Can You Solve These?' challenge!

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Upcoming Events

Wondering what events you could join or have an event to promote yourself? Check out our Upcoming Events.

Get Ready for Power BI Dev Camp

We are thrilled to announce we will begin running a monthly webinar series named Power BI Dev Camp.

Top Solution Authors
Top Kudoed Authors