# How to Implement Moving Averages in Grafana Dashboards

To add moving averages to your Grafana dashboards using popular data sources like Prometheus and InfluxDB.


Begin by accessing your Grafana dashboard. To incorporate a moving average into your visualization, click the **Menu** button and select **Edit** to modify your current dashboard.

![Create or Edit Panel](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/836886e0-e6f9-450a-36e8-5e276a725f00/md2x=3024x1490)

Ensure you choose the correct data source from the dropdown menu. **Prometheus** and **InfluxDB** are widely favored for implementing moving averages due to their robust support for time-series data.

![Choose Data Source](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/0139afef-bcaa-4a25-8a63-ccbc1ec0bf00/md1x=3024x1528)

## Moving Averages with Prometheus

Prometheus offers the `avg_over_time()` function, which effectively serves the purpose of calculating moving averages. While it doesn't have a dedicated `moving_average()` function, `avg_over_time()` allows you to compute the average of a metric over a specified time window.

For example, to calculate a 5-minute moving average of HTTP requests for the `frontend` job, you can use the following Prometheus query:

```prometheus
avg_over_time(rate(http_requests_total{job="frontend"}[1m])[5m:])
```

Prometheus does not natively support more sophisticated moving averages like Weighted Moving Averages (WMA) or Exponential Moving Averages (EMA). Consider leveraging Grafana's transformation features or integrating with InfluxDB for these advanced calculations.



## Using the `moving_average()` Function in InfluxDB

To implement a moving average in InfluxDB, you can leverage the `moving_average()` function directly within your InfluxQL queries. This function simplifies the process of smoothing out data by calculating the average over a specified number of data points, effectively highlighting long-term trends while minimizing short-term fluctuations.

The basic syntax for the `moving_average()` function is as follows:

```influxql
moving_average(metric, windowSize)
```


Suppose you want to monitor the memory usage of your application servers to identify long-term usage patterns and mitigate short-term spikes. Calculating a 15-point moving average can provide a clearer view of memory usage trends. Here's how you can structure your InfluxQL query:

```influxql
SELECT moving_average(mean("memory_usage"), 15) 
FROM "system_metrics" 
WHERE time > now() - 3h 
GROUP BY time(10m)
```

By effectivelyusing the `moving_average()` function in InfluxDB and adhering to these best practices, you can create insightful and responsive Grafana dashboards. 

[ad-uptime]