Variables: how to conditionally add trailing comma

  • What Grafana version and what operating system are you using?
    v10.4.2, Linux

  • What are you trying to achieve?
    Group rows by columns specified in Grafana’s custom multi-value variable.

  • How are you trying to achieve it?

SELECT
    ${group_by:doublequote},
    ...
GROUP BY
    ${group_by:doublequote},
    ...
  • What happened?
    Everything works as expected except in a case when no group_by values are selected.
    In this case I have "", which is not a valid SQL.

  • What did you expect to happen?
    Have a possibility to conditionally add trailing comma to variable expansion - either by some filter or by templating like $(if ) in Makefiles:

${group_by:doublequotescomma} -> "foo","bar",
${group_by:doublequotescomma} -> "foo",
${group_by:doublequotescomma} ->                    // nothing at all


${if $group_by,${group_by:doublequotes} + ",",}

If there are no means for it in Grafana, then probably someone could point me how to overcome this limitation using SQL?

do an if statement in the query section to check value of $group_by proper postgresql if statement
I made the All value -1

1 Like