How to Visualization percentage threshold relative to the average value

Visualization percentage threshold relative to the average value
Step 1 : Create table in postgresql and then insert data in it.



Step 2 : Now select stat visualizations to display data on grafana panel and then configure the stat visualizations .


Step 3 : Fetch data using these query mention below.
–Query
WITH metrics_with_stats AS (
SELECT
metric_name,
metric_value,
AVG(metric_value) OVER () AS avg_value
FROM metrics_table
)
SELECT
metric_name,
metric_value,
CASE
WHEN metric_value = 0 THEN ‘Red’ – Highlight 0 as Red
WHEN metric_value >= avg_value * 1.3 THEN ‘Yellow’ – 30% above the average
WHEN metric_value <= avg_value * 0.7 THEN ‘Pink’ – 30% below the average
ELSE ‘Green’ – Default color
END AS background_color
FROM metrics_with_stats;

Step 4 : Now Add transformation

Config from query results.


Configur the transformation setting and select the fields mention below.

Final output:

Highlight Zeros : Any row where metric_value = 0 must be highlighted with a red background.

Highlight High Values : Rows where metric_value is 30% or more above the average of all values should be highlighted with a yellow background.

Highlight Low Values : Rows where metric_value is 30% or more below the average of all values should be highlighted with a pink background.

Default Highlight : Rows that do not meet the above conditions should be highlighted with a green background.