Using a panel variable in a complex select clause

Hi all,

I am pretty new to Grafana, and struggling to create a dashboard template. I have a “power” measurement with field keys that I don’t know in advance, and I want the user to be able to graph one or more of those fields on a graph.
I have created a dashboard query variable to allow the user to select the fields he wants to see:

$fkeys = query[ show field keys from "power" ]

Assuming the user has select “fridge” and “heater”, the graph query I am trying to create is this:

select mean(fridge) as fridge, mean(heater) as heater from power group by time(1m)

I have tried to create a second, hidden variable that would hold the

“mean(<$fkey>) as $fkey”

string for each select field key, but I didn’t go anywhere… is there any other way to achieve this?

Any suggestion welcome!

I just found this, which works acceptably though the labels end up as “mean_heater” and “mean_fridge”:

select mean(/^${fkeys:regex}$/) from power group by time(1m)