Forum Discussion

BBIUser's avatar
BBIUser
Icon for Helper IV rankHelper IV
7 years ago
Solved

How to use this CASE statement to DAX or to a Custom Column?

Hi,   Having trouble to get the result out of these CASE statements. Sample data is attached below.   1) sum(case when Column1='Yes' or Column2='Yes')then 1 else 0 end) TotalValBranches 2...
  • MFelix's avatar
    MFelix
    7 years ago
    Hi BBIUser,

    You are correct this ask for a table if want to apply any filters you should wrap your column in a ALLSELECTED if you want an overall result the use the ALL function.

    In the case the measure would be something like this:

    TotalValBranches = calculate (countrows(ALLSELECTED(Table[Current]); Table[Column1] ='Yes' || Table[Column2] ='Yes')

    This should work. Then just divide 1 measure by the other to get the percentage.

    Regarding the columns vs measure best practices on DAX says thst if you can calculate it making a measure don't use columns.

    Columns add size to your model and decrease performance since you are adding values to your model, assuming you table as 100.000 rows adding one column is adding 100.000 additional data cells.
    Measures are only calculated when they are called and also store on cache for next uses.

    Regards
    MFelix
  • v-lili6-msft's avatar
    v-lili6-msft
    7 years ago

    hi, BBIUser

    Your formulas are M language in the Edit Queries.

    For creating the calculate column by DAX 

    You could click Modeling ->New Column

    Then use these formulae to create three column

     

    TotalValBranches = IF([Column1] = "Yes" || [Column2] = " Yes", 1,  0)
    TotalAttBranches = if (([Column1] = "Yes" || [Column2]= "Yes") && [Current]=0 && [Expected]=0, 1 , 0)
    TotalChBranches = IF([Column3] = "Yes",  1 , 0)

    Comparison

    And then add a measure for TotalValBranches/TotalAttBranches by DIVIDE Function

    TotalValBranches/TotalAttBranches =
    DIVIDE (
        CALCULATE ( SUM ( 'Table'[TotalValBranches] ) ),
        CALCULATE ( SUM ( 'Table'[TotalAttBranches] ) ),
        0
    )

    and drag it into card visual.

     

    also, you could just create a measure like below:

    Measure = 
    DIVIDE (
        CALCULATE (
            COUNTROWS ( 'Table' ),
            FILTER ( 'Table', [Column1] = "Yes" || [Column2] = " Yes" )
        ),
        CALCULATE (
            COUNTROWS ( 'Table' ),
            FILTER (
                'Table',
                ( [Column1] = "Yes"
                    || [Column2] = "Yes" )
                    && [Current] = 0
                    && [Expected] = 0
            )
        ),
        0
    )

    Columns add size to your model and decrease performance since you are adding values to your model and they are static.

    Measures are only calculated when they are called and also store on cache for next uses and they are dynamic.

     

    here is my pbix, please try it.

    https://www.dropbox.com/s/sl6bmsjw2302ihg/How%20to%20use%20this%20CASE%20statement%20to%20DAX%20or%20to%20a%20Custom%20Column.pbix?dl=0

     

    Best Regards,

    Lin