This is an advanced topic that assumes a good, pre-existing knowledge of SQL and LookML.
Looker automatically provides users with the ability to manipulate their queries by creating filters, which are based on dimensions and measures. While this simple method meets many use cases, it can’t enable every analytical need. Templated filters and Liquid parameters vastly expand the possible use cases you can support.
From a SQL perspective, dimensions and measures can only alter the outermost
HAVING clauses in your query. However, you might find that you want to let users manipulate other parts of the SQL. Adjusting part of a derived table, adjusting which database table gets queried, or creating multipurpose dimensions and filters are just some of the features you can enable with templated filters and Liquid parameters.
Templated filters and Liquid parameters make use of the Liquid templating language to insert user input into SQL queries. First, you use a LookML parameter to create a field for users to interact with. Next, you use a Liquid variable to inject the user input into SQL queries.
Let’s look at a few examples to demonstrate the value of templated filters and Liquid parameters.
Making a Dynamic Derived Table with a Templated Filter
Consider a derived table that calculates a customer’s lifetime spend, within the northeast region:
In this query, you can create dimensions from
lifetime_spend. However, suppose you wanted the user to be able to specify the
region, instead of hard-coding it to “northeast”. The
region cannot be exposed as a dimension, and therefore the user cannot filter on it as normal.
One option would be to use a templated filter, which would look like this:
Read more below for step-by-step instructions.
A derived table cannot be persisted if it makes use of a templated filter. There are potentially an infinite number of possible user inputs, so the number of persistent tables in your database could become unmanageable.
Making a Dynamic Measure with a Liquid Parameter
Consider a filtered measure that adds up the number of pants sold:
This is straightforward, but if there were dozens of categories, it would be tedious to create a measure for each. Furthermore, it may clutter the Explore experience for users.
An alternative would be to create a dynamic measure like this:
Read more below for step-by-step instructions.
Step One: Create Something for the User to Interact With
- For templated filters, add a
- For Liquid parameters, add a
In either case, these fields will appear to the user under the Filter-Only Fields section of the Field Picker:
parameter fields can accept a series of child parameters, allowing you to customize how they operate. See the Field Parameters documentation page for a complete list. There are two options that bear special mentioning for
parameter fields can have a special type called unquoted:
This type allows values to be inserted into SQL without being enclosed in quotes, as a string would be. This can be useful when you need to insert SQL values such as table names.
parameter fields have an option called allowed values that let you associate a user-friendly name with the value you want to insert. For example:
Step Two: Apply the User Input
The second step is to use Liquid to add the templated filter or Liquid parameter as desired.
The syntax for templated filters breaks down like this:
- The words
filter_namewith the name of the filter you created in the first step. You can also use a dimension if you did not create a filter-only field.
sql_or_lookml_referencewith the SQL or LookML that should be set “equal” to the user input (read more detail below). If using LookML, use the
In the example above we used:
The interaction between the Liquid tags and the SQL you write in between them is important to understand. The templated filter tags are always transformed into a logical expression. For example, if the user entered “Northeast” into the
order_region filter, Looker would turn these tags into:
order.region = 'Northeast'. In other words, Looker understands the user input and generates the appropriate logical expression.
This is often a point of confusion among Looker developers. Templated filters always result in a logical expression of some kind, and not the individual value entered by a user.
Because templated filters return a logical expression, you can use them with other logical operators and logical expressions that are valid in the SQL
WHERE statement. Using the example above, if you wanted to return all values except the region the user selected, you could use the following in the
It is also valid to use a LookML field as the filter condition. Any filters applied directly to the LookML field will determine the value of the
The syntax for Liquid parameters breaks down like this:
- The word
parametername you created in the first step.
For example, to apply the input from the
parameter field in step one, above you could create a measure like this:
Choosing Between Templated Filters and Liquid Parameters
Although templated filters and Liquid parameters are similar, there is an important difference between them:
- Liquid parameters insert user input directly (or using the values you define with allowed values).
- Templated filters insert values as logical statements, as described above.
In situations where you want to offer users more flexible input (such as various kinds of date ranges or string searches), you should try to use templated filters when possible. Looker can interpret the user input and write the appropriate SQL behind the scenes. This prevents you from having to account for every possible type of user input.
In situations where a logical statement can’t be inserted, or where you know a finite set of options the user might enter, use Liquid parameters.