Calculating Power Factor using Query Language

Hi there! I’m running Grafana V7 with influxDB V1.8. Trying to do some math inside the query to show power factor of an energy installation. These are the variable I currently have:

Real Power = P =“shellyem-BXXXX” = ‘shellies/shellyem-B9FXXX/emeter/0/power’
Reactive Power = Q = “shellyem-BXXXX” = ‘shellies/shellyem-B9FXXX/emeter/0/reactive_power’

By combining these two variables I can get the power factor using the power triangle,

acp305

Power Factor = cos (Φ) = P / S = P / sqrt (P^2 + Q^2)
where S^2 = P^2 + Q^2; S = sqrt(P^2 + Q^2)

This is an example of two queries that show Real Power and Reactive Power:

SELECT mean("value") FROM "mqtt_consumer" WHERE ("shellyem-BXXXX" = 'shellies/shellyem-B9FXXX/emeter/0/power') AND $timeFilter GROUP BY time(1m) fill(null)

SELECT mean("value") FROM "mqtt_consumer" WHERE ("shellyem-BXXXX" = 'shellies/shellyem-B9FXXX/emeter/0/reactive_power') AND $timeFilter GROUP BY time(1m) fill(null)

How do I combine these two queries (P & Q) to perform the following operation; P / sqrt (P^2 + Q^2) = Power Factor. Is it possible with a Query?

Thanks in advance for your support!