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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
kevinsray
Helper II
Helper II

SQL CASE to DAX

I have been trying to convert this SQL case to Dax to work in Power Bi, but just can`t seem to find the right formula.
SELECT TOP(100) STOCKCODE, SUM(QUANTITY) as 'QUANTITY'
FROM (SELECT DI.STOCKCODE,
(CASE WHEN SI.CHKQTY = 'Y' THEN QUANTITY * NITS_PER_CARTON
ELSE QUANTITY
END) AS QUANTITY
FROM DI
JOIN SI
ON SI.STOCKCODE = DI.STOCKCODE

 

Any hints would be greatly appreciated.

2 REPLIES 2
v-xicai
Community Support
Community Support

Hi @kevinsray ,

 

You can create a calculated table Table1 first of all, then add index column in the Table1, and then create measure Sum for Quantity .

 

Table1 = SELECTCOLUMNS(FILTER(DI,DI[STOCKCODE]=MAX(SI[STOCKCODE])),"STOCKCODE",DI[STOCKCODE],"QUANTITY",IF(MAX(SI[CHKQTY] )= "Y",[QUANTITY] * [NITS_PER_CARTON],[QUANTITY] ))

 

1.png

 

 

 

 

 

Sum for Quantity = CALCULATE(SUM(Table1[QUANTITY]),FILTER(Table1,Table1[Index]<=100))

 

Best Regards,

Amy

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Amy,

Thank you for your help.

I am using the formula to create the table, but one small issue.

This QUANTITY * NITS_PER_CARTON calculation is actually from 2 different tables.

So, I guess I should have written DI.QUANTITY * SI.NITS_PER_CARTON


Is there any way to get around this?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.