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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Anonymous
Not applicable

Using switch to find a test inside a text and give a category

Hi Gurus, 

 

I created  below formule, it alwasy return "YES" regardless. "Closed Order is a calculated column inside a tabel.

my 'SAP_WO'[PM_ORDER_SYSTEM_STATUS] columns data is like below

 

CLSD,NCMP,NMAT,PRC,SETC    Returns "Yes"  (correct result)

REL,ESTC,MACM,PRC,SETC         Returns "Yes" (wrong result, should be "No"

 

Can you please help me out.

 

 

"Closed Orders", SWITCH(
TRUE()
,ISERROR(SEARCH("CNF",'SAP_WO'[PM_ORDER_SYSTEM_STATUS])) , "Yes"
,ISERROR(SEARCH("TECO",'SAP_WO'[PM_ORDER_SYSTEM_STATUS])) , "Yes"
,ISERROR(SEARCH("CLSD",'SAP_WO'[PM_ORDER_SYSTEM_STATUS])) , "Yes"
,"No"
),
3 REPLIES 3
jdbuchanan71
Super User
Super User

You have to think about what the switch is doing.  It is finding the first TRUE() statement and returning the string.
ISERROR(SEARCH("CNF",'SAP_WO'[PM_ORDER_SYSTEM_STATUS])) , "Yes" is TRUE on every line that does not have "CNF" so it returns "YES".  

What you want is this

Closed Orders = 
SWITCH (
    TRUE (),
    NOT ISERROR ( SEARCH ( "CNF", 'SAP_WO'[PM_ORDER_SYSTEM_STATUS] ) ), "Yes",
    NOT ISERROR ( SEARCH ( "TECO", 'SAP_WO'[PM_ORDER_SYSTEM_STATUS] ) ), "Yes",
    NOT ISERROR ( SEARCH ( "CLSD", 'SAP_WO'[PM_ORDER_SYSTEM_STATUS] ) ), "Yes",
    "No"
)

NOT ISERROR means it found the text in the string.

Anonymous
Not applicable

@jdbuchanan71 , Thanks a lot for the explanation. 

Anonymous
Not applicable

Hi, 

 

Manged to get the desired result with CONTAINSTRING.

 

"Closed Orders", SWITCH(
TRUE()
,CONTAINSSTRING(SAP_WO[PM_ORDER_SYSTEM_STATUS],"CNF") , "Yes"
,CONTAINSSTRING(SAP_WO[PM_ORDER_SYSTEM_STATUS],"TECO") , "Yes"
,CONTAINSSTRING(SAP_WO[PM_ORDER_SYSTEM_STATUS],"CLSD") , "Yes"
,"No"
),

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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

Top Solution Authors