Calculate difference from previous row

I would like to be able to have a panel that shows a change over time. My datasource is providing raw data, but I would like to have a column calculated that shows the difference between the current row and the row with the previous timestamp. Assuming my datasource doesn’t have a way to provide this value, is there a way to transform the data at the panel level to generate this value?

Hi @mmahacek1 welcome to the forum :grafana: :wave: ,

It’s possible you can do this in Grafana but it depends on your specific needs and what your data looks like / how it behaves. Can you share a screenshot of your table panel and more about your query and datasource?

You can add a column using the add field from calculation transformation.

I would check out the list of out-of-the-box calculations to see if any fit your needs. Although most deal with first / last values returned by a query, and not last / last - 1. Alternately, if last and last - 1 were in the same row, you could perform a binary operation, generating that unique calculation for each row, saved in a new field (column). Here is a dashboard JSON with an example using dummy data.

Try importing the JSON and let me know what you think. Make sure you have installed the TestData DB datasource that comes with Grafana :+1:

{
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "target": {
          "limit": 100,
          "matchAny": false,
          "tags": [],
          "type": "dashboard"
        },
        "type": "dashboard"
      }
    ]
  },
  "editable": true,
  "gnetId": null,
  "graphTooltip": 0,
  "id": null,
  "links": [],
  "panels": [
    {
      "datasource": "TestData DB",
      "fieldConfig": {
        "defaults": {
          "color": {
            "mode": "thresholds"
          },
          "custom": {
            "align": "auto",
            "displayMode": "auto"
          },
          "mappings": [],
          "thresholds": {
            "mode": "absolute",
            "steps": [
              {
                "color": "green",
                "value": null
              },
              {
                "color": "red",
                "value": 80
              }
            ]
          }
        },
        "overrides": []
      },
      "gridPos": {
        "h": 9,
        "w": 12,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "options": {
        "showHeader": true
      },
      "pluginVersion": "8.1.2",
      "targets": [
        {
          "csvContent": "previous value, new value\n1, 2\n2, 4\n4, 60\n60, 31\n31, 10",
          "refId": "A",
          "scenarioId": "csv_content"
        }
      ],
      "title": "Panel Title",
      "transformations": [
        {
          "id": "calculateField",
          "options": {
            "mode": "reduceRow",
            "reduce": {
              "reducer": "diff"
            }
          }
        }
      ],
      "type": "table"
    }
  ],
  "refresh": "",
  "schemaVersion": 30,
  "style": "dark",
  "tags": [],
  "templating": {
    "list": []
  },
  "time": {
    "from": "now-6h",
    "to": "now"
  },
  "timepicker": {},
  "timezone": "",
  "title": "New dashboard",
  "uid": null,
  "version": 0
}

The difference calculation doesn’t work off previous. I was able to find the datasource I’m working with does actually have a filter option to calculate the derivative/rate of change between rows, which is able to provide the value I’m looking for.
Thanks.