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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Kolumam
Post Prodigy
Post Prodigy

How to rank based on 2 columns?

Hi All,

 

I need to rank the table based on 2 columns. Here is the table.

 

NameProbabilityQuantityClose Date
Tesla20%101/9/2020
Honda50%101/8/2020
Maruti50%101/7/2020

 

In my above table, first it needs to be ranked by probability * quanitity.

If probability * quantity is same for two rows, it needs to be ranked by the Close Date. So the earlier the close date, the higher the ranking.

 

Expected Output:

 

NameProbabilityQuantityClose DateRanking
Tesla20%101/9/20203
Honda50%101/8/20202
Maruti50%101/7/20201

 

Is it possible?

 

2 ACCEPTED SOLUTIONS
mahoneypat
Employee
Employee

Please try this expression.  I put your data in and called the table 'Make'.  It works by also adding in the days between today and the close date (divided by a large number so it doesn't affect the overall rank, but does break the tie).  Based on your values, you can increase the divided by number.

 

Rank with Date =
RANKX (
ALL ( Make ),
CALCULATE ( SUMX ( Make, Make[Quantity] * Make[Probability] ) )
+ DATEDIFF ( CALCULATE ( MIN ( Make[Close Date] ) ), TODAY (), DAY ) / 1000
)

 

If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

az38
Community Champion
Community Champion

@Kolumam 

try a measure

Measure = RANKX(
ALLSELECTED('Table'), 
CALCULATE(
SUMX('Table','Table'[Probability]*'Table'[Quantity] + (10000 - INT('Table'[Close Date])/10000))
),
 , DESC)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

6 REPLIES 6
mahoneypat
Employee
Employee

Please try this expression.  I put your data in and called the table 'Make'.  It works by also adding in the days between today and the close date (divided by a large number so it doesn't affect the overall rank, but does break the tie).  Based on your values, you can increase the divided by number.

 

Rank with Date =
RANKX (
ALL ( Make ),
CALCULATE ( SUMX ( Make, Make[Quantity] * Make[Probability] ) )
+ DATEDIFF ( CALCULATE ( MIN ( Make[Close Date] ) ), TODAY (), DAY ) / 1000
)

 

If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


az38
Community Champion
Community Champion

Hi @Kolumam 

try technique from this great article https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns

 

Column = RANKX('Table', [Probability]*[Quantity] + (10000 - INT([Close Date])/10000), , DESC)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Thank you so much. It kinda worked!

 

The issue now is how do I add a country filter from Country table?

 

See the below screenshot. If I filter a country, the ranking get's lost and it doesn't start from 1.

 

unknown.png

Try changing the ALL() to an ALLSELECTED() so it responds to the slicers.

 

If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thanks mate! It worked. If I have any issues will get back to you.

az38
Community Champion
Community Champion

@Kolumam 

try a measure

Measure = RANKX(
ALLSELECTED('Table'), 
CALCULATE(
SUMX('Table','Table'[Probability]*'Table'[Quantity] + (10000 - INT('Table'[Close Date])/10000))
),
 , DESC)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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