In Looker, Liquid is a powerful templating language that you can use to introduce dynamic elements into your LookML model. One of its most useful features is parameters. Parameters allow developers to create interactive fields that users can modify. This way, users can customize their analyses without diving into the LookML code. Let's delve deeper into Liquid parameters, their use cases, and how you can optimize their usage.
Understanding Liquid Parameters
A Liquid parameter allows users to insert their input into LookML code. Here's an example of how a parameter is defined:
view: orders {
parameter: order_type {
type: string
allowed_value: {
value: "retail"
label: "Retail Orders"
}
allowed_value: {
value: "wholesale"
label: "Wholesale Orders"
}
}
}
In this example, order_type is a parameter that can take on the values "retail" or "wholesale". The user sees the labels "Retail Orders" and "Wholesale Orders" in the Looker interface.
Once a parameter is defined, it can be used in conjunction with Liquid logic to dynamically change LookML code. Here's an example:
view: orders {
measure: total_sales {
type: sum
sql: CASE WHEN {% parameter order_type %} = 'retail' THEN ${retail_price} * ${quantity}
WHEN {% parameter order_type %} = 'wholesale' THEN ${wholesale_price} * ${quantity}
ELSE NULL
END ;;
}
}
In this case, the total_sales measure uses the order_type parameter to decide whether to calculate sales based on retail_price or wholesale_price.
Effective Use of Liquid Parameters
Liquid parameters are not just limited to string type and can be number, date, yesno, tier, and more, each enabling different kinds of user interactions.
A more advanced use case of Liquid parameters is creating dynamic dimensions or measures. For instance, you could create a dimension that allows users to choose which column to display:
view: orders {
parameter: display_column {
type: unquoted
allowed_value: { value: "order_id" }
allowed_value: { value: "order_date" }
}
dimension: selected_column {
sql: ${TABLE}.{% parameter display_column %} ;;
}
}
In this case, display_column is a parameter of type unquoted, which allows users to select either order_id or order_date to display.
Optimizing Liquid Parameter Usage
While Liquid parameters are powerful, they can also introduce complexity into your LookML model. Here are some best practices to follow:
-
Ensure clarity: Make sure parameter labels are clear, as users will interact with these labels in the Looker UI.
-
Limit choices: Avoid overwhelming users with too many choices. Stick to a limited set of options that provide the most value.
-
Validate input: Make sure to validate the user's input to prevent errors or inappropriate usage. This is particularly important for parameters of type
stringorunquoted. -
Use judiciously: Each parameter adds a level of complexity to your LookML model. Use them sparingly and only when the benefits to users are clear.
In conclusion, Liquid parameters are a fantastic way to make Looker views more interactive and customizable, empowering users to make more detailed and personalized analyses. By understanding the ins and outs of Liquid parameters, you can leverage this powerful feature to its full potential.