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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
kkishba2
Helper I
Helper I

Trying to return one value in IIF to achieve a Distinct (max value in status hierarchy)

The case statement below is producing the intended hierarchy, but I need to return only ONE of the values for a distinct status in my overall SQL statement (which is why I was trying to perform a nested IIF).

 

Case when se.STATUS in ('Final Reviewed','Reported') then 'Final Result'
when se.STATUS in ('Bench Reviewed', 'Peer Reviewed') then 'Preliminary/Discretionary Result'
when se.STATUS in ('Available', 'Batched','[Group]', 'Received', 'SubContracted') then 'Received/No Results'
when se.STATUS = 'Disapproved' then 'Disapproved'
else se.STATUS END AS ResultStatus,


IIF (ResultStatus ='Final Result', 'Final Result',
IIF(ResultStatus='Preliminary/Discretionary Result', 'Preliminary/Discretionary Result',
IIF(ResultStatus='Received/No Results', 'Received/No Results',
IIF(ResultStatus='Disapproved', 'Disapproved'))
, ResultStatus) as Status

 

I receive the following error message:

Msg 102, Level 15, State 1, Line 62
Incorrect syntax near ')'.

 

I am quite sure I have either too many or not enough parens to achieve a distinct value for "Status".

 

Can someone assist me - or point me in the right direction?

1 ACCEPTED SOLUTION
divyed
Super User
Super User

Hello @kkishba2 ,

 

I think there is small syntex error here. Can you try below code please :

 

IIF (ResultStatus = 'Final Result', 'Final Result',
IIF(ResultStatus = 'Preliminary/Discretionary Result', 'Preliminary/Discretionary Result',
IIF(ResultStatus = 'Received/No Results', 'Received/No Results',
IIF(ResultStatus = 'Disapproved', 'Disapproved', ResultStatus))))
AS Status

 

I hope this will solve your issue , If yes, please mark this as solution

 

Cheers

LinkedIn : https://www.linkedin.com/in/neeraj-kumar-62246b26/

View solution in original post

7 REPLIES 7
divyed
Super User
Super User

Hello @kkishba2 ,

 

I think there is small syntex error here. Can you try below code please :

 

IIF (ResultStatus = 'Final Result', 'Final Result',
IIF(ResultStatus = 'Preliminary/Discretionary Result', 'Preliminary/Discretionary Result',
IIF(ResultStatus = 'Received/No Results', 'Received/No Results',
IIF(ResultStatus = 'Disapproved', 'Disapproved', ResultStatus))))
AS Status

 

I hope this will solve your issue , If yes, please mark this as solution

 

Cheers

LinkedIn : https://www.linkedin.com/in/neeraj-kumar-62246b26/
kkishba2
Helper I
Helper I

I am attempting write a script to create a view in SSMS that I can include in my semantic model for reporting in Power BI.  Have I posted to the wrong forum?

 

yes, you have.

 

Anyway

but I need to return only ONE of the values for a distinct status in my overall SQL statement 

That's what CASE WHEN does - it exits after the first match, same like nested Ifs, or like SWITCH in Power BI.

 

Did you see an actual issue with the SQL code using CASE WHEN ?

kkishba2
Helper I
Helper I

My case statement is working as expected.  My IIF function is not working.

 

Where is the Power BI angle to your question?

kkishba2
Helper I
Helper I

Microsoft SSMS

I didn't include the entire SQL script - I am trying to narrow the return to only provide one "Status" statement, not each status throughout the lifecycle of an analyte test.

lbendlin
Super User
Super User

Which application/platform is this for?

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.