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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

TopN function cant display on the matrix

Hi, community, 

Please see the attached picture. I have a TOPN measure. I would like to get the top 10 customers based on the sales amount.

TOP10PCUSTOMERBYSALES1 = TOPN(10,VALUES(Orders[Customer ID]),SUM(Orders[Sales]),DESC)
When showing the measure in a table matrix, it gives the error below.
Above measure as illustrated in the book named "Exam Ref 70-778 analyzing and visualizing data by using Microsoft Power BI. It does not work in my case. 
 
If anyone can explain the logic and reason why it is wrong, also provide a solution that will be great. thank you!
2020-05-16 23_12_31-learing - Power BI Desktop.png

 

4 REPLIES 4
AllisonKennedy
Super User
Super User

@Anonymous  It looks like you're trying to use a Table function inside a MEASURE, so TOPN is returning an entire table of values, but you need to get that down to 1 scalar expression, which is what @amitchandak 's suggestion will do for you. 

 

By putting the TOPN inside a filter expression of a CALCULATE, you are forcing the measure to only calculate for the TOPN values, but you still need to define what the measure actually is (ie SUM(Orders[Sales]). If you do this only, then you will get the same value for every customer, ie total sales for top 10 customers combined: 

 

TotalSalesforTop10Customers =
CALCULATE(SUM(Orders[Sales]), TOPN(10,VALUES(Orders[Customer ID]), SUM(Orders[Sales]), DESC))

 

IF you use a variable, then you can return each customer's total sales for only the Top10 customers:

Top10CustomersbySales =
VAR ranking= VALUES(Orders[Customer ID])
RETURN
CALCULATE(SUM(Orders[Sales]), TOPN(10,ALL(Orders[Customer ID]), SUM(Orders[Sales]), DESC),ranking)

 

Otherwise do your formula as a new TABLE rather than a MEASURE. 

 

Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved. 

 

If you found this post helpful, please give Kudos.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Anonymous
Not applicable

@AllisonKennedy  thank you for the detailed explanation! that is very helpful for me to understand why... thanks again!

You could also use the built in TOP N visual level filter on Customer ID (change the filter on Customer ID from Advanced or Basic to Top N) and drag Sales into the 'by value' field. 

 

Has this post solved your problem? Please mark it as a solution so that others can find it quickly and to let the community know your problem has been solved. 

 

If you found this post helpful, please give Kudos.

I work as a trainer and consultant for Microsoft 365, specialising in Power BI and Power Query. 

https://sites.google.com/site/allisonkennedycv


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

amitchandak
Super User
Super User

@Anonymous , refer , if this can help

https://databear.com/power-bi-dax-topn-function/

 

TOP10PCUSTOMERBYSALES1 =
VAR
ranking = VALUES(Orders[Customer ID])
Return
CALCULATE(SUM(Orders[Sales]),
TOPN(10,ALL(Orders[Customer ID]),SUM(Orders[Sales])),
ranking,DESC)

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
https://radacad.com/how-to-use-rankx-in-dax-part-3-of-3-the-finale

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.