User Guide Getting Started Help Center Documentation Community Training
New LookML
Old LookML
New LookML
Looker
Templated Filters and Liquid Parameters

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 WHERE or 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.

Examples

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:

view: customer_facts { derived_table: { sql: SELECT customer_id, -- Can be made a dimension SUM(sale_price) AS lifetime_spend -- Can be made a dimension FROM order WHERE region = 'northeast' -- Can NOT be made a dimension ;; } }

In this query, you can create dimensions from customer_id and 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:

view: customer_facts { derived_table: { sql: SELECT customer_id, SUM(sale_price) AS lifetime_spend FROM order WHERE {% condition order_region %} order.region {% endcondition %} ;; }   filter: order_region { type: string } }

Read more below for step-by-step instructions.

Making a Dynamic Measure with a Liquid Parameter

Consider a filtered measure that adds up the number of pants sold:

measure: pants_count { type: count filters: { field: category value: "pants" } }

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:

measure: category_count { type: sum sql: CASE WHEN ${category} = '{% parameter category_to_count %}' THEN 1 ELSE 0 END ;; }   filter: category_to_count { type: string }

Read more below for step-by-step instructions.

Basic Usage

Step One: Create Something for the User to Interact With

In either case, these fields will appear to the user under the Filter-Only Fields section of the Field Picker:

Both filter and 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.

First, parameter fields can have a special type called unquoted:

parameter: table_name { type: 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.

Second, 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:

parameter: number_of_results { type: string allowed_value: { label: "Less than 500" value: "< 500" } allowed_value: { label: "Less than 10,000" value: "< 10000" } allowed_value: { label: "All Results" value: "> 0" } }

Step Two: Apply the User Input

The second step is to use Liquid to add the templated filter or Liquid parameter as desired.

Templated Filters

The syntax for templated filters breaks down like this:

{% condition filter_name %} sql_or_lookml_reference {% endcondition %}

In the example above we used:

{% condition order_region %} order.region {% endcondition %}

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 developers. Templated filters always result in a logical expression of some kind, and not the individual value entered by a user.

Liquid Parameters

The syntax for Liquid parameters breaks down like this:

{% parameter parameter_name %}

In the example above we used:

{% parameter category_to_count %}

Choosing between Templated Filters and Liquid Parameters

Although templated filters and Liquid parameters are similar, there is an important difference between them:

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.

Top