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
vendersonalias0
Frequent Visitor

How to RANK by MAX date of each group without creating a second table?

My source data has a table with users and each sale they make is recorded as a row.

My goal is to find the latest date each user made a sale then rank those dates accordingly with the latest being 1.

 

Table Example

pbihelp.PNG

 

The formula I'm using to find the max date is working correctly

Max Date = CALCULATE(MAX('salestable'[Date]),ALLSELECTED('salestable'[Date]))
 
but my last sale measure is giving me the ranking relative to all dates rather than the max date of each group
my intended output should have:
rank 2 instead of rank 4,
rank 3 instead of rank 7 etc.
I believe it is ranking each group's max date vs all dates in the table instead of only the ranks by group.
 
Last Sale = RANKX(ALL('salestable'),[Max Date],,DESC,Dense)
 
I'm not sure if there is a better way to do this but the desired output is to have cards displaying the names of the last few users which made a sale. (In the real data, there are at most 2 sales / week)
My idea was to have a multi row card with all users filtered rank 1, the second card would be users filtered to rank 2 etc. up until 3-4. There is probably a better way to do this but this is what I came up with, any suggestions or improvements would be welcome too.
 
Thank you!
Happy Holidays!

 

 

1 ACCEPTED SOLUTION
vendersonalias0
Frequent Visitor

Thanks for the help guys, funny enough when I was creating a mock data set I happened to use ALLEXCEPT in the MAX DATE formula, then ALLSELECTED in the last date measure started to work... pbihelp.PNG

View solution in original post

5 REPLIES 5
v-yiruan-msft
Community Support
Community Support

Hi @vendersonalias0 ,

You can create a measure as below:

Last Sale =
RANKX (
    ALLSELECTED ( 'salestable' ),
    CALCULATE ( MAX ( 'salestable'[Date] ) ),
    ,
    DESC,
    DENSE
)

Last sale.JPG

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
parry2k
Super User
Super User

@vendersonalias0 makes sense, I guess you used ALLEXCEPT ( Table, Table[User]), correct?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

vendersonalias0
Frequent Visitor

Thanks for the help guys, funny enough when I was creating a mock data set I happened to use ALLEXCEPT in the MAX DATE formula, then ALLSELECTED in the last date measure started to work... pbihelp.PNG

parry2k
Super User
Super User

@vendersonalias0 there are many ways to achieve this. I would recommend putting sample data in pbix and share thru one drive/google drive and will get you the solution.



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

CNENFRNL
Community Champion
Community Champion

@vendersonalias0 , frankly speaking, the complexity of RANKX() is way beyond one's expectation, especially when you include another measure in it; context transition occur twice during the evaluation of RANKX.

Unless you paste some dummy data or a pbix file, it's hard to author a correct measure that fits in your scenario.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

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.