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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
mohassan99
Helper II
Helper II

How do I calculate a measure to return the top 5 in sorted order?

I have a table including "MoneyLeftOnTable" and "cgToMaxPotential".  I want to sort on "MoneyLeftOnTable" DESC then "cgToMaxPotential" ASC and return the top 5 rows ordered by "MoneyLeftOnTable" DESC then "cgToMaxPotential" ASC. 

 

I got the top 5 by creating a rank variable to take sort by when I use TopN filter by rank variable. Now I would only like them ordered and this is not the default behavior of the TOPN filter. Can I calculate a measure to sort the results and return the top 5 so I can bypass the Top5 filter?

 

There is a hidden filter on the page to filter for a single provider at a single location in a single year that should result in each measure having one row in results.

 

I am using desktop and data import mode, not DirectQuery.

 

I am a novice to DAX and it seems complicated. So much nesting of things that even reading the documentation doesn't really clarify to me.

 

Table:

mohassan99_0-1652298194851.png

 

Filter:

 

mohassan99_1-1652298218232.png

7 REPLIES 7
v-yalanwu-msft
Community Support
Community Support

Hi, @mohassan99 ;


Is your problem solved? If so, kindly mark the proper reply as a solution to help others having the similar issue and close the case. If not, let me know and I'll try to help you further.
Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Not solved. 

mohassan99
Helper II
Helper II

 

@v-yalanwu-msft @amitchandak  Why am I getting an error for this:

mohassan99_0-1652059603572.png

mohassan99_1-1652059735155.png

 

 

Remove the ) from before the first comma.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yalanwu-msft
Community Support
Community Support

Hi, @mohassan99 ;

Can you show your current visual and expected result with some screenshot or pictures (after removing sensitive info)? I don't understand what the visual expected to be like. Dummy data is also appreciated. Thanks.

and may be rank() or topn is suit for you.


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

mohassan99
Helper II
Helper II

There's only one value for each of these and only one "date" value stored as text for each year, "2020" & "2021". 

 

amitchandak
Super User
Super User

@mohassan99 , Assume you have measure return or last year revenue

 

Top 5 Rank = CALCULATE([last year revenue],TOPN(5,all(Table[Company]),[last year revenue],DESC),VALUES(Table[Company]))

 

 

for last year refer TI option

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))
This year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR('Date'[Date]),"12/31"))
Last year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31"))
YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),previousyear('Date'[Date]))
Last to last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-2,Year),"12/31"))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),SAMEPERIODLASTYEAR('Date'[Date]))

 

//Only year vs Year, not a level below

This Year = CALCULATE(sum('Table'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])))
Last Year = CALCULATE(sum('Table'[Qty]),filter(ALL('Date'),'Date'[Year]=max('Date'[Year])-1))

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.