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
_Regina
Helper I
Helper I

Calculate with TOPN

I have a table like this

TableA

 

PromotionCommissionSale dateSale IDProduct ID
PromoA1021 Dec 20211231
PromoA1029 Dec20211241
PromoA85 Jan 20221252
PromoB1516Nov 20211222

 

Table B

Product IDProduct Description
1TV
2Camera

 

I need to calculate total commission by Promo for first 10 sales where product category is TV and sale date is between 1 Sept 2021 to 31 Dec 2021 and where Commission is not zero.

 

I wrote the measure as

Switch(Promotion,"PromoA",
CALCULATE (
SUM ( [Commissions] ),
DATESBETWEEN (
[Sale date],
DATE ( 2021, 09, 01 ),
DATE ( 2021, 12, 31 )
),
 TOPN(
 10,
 TableA,
TableA[Sale ID],
 DESC
),
filter(TableA ,related(TableB[Product Description])="TV"
),TableA[Commissions]<>0)
 
 
This gives me blank But if I remove the TOPN clause it gives me correct result. what should I do to get the correct results with TOPN.
 
I would really appreciate your guidance here.
 
Thanks 
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@_Regina , Try like

Measure =
Var _1 =
CALCULATE (
SUM ( [Commissions] ),
DATESBETWEEN (
[Sale date],
DATE ( 2021, 09, 01 ),
DATE ( 2021, 12, 31 )
),filter(TableA ,related(TableB[Product Description])="TV" && TableA[Commissions]<>0))
return
Calculate( _1, TOPN(
10,
allselected(TableA[Sale ID]), _1
DESC
), Values(TableA[Sale ID]) )

 

 

not clear on need to switch

 

refer

https://www.sqlbi.com/articles/filtering-the-top-3-products-for-each-category-in-power-bi/

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

View solution in original post

2 REPLIES 2
amitchandak
Super User
Super User

@_Regina , Try like

Measure =
Var _1 =
CALCULATE (
SUM ( [Commissions] ),
DATESBETWEEN (
[Sale date],
DATE ( 2021, 09, 01 ),
DATE ( 2021, 12, 31 )
),filter(TableA ,related(TableB[Product Description])="TV" && TableA[Commissions]<>0))
return
Calculate( _1, TOPN(
10,
allselected(TableA[Sale ID]), _1
DESC
), Values(TableA[Sale ID]) )

 

 

not clear on need to switch

 

refer

https://www.sqlbi.com/articles/filtering-the-top-3-products-for-each-category-in-power-bi/

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

Thank you . Could you help me understand why your solution works and not mine ?

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

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.