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

DAX help needed

Hi everyone, 

 

I have a dax query that works very slow, and my data model consist of 1 fact table as well as 2 basic dimension like dim date, 

 

can you suggest an optimized code for the dax query below?

 

sales_ =
VAR delivery =
SUMMARIZE (
fact_sales,
fact_sales[id],
fact_sales[product],
"totalsales",
IF (
fact_sales[product_id] = "0",
CALCULATE (
SUM ( fact_sales[amount] ),
TOPN ( 4, fact_sales, fact_sales[s_id], DESC )
),
IF (
fact_sales[product_id] = "1",
CALCULATE (
SUM ( fact_sales[amount] ),
TOPN ( 2, fact_sales, fact_sales[s_id], DESC )
),
CALCULATE (
SUM ( fact_sales[amount] ),
TOPN ( 1, fact_sales, fact_sales[s_id], DESC )
)
)
)
)
RETURN
SUMX ( delivery, [totalsales] )

 

Thanks&Regards,

 

12 REPLIES 12
lbendlin
Super User
Super User

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Anonymous
Not applicable

 

Hi @lbendlin ,

 

Thank you for your reply, I have shared sample data below and dax formula, really appreciate your help,

 

looking forward to hear your feedback.

 

 

id product product_id amount s_id
100-4024449860,331,910668506
100-4024449860,331,910668868
100-4024449860,331,910670033
100-4023063296,220199334
100-4023063321,222,71866829
100-4023063351,225,878233
100-4023063537,220,8199336
100-4023063906,22211889161
100-4023063997,221,41984698
100-4023064113,225,51992505
100-4023064143,22077493
100-4023064155,2210,910037981
100-4023064180,2218,41994162
100-4023064180,222,72015940
100-4023064180,223,22034646
100-4023064287,221,310613124
100-4023064570,221,7182786
100-4023064570,221,7183063
100-4023064735,225,51994150
100-4023064946,224,610289201
100-4023065082,224,910038215
100-4023065082,221,710038252
100-4023065116,220,410037979
100-4023065148,220,7199364
100-4023065287,220,6199356
100-4023065356,222,210607848
100-4023065360,221,2183745
100-4023065604,220,610036460
100-4023065850,220,634275
    
     
     
     
     
     
     
     
     
     

sales_ =
VAR delivery =
SUMMARIZE (
fact_sales,
fact_sales[id],
fact_sales[product],
"totalsales",
IF (
fact_sales[product_id] = "0",
CALCULATE (
SUM ( fact_sales[amount] ),
TOPN ( 4, fact_sales, fact_sales[s_id], DESC )
),
IF (
fact_sales[product_id] = "1",
CALCULATE (
SUM ( fact_sales[amount] ),
TOPN ( 2, fact_sales, fact_sales[s_id], DESC )
),
CALCULATE (
SUM ( fact_sales[amount] ),
TOPN ( 1, fact_sales, fact_sales[s_id], DESC )
)
)
)
)
RETURN
SUMX ( delivery, [totalsales] )

 

can you please re-post the sample data in usable format?

 

lbendlin_0-1711404822964.png

 

Anonymous
Not applicable

idproductproduct_idamounts_id
100-4024449860301,910668506
100-4024449860301,910668868
100-4024449860301,910670033
100-4023063296200199334
100-4023063321202,71866829
100-4023063351205,878233
100-4023063537200,8199336
100-402306390621211889161
100-4023063997211,41984698
100-4023064113215,51992505
100-402306414321077493
100-40230641552110,910037981
100-40230641802118,41994162
100-4023064180212,72015940
100-4023064180223,22034646
100-4023064287221,310613124
100-4023064570221,7182786
100-4023064570221,7183063
100-4023064735225,51994150
100-4023064946224,610289201
100-4023065082224,910038215
100-4023065082221,710038252
100-4023065116220,410037979
100-4023065148220,7199364
100-4023065287220,6199356
100-4023065356222,210607848
100-4023065360221,2183745
100-4023065604200,610036460
100-4023065850210,634275

 

@lbendlin , can you pls try this data?

Your sample data only has Product IDs 2 and 3 but the DAX explicitly calls out IDs 0 and 1.  Please clarify.

 

How many rows are in your actual table?

 

lbendlin_0-1711413011147.png

This article is likely relevant for your issue:  https://www.sqlbi.com/articles/all-the-secrets-of-summarize/

 

Anonymous
Not applicable

well I have got performance issue and I have checked that article as well, but when i change it to addcolumns it is returning different value, so I want to optimize the query.

Doesn't this make more sense?

lbendlin_0-1711414826839.png

 

Anonymous
Not applicable

I have tried but it didnt return same value, do you think why?

Anonymous
Not applicable

Hi @lbendlin , I have fixed the sample data, can you try with new one, and my main point in the dax query is I want to find total sales based on id,product and during calculation there is if condition and it should provide different topn strategy according to product_id.

 

I still don't understand the business logic.  What are you trying to achieve?

 

lbendlin_0-1711413348539.png

 

Anonymous
Not applicable

Hi @lbendlin , I have tried but it looks messy again can you open the excel file below ?

 

https://drive.google.com/file/d/1w7YQPyczBTFPcszM8je4aAefhzNiarri/view?usp=drive_link

Wilson_
Memorable Member
Memorable Member

Hello sc6161,

 

Can you please provide a sample pbix file? (If you don't know how, please check out the pinned thread in the forum.)




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

Proud to be a Super User!





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.