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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
IzuruWi
Frequent Visitor

Convert SQL case statement to DAX

Hi all,
I need some help with convert the below CASE statement to DAX query as a measure. Which should be able to execute in it's optimal perfomance state.

** I have mentioned the columns in the PBIX file for easier reference. 

CASE WHEN
DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)]
IN ('1 - CURRENT ASSETS','2 - NON - CURRENT ASSETS')
THEN SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] )
ELSE SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1 END

 

1 ACCEPTED SOLUTION

NewMeasure=IF(MAX(DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)]) IN {"1 - CURRENT ASSETS","2 - NON - CURRENT ASSETS"},SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1)

or 

NewMeasure=IF(SELECTEDVALUE(DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)]) IN {"1 - CURRENT ASSETS","2 - NON - CURRENT ASSETS"},SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1)

View solution in original post

6 REPLIES 6
wdx223_Daniel
Super User
Super User

NewTable=ADDCOLUMNS(VALUES(DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)]),"Total",IF(DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)] IN {'1 - CURRENT ASSETS','2 - NON - CURRENT ASSETS'},CALCULATE(SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] )),CALCULATE(SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] )) * -1))

@wdx223_Daniel Thank you very much for the quick response. Actually I need this to be used in a measure. So, how it should be changed? Sorry to bother you.

NewMeasure=IF(SELECTEDVALUE(DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)]) IN {'1 - CURRENT ASSETS','2 - NON - CURRENT ASSETS'},SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1)

Thank you very much @wdx223_Daniel. You have given measure works like a charm.
But the measure that you have been mentioned above returns the minus values instead of plus values. I understood it happened because of the value is multiplying from the minus 1.

I do have previously written measure by some other guy for the similar scenario.  But it doesn't returns the minus values as the above measure. However, It takes much longer time to execute. Highly appreciate if you can please help me to improve the below measure on performance based and find the reason for not returning the minus values?

 

MEASURE FACT_CONSOL_BALANCE_OL[Measure 4] =
        SWITCH (
            TRUE (),
            CONTAINS (
                DIM_ANALYTIC_STRUCT_ACCOUNT,
                DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)], "1 - CURRENT ASSETS"
            ), SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),
            CONTAINS (
                DIM_ANALYTIC_STRUCT_ACCOUNT,
                DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)], "2 - NON - CURRENT ASSETS"
            ), SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),
            SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1
        )

NewMeasure=IF(MAX(DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)]) IN {"1 - CURRENT ASSETS","2 - NON - CURRENT ASSETS"},SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1)

or 

NewMeasure=IF(SELECTEDVALUE(DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)]) IN {"1 - CURRENT ASSETS","2 - NON - CURRENT ASSETS"},SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1)

@wdx223_Daniel Thank you very much..

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Solution Authors
Top Kudoed Authors