The Problem
Let's say that we have two views: Rental
and Customer
. The Rental
view has more than 1.5B records, and each record has a customer_id
. The Customer
view has more than 200K records with the following columns:
customer_id
first_name
last_name
We want to filter the data in the Rental
view by entering the name of the customer in the filter. However, the customers' names are stored in the Customer
view and we don't want to join these two views because of the number of records in both views.
A Solution
This problem can be solved by using Liquid variables and the parameter
parameter.
-
In the
Customer
view, concatenate the ID, first name, and last name to create a new dimension calledfull_name_and_id
:dimension: full_name_and_id { type: string sql: ${customer_id} || '-' || ${first_name} || '-' || ${last_name} ;; }
The values of this dimension are formatted as
ID-first_name-last_name
, for example,123-Christel-Ilaka
. -
In the
Rental
view, create a filter-only field callednamesearch
using a Liquidparameter
:parameter: namesearch { type: string suggest_explore: customer suggest_dimension: customer.full_name_and_id suggest_persist_for: "24 hours" }
The
suggest_explore
andsuggest_dimension
parameters allow the user to query theCustomer
view via itsexplore
without having to join both views (theexplore
namedcustomer
must exist in the project). The dimension created in step 1,full_name_and_id
, is queried to generate the suggested values. -
Since the
Rental
view needs to be filtered based on theID
dimension, create ayesno
dimension that will be used to extract theID
from the value entered by the user:The Liquid used with the
sql
parameter in line 5 splits the value entered in the filter, creating an array calledmy_array
. Then the first element of this array is assigned to a variable,the_id
, in line 6. Finally, for each record in theRental
view, the code in line 7 checks if thecustomer_id
is equal to the value stored in the variablethe_id
and, if it is, returnsyes
; otherwise, it returnsno
. -
Add a
sql_always_where
parameter inexplore: rental
.The Liquid in line 3 verifies whether the parameter
namesearch
is being used in the Looker UI as a filter; if it is, then the query will only return records wherefiltered_name
is equal toyes
. If the parameter is not being used as a filter, the query returns all the data.
When the parameter namesearch
is not used to filter results (no filter is being applied), the generated SQL looks like:
When the parameter namesearch
is applied to filter results by a specific user, the generated SQL looks like: