Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
SuchCT
Helper II
Helper II

Ranking rows with calculated column

Hello everyone,

I have a database that looks like this:
Person             Week                    Sales

A                      1                           845
B1519
C1304
A2973
B2559
C2187
A3528
B3337
C3406
A4272
B4174
C4159



And in need to get the average sales per person considering only their top 2 weeks. For person C that would be 406 from week 3 plus 304 from week 1 divided by 2. AVG. 355.

However i can´t find a way to do it as when I use rankx to get the top weeks based on sales i always get a circular dependency with a formula along these lines
rankx ( allexcept ( table , person , week ) , Sales)

This is what i need

Person       Week          Sales        Rank

A                 1                  845              2
B15192
C13042
A29731
B25591
C21873
A35283
B33373
C34061
A42724
B41744
C41594

 

1 ACCEPTED SOLUTION

Hi @SuchCT
This gives the ranks of each person's weeks.

Nathaniel_C_1-1688591240592.png

Rank col = 

var _person = RankingSales[Person]

var _rank = rankx(FILTER(ALL(RankingSales),RankingSales[Person]=_person),RankingSales[Sales])

return _rank


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

9 REPLIES 9
Nathaniel_C
Community Champion
Community Champion

Hi @SuchCT ,
Sounds good.  I will think about making it scalable. I believe this solves your original query.

Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @SuchCT
This gives the ranks of each person's weeks.

Nathaniel_C_1-1688591240592.png

Rank col = 

var _person = RankingSales[Person]

var _rank = rankx(FILTER(ALL(RankingSales),RankingSales[Person]=_person),RankingSales[Sales])

return _rank


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




thanks, that works great so I´ll mark it as the solution. Just one more thing, I'm trying to resolve ties. In the base some person had the exact same sales for two different weeks, I'd need for one of them to have one rank and the next rank for the other week

Hi @SuchCT ,
You are welcome!
With regards to the additional question, would you give me an example, and expected outcome?

 

Thanks,

Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi  @SuchCT 
Here is a scalable value

average based on rank col = 
var _howManyWks = +3  //This is hard coded , but you could bring in from a measure

var _person = RankingSales[Person]

var _rank = rankx(FILTER(ALL(RankingSales),RankingSales[Person]=_person),RankingSales[Sales])

var _sumXwks = CALCULATE(SUM(RankingSales[Sales]), FILTER(ALL(RankingSales), RankingSales[Rank col] <=_howManyWks && RankingSales[Person]=_person))

var _aver = DIVIDE(_sumXwks,_howManyWks)

return _aver

Nathaniel_C_2-1688593283045.png



Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel







Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hi @SuchCT ,

And if you only wanted to show the value 1 time for each person you could put an IF statement in the return.  Here I matched it with the ranked week equal to the number of weeks.

 

Nathaniel_C_0-1688593938812.png


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Nathaniel_C
Community Champion
Community Champion

Hi @SuchCT 

If I understand you, you are trying to get the average of each person's top two weeks. If that is right, please try this.

Nathaniel_C_0-1688589138705.png
On the left is the original table, on the right is the table with the measure.

My table name is Ranking Sales

Average of Top 2 weeks = 

var _person = MAX(RankingSales[Person])
var _week = MAX(RankingSales[Week])

var _calcMax =CALCULATE(MAX(RankingSales[Sales]),FILTER(ALL(RankingSales),RankingSales[Person]=_person))

var _calcMaxWeek= CALCULATE(MAX(RankingSales[Week]),FILTER(ALL(RankingSales),RankingSales[Sales]=_calcMax &&RankingSales[Person]=_person))

var _calcNextMax = CALCULATE(MAX(RankingSales[Sales]),FILTER(ALL(RankingSales),RankingSales[Person]=_person && RankingSales[Week]<>_calcMaxWeek))

var _aver = DIVIDE(_calcMax+_calcNextMax,2)
return _aver

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




This works great but i'd need it to be a column as to use it on a chart as an x-axis, is that possible?

Thanks, I'll se if it aplplies and let you know. Also i'll check if I can make it scalable and work for the top n weeks too

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Kudoed Authors