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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Jensej
Helper V
Helper V

CASE WHEN THEN in Measure

Hello everyone! 

 

Trying to create a measure with the IF or Switch function but can't make it work. 

 

Can someone please help me to rewrite this so it works in a measure:

 

SUM(
CASE WHEN a.accountNr1 = b.accountNr THEN Amount ELSE 0 END
-
CASE WHEN a.accountNr2 = b.accountNr Then Amount ELSE 0 END) * - 1 as Total
1 ACCEPTED SOLUTION

@Jensej 

Can you try the following formula:

 

Measure = (
SUMX (
    Sheet1,    -- Table Name
    IF (
        Sheet1[accountNr1] = Sheet1[accountNr],
        CALCULATE(SUM ( Sheet1[Amount ] )),
        0)
    )
)
-
SUMX (
    Sheet1,    -- Table Name
    IF (
        Sheet1[accountNr2] = Sheet1[accountNr],
        CALCULATE(SUM ( Sheet1[Amount ] )),
        0)
    )
))*-1

 

View solution in original post

7 REPLIES 7
amitchandak
Super User
Super User

@Jensej , You can try a measure like

SUMX(filter(a[accountNr1] = max(b[accountNr])) , a[Amount]) - (SUMX(filter(a[accountNr2] = max(b[accountNr])) , a[Amount])*-1)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thanks for your reply but i don't get the correct result. Are you sure about the code? Why use SUMX and not just SUM and why MAX?

@Jensej ,Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

 

 

Please provide your feedback comments and advice for new videos
Tutorial Series Dax Vs SQL Direct Query PBI Tips
Appreciate your Kudos.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@Jensej 

Can you try the following formula:

 

Measure = (
SUMX (
    Sheet1,    -- Table Name
    IF (
        Sheet1[accountNr1] = Sheet1[accountNr],
        CALCULATE(SUM ( Sheet1[Amount ] )),
        0)
    )
)
-
SUMX (
    Sheet1,    -- Table Name
    IF (
        Sheet1[accountNr2] = Sheet1[accountNr],
        CALCULATE(SUM ( Sheet1[Amount ] )),
        0)
    )
))*-1

 

I made like this in end at it worked. 

 

Measure = 
VAR _A = 
SUMX (
    Sheet1,    -- Table Name
    IF (
        Sheet1[accountNr1] = Sheet1[accountNr],
        CALCULATE(SUM ( Sheet1[Amount ] )),
        0)
    )
VAR _B = 
SUMX (
    Sheet1,    -- Table Name
    IF (
        Sheet1[accountNr2] = Sheet1[accountNr],
        CALCULATE(SUM ( Sheet1[Amount ] )),
        0)
    )

Return ( _A - _B ) * 1

Thanks for your reply i will try but it seems there's a problem with to many ))

 

🙂

Hi @Jensej ,

 

If the problem persists,could you share the sample data or sample pbix and the result you expected?
Please mask any sensitive data before uploading

 

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

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.

Top Solution Authors
Top Kudoed Authors