Maximizing code reusability with DRY LookML: Defining a string once to use throughout your LookML project

You can use the LookML constant parameter within your project's manifest file to define a string that can be used throughout your project. LookML constants can be useful when you need to define a specific string — such as a number, a name, or HTML formatting for field values — and reuse that value throughout your project.

This page includes the following examples of using LookML constants to define and maintain reusable strings values in once place:



Example: Using the same string in the labels of multiple Explores

Suppose you want to create two Explores, labeled San Francisco Users and San Francisco Orders in the UI, but you don't want to manually type the text for each label.

To do this, you can define a constant place_name with the value "San Francisco" in the project manifest file for your project:

constant: place_name {
  value: "San Francisco"

This constant can then be referenced in any part of your project where a string is accepted, using the syntax @{place_name}. In this example, you can define the users and orders Explores, specifying "@{place_name} Users" and "@{place_name} Orders" as values for the label parameter, as in the following example:

explore: users {
  label: "@{place_name} Users"

explore: orders {
  label: "@{place_name} Orders"

In this example, Looker displays San Francisco Users and San Francisco Orders in the Explore menu and in the titles of the Explores, rather than the default Users and Orders labels.

Suppose you want to update all the references to San Francisco to Bay Area.

Rather than having to update each reference manually, you only have to make a single update to the place_name constant in the manifest file for your project:

constant: place_name {
  value: "Bay Area"

Since you defined the place_name constant, you don't have to manually change San Francisco to Bay Area in multiple places. The references to San Francisco with the place_name constant will be replaced with Bay Area, so Looker will display Bay Area Users and Bay Area Orders in the Explore menu and in the titles of the Explores.

Example: Applying the same formatting to negative values for multiple fields

Imagine that you want negative data values to be displayed in red and within parentheses wherever they appear in reports.

By setting this formatting as the value for a LookML constant, you can specify the formatting just once by using Liquid variables and HTML. Then, you can reference the constant whenever you want to apply that formatting to a field.

For example, you can create a constant called negative_format that you can use to apply this formatting to a field:

constant: negative_format {
  value: "{% if value < 0 %}
            <p style='color:red;'>({{rendered_value}})</p>
          {% else %}
          {% endif %}"

This code creates the constant negative_format, which specifies that negative data values should have a red font and be surrounded by parentheses. You can then apply this formatting to dimensions and measures in your dataset by using the html parameter.

For example, you can create Total Amount measure of type: sum and specify @{negative_format} as the value for the html parameter:

measure: total_amount {
  type: sum
  value_format_name: usd
  sql: ${amount} ;;
  html: @{negative_format} ;;

In your table, negative values for the Total Amount measure will be formatted as specified in the negative_format constant definition, with a red font and surrounded by parentheses.

If you want to apply the same formatting to negative values for other fields, you can reference the negative_format constant in the html parameter for those fields.