In looker calculation I’d like to combine IF and OR.
For example given a list of groceries like
apple
pear
bean
radish
sponge
orange
lettuce
I’d like to automatically identify as a fruit, vegetable or other
i.e.
if grocery = “apple” OR “pear” OR “orange” then return “fruit”
if grocery = “bean” OR “radish” OR “lettuce” then return “vegetable”
otherwise return “other”
I have no SQL experience, limited Looker and some Excel.
Sorry such a basic question
Sounds like a perfect fit for a CASE statement.
If you’re not too familiar with the SQL syntax for this, there’s an easy LookML version:
https://docs.looker.com/reference/field-params/case
for your fruit example, assuming “grocery” is a dimension already, it might look something like:
dimension: grocery_classified {
case: {
when: {
sql: ${grocery} = 'apple' OR ${grocery} = 'pear' OR ${grocery} = 'orange';;
label: "fruit"
}
when: {
sql: ${grocery} = 'beans' OR ${grocery} = 'radish' OR ${grocery} = 'lettuce' ;;
label: "vegetable"
}
else: "tofu"
}
}