From ffd8dd227efad2e6439b5156559fc6e3cb3b047f Mon Sep 17 00:00:00 2001 From: Daniel Vagie Date: Mon, 16 Sep 2024 11:22:08 -0400 Subject: [PATCH 1/5] Update dashboard page with jinja-templating --- docs/app/visualization/Dashboards.mdx | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/docs/app/visualization/Dashboards.mdx b/docs/app/visualization/Dashboards.mdx index a4d020f4..78fcbbf4 100644 --- a/docs/app/visualization/Dashboards.mdx +++ b/docs/app/visualization/Dashboards.mdx @@ -101,6 +101,61 @@ can be filtered on, sorted by, and/or used to categorize data. Dashboard - Calculated Column +## Adding Jinja Templating to SQL Queries + +There are two ways to use Jinja templating in SQL queries: + +1. [Virtual Quaries](#adding-virtual-datasets) +2. Metrics + +### Virtual Queries + +A virtual query can be constructed to use Jinja templating using: + +```sql +{% set variable_name = filter_values('some_column_name')[0] if filter_values('some_column_name') else 1 %} + +SELECT + *, + {{ variable_name }} AS inherited_filter_value, + value * {{ variable_name }} AS value_multiplied_by_inherited_filter_value +FROM table_name +``` + +Where a variable is set by calling the filter_values function and returning the first value (since +it always returns a list of selected values). The if statement checks to see a filtered value was +selected on column `some_column_name` and returns 1 otherwise. This assigns a default value to +the query so that the dataset can be created and not raise any errors if there are no active +filtered values present on that column. A dataset will not be savable without the if statement. + +### Metrics +Creating a chart, a line chart for instance, requires creating a metric on that variable. You can +use filter values by either referencing the new column created in a virtual query or by making the +metric as follows: + +```sql +AVG(value * {{ filter_values('some_column_name')[0] if filter_values('some_column_name') else 1}}) +``` + +Where this mimics what was done in the virtual dataset above but instead works for both virtual and +physical datasets. The if statement is required here as well in order for the chart to be displayed +when a filter is not selected on `some_column_name`. + + +### Notes + +Its important to make sure that you have +1. if statements to check if a filter value is present +2. You are referencing the column name that is being filtered on in the dashboard + a. For instance you have a filter defined in the dashboard named `My Filter` created on dataset +`my_dataset` on column `some_column_name` where `some_column_name` is referenced in the Jinja +template +3. The column name is not contained in the dataset you are using the Jinja templating in (unless you actually want your chart to be filtered as well) +4. You may need to wrap your Jinja template in a CAST to convert to INT or NUMERIC if necessary +5. Other useful links for more detail or other use cases of Jinja: + a. [From Preset website](https://preset.io/blog/intro-jinja-templating-apache-superset/) + b. [From Apche Superset website](https://superset.apache.org/docs/configuration/sql-templating/) + ## Editing dashboards To edit a dashboard: From 54276d5a6e61221aff18fff2faf24ffb71d29734 Mon Sep 17 00:00:00 2001 From: Daniel Vagie Date: Mon, 16 Sep 2024 11:30:57 -0400 Subject: [PATCH 2/5] Added additional text, some spell check fixes --- docs/app/visualization/Dashboards.mdx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/docs/app/visualization/Dashboards.mdx b/docs/app/visualization/Dashboards.mdx index 78fcbbf4..88e4a5b3 100644 --- a/docs/app/visualization/Dashboards.mdx +++ b/docs/app/visualization/Dashboards.mdx @@ -101,16 +101,17 @@ can be filtered on, sorted by, and/or used to categorize data. Dashboard - Calculated Column -## Adding Jinja Templating to SQL Queries +## Adding Jinja Templates to SQL Queries -There are two ways to use Jinja templating in SQL queries: +There are two ways to use Jinja templates in SQL queries: -1. [Virtual Quaries](#adding-virtual-datasets) +1. [Virtual Queries](#adding-virtual-datasets) 2. Metrics -### Virtual Queries +### Example Using Virtual Queries -A virtual query can be constructed to use Jinja templating using: +An example virtual query that inherits inputs from a filter can be constructed to use Jinja +templates using: ```sql {% set variable_name = filter_values('some_column_name')[0] if filter_values('some_column_name') else 1 %} @@ -127,8 +128,11 @@ it always returns a list of selected values). The if statement checks to see a f selected on column `some_column_name` and returns 1 otherwise. This assigns a default value to the query so that the dataset can be created and not raise any errors if there are no active filtered values present on that column. A dataset will not be savable without the if statement. +After saving the query to a virtual dataset, any chart in a dashboard will have the Jinja template +automatically be rendered. -### Metrics + +### Examples Using Metrics Creating a chart, a line chart for instance, requires creating a metric on that variable. You can use filter values by either referencing the new column created in a virtual query or by making the metric as follows: @@ -150,11 +154,11 @@ Its important to make sure that you have a. For instance you have a filter defined in the dashboard named `My Filter` created on dataset `my_dataset` on column `some_column_name` where `some_column_name` is referenced in the Jinja template -3. The column name is not contained in the dataset you are using the Jinja templating in (unless you actually want your chart to be filtered as well) +3. The column name is not contained in the dataset you are using the Jinja template in (unless you actually want your chart to be filtered as well) 4. You may need to wrap your Jinja template in a CAST to convert to INT or NUMERIC if necessary 5. Other useful links for more detail or other use cases of Jinja: a. [From Preset website](https://preset.io/blog/intro-jinja-templating-apache-superset/) - b. [From Apche Superset website](https://superset.apache.org/docs/configuration/sql-templating/) + b. [From Apache Superset website](https://superset.apache.org/docs/configuration/sql-templating/) ## Editing dashboards From c453c4badb55b1e016a79eeece7b472155d33cf4 Mon Sep 17 00:00:00 2001 From: Daniel Vagie Date: Mon, 16 Sep 2024 11:46:13 -0400 Subject: [PATCH 3/5] Update wordslist --- .wordlist.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.wordlist.txt b/.wordlist.txt index f1bdec82..af96e9d0 100644 --- a/.wordlist.txt +++ b/.wordlist.txt @@ -289,3 +289,6 @@ webhook AIA CDF MockGanymedeContext +Jinja +subsetting +Superset From 1ba8c26aeb845d01b1b3cde04d31a979ad32bc18 Mon Sep 17 00:00:00 2001 From: Daniel Vagie Date: Mon, 16 Sep 2024 11:47:14 -0400 Subject: [PATCH 4/5] Update wordslist --- .wordlist.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.wordlist.txt b/.wordlist.txt index af96e9d0..4d42f5d3 100644 --- a/.wordlist.txt +++ b/.wordlist.txt @@ -292,3 +292,4 @@ MockGanymedeContext Jinja subsetting Superset +DashboardFilterIcon From 9a8d3d1e56dfbfe4d040f31e8ebae6b789a38d20 Mon Sep 17 00:00:00 2001 From: Benson Lee Date: Mon, 16 Sep 2024 12:21:40 -0700 Subject: [PATCH 5/5] cleanup --- docs/app/visualization/Dashboards.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/app/visualization/Dashboards.mdx b/docs/app/visualization/Dashboards.mdx index 88e4a5b3..2347c71e 100644 --- a/docs/app/visualization/Dashboards.mdx +++ b/docs/app/visualization/Dashboards.mdx @@ -103,7 +103,7 @@ can be filtered on, sorted by, and/or used to categorize data. ## Adding Jinja Templates to SQL Queries -There are two ways to use Jinja templates in SQL queries: +There are two ways to use [Jinja templates](https://jinja.palletsprojects.com/) in SQL queries: 1. [Virtual Queries](#adding-virtual-datasets) 2. Metrics @@ -123,7 +123,7 @@ SELECT FROM table_name ``` -Where a variable is set by calling the filter_values function and returning the first value (since +where a variable is set by calling the _filter_values_ function, a function native to the dashboarding environment, and returning the first value (since it always returns a list of selected values). The if statement checks to see a filtered value was selected on column `some_column_name` and returns 1 otherwise. This assigns a default value to the query so that the dataset can be created and not raise any errors if there are no active