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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It 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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors
Top Kudoed Authors