After much research I have managed to develop this trick to be able to invoke a library panel (for example, table type), where the query uses one or more variables as filters in an optional way.
In this way I can use the same library panel from several dashboards in which I can use a filter for a tag (or several) without having to change the query.
So for example if I have 5 tags that I can search, I can generate a query that can use 1 to 5 variables each filtering by a different tag, so that in a dashboard I can have only 2 of the 5 possible filters defined, or all, or none (and show all the data).
The query would be like this (modify to suit needs):
SELECT …
FROM “measurement”
WHERE $timeFilter
AND (‘${var1:raw}’ =~ /^${var1:raw}$/ OR “tag1” =~ /^${var1}$/)
AND (‘${var2:raw}’ =~ /^${var2:raw}$/ OR “tag2” =~ /^${var2}$/)
AND (‘${var3:raw}’ =~ /^${var3:raw}$/ OR “tag3” =~ /^${var3}$/)
AND (‘${var4:raw}’ =~ /^${var4:raw}$/ OR “tag4” =~ /^${var4}$/)
AND (‘${var5:raw}’ =~ /^${var5:raw}$/ OR “tag5” =~ /^${var5}$/)
GROUP BY …
The operation is as follows. If any of the variables (var1 for example) does not exist, grafana will NOT replace ‘${var1:raw}’ with its respective value, performing a comparison with a regex that contains THAT unmodified string.
Otherwise, it enters the OR condition in which the tag is already evaluated with the value or values of said variable.