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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

NESTED IF

NESTED WANDING.PNG

 

 

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] = "M" && [OPDSC] = 2SH,2LG THEN [RESULT] = "M"

2)IF [CODE] = "M" && [OPDSC] <> 2SH,2LG THEN CHECK CURRENT ROW [OPDSC] WITH PREVIOUS ROW [OPDSC] IF IT IS SAME THEN  [RESULT] = "M/A/B"

3)IF [CODE] = "M" && [OPDSC] <> 2SH,2LG THEN CHECK CURRENT ROW [OPDSC] WITH PREVIOUS ROW [OPDSC] IF IT IS NOT SAME THEN  [RESULT] = "A/B"

4)IF [CODE] <> M THEN [RESULT] = USE CORRESPONDING ROWS[CODE]

1 ACCEPTED SOLUTION
Anonymous
Not applicable

@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

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

@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

Anonymous
Not applicable

Thank you soo much, Chris, it worked perfectly. Now I understand the switch statement better.👍

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.