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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

TopN with duplicates

Hi guys,

I would like to create a TOPN measure that sums up the revenues generated by the 2 most high-selling products. However, my table (a simplified version below) contains duplicates.

Is it possible to create a TOPN measure for revenue (column 3) that recognizes duplicates by product ID (column 1)?

 

 

Input:

Product IDRevenue EURO
A100
A100
B50
C20
D10

 

My desired output would be a TOPN measure that gives the result 250 for the top 2 most high selling products, in this instance: A generates 200 EURO and B 50 EURO.

So the measure should on the one hand recognize duplicates (here: A) but still summarize the revenue for duplicate products.

Hope that is not too confusing!

 

 

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Please try this expression in a measure to get your desired result.

 

Top 2 Sum =
VAR summary =
    ADDCOLUMNS (
        VALUES ( 'Table'[Product ID] ),
        "@total", CALCULATE ( SUM ( 'Table'[Revenue EURO] ) )
    )
RETURN
    SUMX ( TOPN ( 2, summary, [@total], DESC ), [@total] )

 

If this works for you, please mark it as the 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

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous ,

According to my understanding, you want to sum the top2 values when there are duplicate values, right?

You could use the following formula:

 

rank =
RANKX (
    ALLSELECTED ( 'Table' ),
    CALCULATE ( SUM ( 'Table'[sumColumn] ) ),
    ,
    DESC,
    DENSE
)
sumTop2 =
CALCULATE (
    SUM ( 'Table'[Revenue EURO] ),
    FILTER ( ALL ( 'Table' ), [rank] <= 2 )
)

My visualization looks like this:

8.13.1.jpg

Is the result what you want? If not, please upload some data samples and expected output.

Please do mask sensitive data before uploading.

 

Best Regards,

Eyelyn Qin

mahoneypat
Microsoft Employee
Microsoft Employee

Please try this expression in a measure to get your desired result.

 

Top 2 Sum =
VAR summary =
    ADDCOLUMNS (
        VALUES ( 'Table'[Product ID] ),
        "@total", CALCULATE ( SUM ( 'Table'[Revenue EURO] ) )
    )
RETURN
    SUMX ( TOPN ( 2, summary, [@total], DESC ), [@total] )

 

If this works for you, please mark it as the 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


AllisonKennedy
Super User
Super User

@Anonymous
What end goal are you looking for? There are built in TOPN filters on most visuals, so you could actually solve this by creating a Matrix visual with:
Product ID in Rows
Sum of Revenue in Values
Apply a filter on Product ID and set the type to TopN instead of Basic. Type 2 for the Top N. Drag Revenue into the the box to calculate the top 2 by. Click Apply filter.
You will now see a list of only the top 2 products by TOTAL revenue based on any other filters in your report.

Otherwise if you want to do this with DAX you could use a grouping function such as SUMMARIZECOLUMNS or other option to group by Product ID.

I hope I understood your question correctly.

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

Hi @Anonymous 

 

Create a measure as

Measure = CALCULATE(SUMX(TOPN(2,VALUES('Table'[Revenue]),'Table'[Revenue],DESC),'Table'[Revenue]))

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

Regards,
Pranit

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.