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

how to use CASE and SUM from sql server to powerbi

Hi everyone,

i'm trying to bring this query 

CASE sum(ANAR.SFRIDO)
when 0 then sum(Vendite.Cost)
else sum(Vendite.Cost+Vendite.Cost*ANAR.SFRIDO/100)

end

Vendite and ANAR are the table. Cost and SFRIDO are the columns

 I understood that i have to use SWITCH but i don't understand how to put togethere switch with sum and arithmetic operators of the original query. How can i transform this query that come from sql server to power bi?
Thank you a  lot !

1 ACCEPTED SOLUTION
Awm
Frequent Visitor

Measure_Name =

Var _SFRIDO = SUM(ANAR[SFRIDO])

Var _COST = SUM(Vendite[Cost])

 

return

IF(_SFRIDO = 0, _COST, _COST +_COST * _SFRIDO / 100

View solution in original post

4 REPLIES 4
Awm
Frequent Visitor

Please optimize the measure.

 

 

Measure_Name = 

Var _switchValue = SUM(ANAR[SFRIDO])

 

return

IF(_switchValue = 0, SUM(Vendite[Cost]), Vendite[Cost]) + MAX(Vendite[Cost]) * _SFRIDO / 100

 

Anonymous
Not applicable

Sorry, I had written the wrong table name by writing “max” but it was wrong, now I removed it and wrote the correct table name i.e. “Sales”. Why do you use the MAX function? Thank you

Awm
Frequent Visitor

Measure_Name =

Var _SFRIDO = SUM(ANAR[SFRIDO])

Var _COST = SUM(Vendite[Cost])

 

return

IF(_SFRIDO = 0, _COST, _COST +_COST * _SFRIDO / 100

Anonymous
Not applicable

Thank you, it seems working. 
What about this other query? I don' t understand how to arrasnge this one. Can you help me?

SQL SERVER:
sum(CASE WHEN anama.ppcoll is null
THEN 0
ELSE
case when anama.ppcoll <> 0
then Vendite."Quantità"/ anama.ppcoll
else 0
end
END)

Thanks!

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.