if then with contains function

I need to create a formula that evaluates a field based on the information contained. Example:
A field contains my search campaign for each product as well as an indicator if brand search:

Campaign:
[search] product a
[search] product b
[search] product c
[search] product d
[search][brand] product a
[search][brand] product b
[brand] product c
…and so forth.

I want to append a column that essentially says if the campaign field contains [search] AND [brand] then “branded search” else if campaign field contains [search] then “non brand search” else "something else (or keep going for the various types of breakouts). I need to make sure that not all fields return with the same output since all fields contain the word “[search]”

The above example table output would be:
Campaign: OUTPUT field:
[search] product a non brand search (since it ONLY contains search, not brand)
[search] product b non brand search (same as above)
[search] product c non brand search (same as above)
[search] product d non brand search (same as above)
[search][brand] product a brand search (contains search AND brand)
[search][brand] product b brand search (contains search AND brand)
[brand] product c something else since ONLY contains brand, not brand and
search, or search only for non brand (and alert that possibly
campaign was set up incorrectly).

I used column expression but getting an error:
if (contains(column(“Campaign name”),“[search]”)) AND (contains(column(“Campaign name”), “[brand]”)),
{“brand search”}
else if (contains(column(“Campaign name”),“[search]”)),
{“non brand search”}
else {“unknown”}

Chat GPT is pretty helpful in this regard - I asked CHATGPT for the help, it gave me three incorrect answers (had errors), and the third time gave me an answer that worked:

if (column(“Campaign name”).toLowerCase().indexOf(“[branded]”) !== -1 && column(“Campaign name”).toLowerCase().indexOf(“search”) !== -1) {
“branded search”;
} else if (column(“Campaign name”).toLowerCase().indexOf(“[search]”) !== -1) {
“non brand search”;
} else if (column(“Campaign name”).toLowerCase().indexOf(“[remarketing]”) !== -1) {
“remarketing”;
} else {
“something else”;
}

Hi @ebarr , it would help if you showed us what the error message says. Usually the error message would allow you to pinpoint what the issue is.

Quickly looking at your statements there, why are you adding a comma at the end of the conditions? That’s probably a syntax error:

if (contains(column("Campaign name"),"[search]")) AND (contains(column("Campaign name"), "[brand]")), <--
{"brand search"}
else if (contains(column("Campaign name"),"[search]")), <--
{"non brand search"}
else {"unknown"}

Try removing these commas, and it should give you:

if (contains(column("Campaign name"),"[search]")) AND (contains(column("Campaign name"), "[brand]"))
  {"brand search"}
else if (contains(column("Campaign name"),"[search]"))
  {"non brand search"}
else
  {"unknown"}

You should not longer get the errors

1 Like

Thank you. It was operator error in my end.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.