User Guide Getting Started Help Center Documentation Community Training
Looker
  
English
Français
Deutsch
日本語
constant

Usage

constant: constant_name {
  value: "string"
  export: none | override_required | override_optional
}

Hierarchy

constant

Default Value

None

Accepts

  • A value subparameter, which accepts a string specifying the value of the constant
  • An export subparameter, which specifies whether the constant must be overridden when used in importing projects

Definition

The constant parameter defines a LookML constant, allowing you to specify a value that can be reused throughout a project. Constants can be referenced anywhere in the model where strings are accepted, thus helping reduce repetition in your LookML code.

Constants must be defined in the project manifest file. Constants may then be used throughout a project using @{constant_name} syntax. Constants can also be passed between projects using project import, and the export parameter can be used to override the constant’s value in the importing project.

Examples

Here are some examples of how you can use the constant parameter to define values that can be used throughout a project.

Labeling Explores with the constant Parameter

Suppose you want to create two Explores, labeled San Francisco Users and San Francisco Orders in the UI. To do this, you can define a constant city with the value "San Francisco" in the manifest.lkml file for your project:

constant: city { value: "San Francisco" }

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

explore: users { label: "@{city} Users" } explore: orders { label: "@{city} 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:

Using the sql_table_name Parameter with Constants

Let’s suppose you want to create several views based on tables in the schema 02349_sfo. To avoid having to specify the value 02349_sfo multiple times, you create a constant named schema in the manifest.lkml file for your project, as follows:

constant: schema { value: "02349_sfo" }

You can then create views based on the 02349_sfo schema by specifying @{schema}.view_name as the value for the sql_table_name parameter:

view: users { sql_table_name: @{schema}.users ;; }

This creates a view called Users that is based on the users table from the schema 02349_sfo.

Using Liquid Variables and HTML with Constants

Imagine that you want negative data values to be displayed within parentheses and in a red font. By setting this formatting as the value for a LookML constant, you can specify the formatting just once 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 negative_format with which to apply this formatting to a field:

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

This creates a constant negative_format that 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 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 follows:

Things to Know

Referring to Constants within a Constant Definition

When defining a constant, you can reference other constants defined within the manifest file for your project. Suppose you have already declared a constant city:

constant: city { value: "San Francisco" }

You may then declare a constant state that references the city constant in the example above:

constant: city_state { value: "@{city}, California" }

The constant state declared above will resolve to the string “San Francisco, California”.

Escaping Constant Syntax

When you wish to write a string that resembles constant syntax, but you don’t want that string to be evaluated as a constant, you can escape the constant syntax using the backslash escape character \ after the @ character. For example:

dimension: id { type: string label: "@\{id}" sql: ${TABLE}.id }

In the above example, the label parameter displays the string @{id} as the label for the id dimension, rather than evaluating the id constant and outputting its value.

Constant references will not be evaluated if the brackets are not closed, in which case it is not necessary to escape constant syntax. Thus, @{id in the example below would not be evaluated as a constant.

dimension: id { type: string label: "@{id" sql: ${TABLE}.id }

Using export in Imported Projects

The export subparameter of constant allows you to specify whether a constant’s value can be overridden in importing projects. The export parameter has the following possible values:

The local_dependency and remote_dependency parameters both have a subparameter override_constant, which is used to override the value of a constant with export set to override_optional or override_required in the importing project. See the Importing Files from Other Projects documentation for more information and an example of overriding constants in importing projects.

Top