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
monojchakrab
Resolver III
Resolver III

Switch and Containsstring not replacing all values as required

Hiya All,

 

I am coming across a strange problem while using the SWITCH function with CONTAINSSTRING and the code is as below :

 

Size = 

SWITCH(TRUE(),
    CONTAINSSTRING('summary table'[Title],"100")||CONTAINSSTRING('summary table'[Title],"110"),
    "100",
    CONTAINSSTRING('summary table'[Title],"300"),
    "300",
    CONTAINSSTRING('summary table'[Title],"50"),
    "50",
    CONTAINSSTRING('summary table'[Title],"Jar"),
    "80g",
    CONTAINSSTRING('summary table'[Title],"Pouch"),
    "500g",
blank()
)

 

While it is doing all replacements as per the requirement, its just the lines which have "pouch" in the title, it is just not returning the target value I want in the column[size]. I have tried with all possible unique key word searches in the [Title] for this particular product/value.

Screengrab below :

not returning the target valuenot returning the target value

 

Here's the title :

monojchakrab_0-1652098600923.png

The target is to return the [size] as 500 gm if the [title] contains "Pouch"...

 

Am I missing something in my code?

 

Appreciate the help.

 

Best

 

 

2 ACCEPTED SOLUTIONS
AntonioM
Solution Sage
Solution Sage

Hi, 

 

The titles with 'pouch' also have '50' in them (because of the 500g), so your SWITCH statement is picking that up first, then putting '50' in the size column. 

 

SWITCH always goes through each of the statements until it finds a match, so if you put the 'pouch' option first, then you shouldn't have this problem (As long as none of the other titles just so happen to have 'pouch' in).

 

Alternatively, you could also try changing the 50 to '50g', as this wouldn't pick up the 500.

View solution in original post

2 REPLIES 2
AntonioM
Solution Sage
Solution Sage

Hi, 

 

The titles with 'pouch' also have '50' in them (because of the 500g), so your SWITCH statement is picking that up first, then putting '50' in the size column. 

 

SWITCH always goes through each of the statements until it finds a match, so if you put the 'pouch' option first, then you shouldn't have this problem (As long as none of the other titles just so happen to have 'pouch' in).

 

Alternatively, you could also try changing the 50 to '50g', as this wouldn't pick up the 500.

Thanks @AntonioM ...gotcha!

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.