Grafana can perform calculations on your data, but it’s not a full-blown ETL tool; it’s a dashboarding tool that displays calculated data.

Let’s say you’re pulling metrics from Prometheus, and you’ve got http_requests_total and http_errors_total. You want to see the error rate.

First, here’s the raw data you might see in Grafana’s "Table" view:

Time http_requests_total http_errors_total
2023-10-27T10:00:00Z 1000 10
2023-10-27T10:01:00Z 1050 12
2023-10-27T10:02:00Z 1100 8

To calculate the error rate, you’d go to the "Transform" tab in your Grafana panel editor.

Click "Add transform" and select "Add field from calculation."

Under "Operation," choose "Binary operation."

For "Field a," select http_errors_total. For "Field b," select http_requests_total. For "Operation," choose "Divide."

This gives you a new field, let’s call it "Error Rate (Raw)".

Time http_requests_total http_errors_total Error Rate (Raw)
2023-10-27T10:00:00Z 1000 10 0.01
2023-10-27T10:01:00Z 1050 12 0.0114
2023-10-27T10:02:00Z 1100 8 0.0073

Now, what if you want to express this as a percentage? You can add another "Add field from calculation" transform.

For "Field a," select "Error Rate (Raw)." For "Field b," enter the literal value 100. For "Operation," choose "Multiply."

Let’s rename this new field to "Error Rate (%)".

Time http_requests_total http_errors_total Error Rate (Raw) Error Rate (%)
2023-10-27T10:00:00Z 1000 10 0.01 1
2023-10-27T10:01:00Z 1050 12 0.0114 1.14
2023-10-27T10:02:00Z 1100 8 0.0073 0.73

This is powerful because it means you don’t necessarily need to do this calculation in your data source. If your data source is slow or expensive to query, you can fetch raw numbers and transform them in Grafana.

There are many operations available: Add, Subtract, Multiply, Divide, Modulo, Power, Log, Log10, Floor, Ceil, Round, Abs, Sqrt, etc.

You can also use "Math" expressions, which are more like a mini-scripting language for calculations. This is useful for more complex scenarios.

For instance, to get the error rate as a percentage directly from the raw Prometheus metrics, you could use a "Math" transform with the expression: ($A / $B) * 100. Here, $A would refer to the first field from your query (e.g., http_errors_total) and $B to the second (e.g., http_requests_total).

The order of operations matters, and you can use parentheses. For example, to calculate the average of two different error rates, say from two different services, you might have fields $A and $B and use the expression ($A + $B) / 2.

One thing many people miss is that these transformations are applied sequentially. If you have multiple transforms, the output of the first one becomes the input for the second. This allows you to build up complex calculations step-by-step. For example, you could first divide errors by total requests, then multiply by 100 to get a percentage, and then add a constant offset if needed.

The final step after transforming data is usually to configure how it’s displayed in your chosen visualization (Graph, Table, Stat, etc.). You can set units, display names, and color schemes based on the transformed fields.

Once you’ve got your calculated fields looking right, you’ll want to explore how to use them in panel overrides for fine-grained control over visualization.

Want structured learning?

Take the full Grafana course →