Skip to content

[FAQ] Why do Grafana pie charts show n instead of relevance or feedback categories? #376

Description

@yazdanparasthesam

Course

llm-zoomcamp

Question

Why do Grafana pie charts show n or only one feedback slice even when the PostgreSQL query returns separate relevance or feedback categories?

Answer

Add a Rows to fields transformation so each category becomes its own named numeric field before the pie chart reduces the data.

In the panel editor:

Set the PostgreSQL query's Format to Table. Confirm that it returns one row per category, with a text category column and a numeric count column.
Open Transformations → Add transformation → Rows to fields.
In the field mapping table, set the category column, such as feedback, to Use as → Field name.
Set the count column, such as n, to Use as → Field value.
Use the resulting fields in the pie chart. With a single count per field, Calculate → Last (not null) retains each category separately. Enable the legend's names and values to check the result.
For example, this read-only PostgreSQL query provides two categories without requiring an existing table:

WITH feedback_counts(feedback, n) AS (
    VALUES ('thumbs up', 3), ('thumbs down', 2)
)
SELECT feedback, n
FROM feedback_counts;

The equivalent transformation in a provisioned panel definition is:

{
  "transformations": [
    {
      "id": "rowsToFields",
      "options": {
        "mappings": [
          {"fieldName": "feedback", "handlerKey": "field.name"},
          {"fieldName": "n", "handlerKey": "field.value"}
        ]
      }
    }
  ]
}

Merge this property into the relevant panel; it is not a complete dashboard definition. For a relevance chart, replace feedback with the query's relevance column.

Why this happens: SQL output can contain several rows in one numeric field named n. Reducing that field to its last value can produce one slice, rather than one slice per category. Renaming its display label alone does not separate the category values. Rows to fields preserves a separate numeric field for each category.

Validation: the example should display thumbs up: 3 and thumbs down: 2, totaling 5 votes, not one slice labelled n. For real telemetry, compare against the SQL aggregates from the same database and time range. This transformation does not populate an empty query result.

This configuration was verified with Grafana 10.2.3 and a PostgreSQL datasource during an LLM Zoomcamp capstone.

References:

Grafana: Rows to fields transformation
Grafana 10.2.3 transformation implementation

Checklist

  • I have searched existing FAQs and this question is not already answered
  • The answer provides accurate, helpful information
  • I have included any relevant code examples or links

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions