Using a custom variable in CASE Statement in a where clause

Hi,

I have a select query but i want to make the WHERE clause dynamic with different filtering based on the value of a variable.

This is currently what I have but it doesn’t work - am I using the CASE statement in the wrong way?

WHERE CASE WHEN $ReportType = ‘A’ THEN Filterfield1 = (‘FieldName’)
WHEN $ReportType = ‘B’ THEN FilterField2 = (‘OtherFieldName’)
END

You didn’t disclose what kind of query language are you using, but I will be surprised if it support CASE expression in the WHERE statement (for example SQL supports that in SELECT statement).

Use simple key/value dashboard variable, e.g.
wherecondition:
A: Filterfield1 = (‘FieldName’)
B: FilterField2 = (‘OtherFieldName’)

So user will be able to choose from UI selectbox A or B (don’t allow multivalue selection)

And then use it in the panel query, e.g.: WHERE ${wherecondition:raw}. All are just examples, not copy&pase solutions, so fix any syntax/functional issues.
`

1 Like

Hi, Good idea i’ll use that thanks