Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

NESTED IF

    Hello, I am stuck with a nested if condition problem. The Result table is what I am trying to create in powerbi. Can anyone help me out with this, thanks in advance. LOGIC: 1)IF [CODE] ...
  • Anonymous's avatar
    Anonymous
    6 years ago

    Anonymous , try this calculated column:

     

    DAX Column = 
    VAR Current_Index = TableName[Index]
    VAR Prev_Index = Current_Index - 1
    VAR Prev_OPDSC = 
    	CALCULATE(
    		MIN(TableName[OPDSC])
    		,ALL(TableName)
    		,TableName[INDEX] = Prev_Index
    	)
    VAR Result = 
    	SWITCH( TRUE()
    		,TableName[CODE] = "M" 
    			&& TableName[OPDSC] = "2SH,2LG"
    				, "M"
    		,TableName[CODE] = "M" 
    			&& TableName[OPDSC] <> "2SH,2LG"
    			&& TableName[OPDSC] = Prev_OPDSC
    				, "M/A/B"
    		,TableName[CODE] = "M" 
    			&& TableName[OPDSC] <> "2SH,2LG"
    			&& TableName[OPDSC] <> Prev_OPDSC
    				, "A/B"
    		,TableName[CODE] <> "M"
    				,TableName[CODE]
    	)
    RETURN
    Result

     

    This code first determines what the previous index is, and gets the [OPDSC] for that row.

     

    The SWITCH() TRUE() pattern lets you evaluate multiple statements, and returns the first result that is true.

     

    Just make sure to replace all instances of "TableName" with the name of your table!

     

    Cheers,

     

    ~ Chris