How to create grafana dashboard and filter data using variables

Step 1: Create table in postgresql and insert data in it to visualize on dashboard panel.


Step 2: Create new dashboard and select table visualization

Step 3 : Add variables to filter the data

Create the Variables in Grafana

To filter and visualize your fruit data, you’ll need to create variables for dynamic dropdowns (Fruit Type, Fruit Subtype, and Fruit Details).

3.1 Create Level 1 Variable (Fruit Type)

  1. Go to Dashboard SettingsVariablesAdd Variable.
  2. Configure the following:
  • Name : Level_1_Fruit
  • Type : Query
  • Data Source : Select your PostgreSQL data source.
  • Query :

sql

CopyEdit

SELECT DISTINCT “level_1_fruit_information” FROM fruits

  1. Refresh : On Dashboard Load
  2. Include All option : Enabled (if you want to allow “All” option).
  • Click Save.

3.2 Create Level 2 Variable (Fruit Subtype)

  1. Go to Dashboard SettingsVariablesAdd Variable.
  2. Configure the following:
  • Name : Level_2_Fruit
  • Type : Query
  • Data Source : Select your PostgreSQL data source.
  • Query :

sql

CopyEdit

SELECT DISTINCT “level_2_fruit_information” FROM fruits

WHERE “level_1_fruit_information” = ‘$Level_1_Fruit’

  1. Refresh : On Variable Change
  2. Include All option : Enabled (optional).
  • Click Save.
  • 3.3 Create Level 3 Variable (Fruit Details)
  1. Go to Dashboard SettingsVariablesAdd Variable.
  2. Configure the following:
  • Name : Level_3_Fruit
  • Type : Query
  • Data Source : Select your PostgreSQL data source.
  • Query :

sql

CopyEdit

SELECT DISTINCT “level_3_fruit_information” FROM fruits

WHERE “level_2_fruit_information” = ‘$Level_2_Fruit’

  1. Refresh : On Variable Change
  2. Include All option : Enabled (optional).
  • Click Save.
  • Step 4 : Fetch data using these query to show data on grafana


Final output