측정기준의 sql 매개변수에서 ${TABLE}.field_name 구문을 사용하여 LookML 프로젝트의 데이터베이스 테이블 열을 한 번 정의합니다. 그런 다음 프로젝트의 다른 곳에서 ${field_name} 또는 ${view_name.field_name} 구문을 사용하여 측정기준을 참조합니다. 이렇게 하면 데이터베이스 열의 LookML 정의를 한 곳에서 유지할 수 있습니다(원래 ${TABLE}.field_name 측정기준). 이는 프로젝트의 여러 위치에서 참조해야 하는 경우에 유용합니다.
예를 들어 ${TABLE}.sale_price 구문을 사용하여 order_items라는 뷰에서 sale_price라는 기본 측정기준을 정의할 수 있습니다.
dimension: sale_price {
type: number
value_format_name: usd
sql: ${TABLE}.sale_price ;;
description: "The price at which an item is set to sell."
}
sale_price 측정기준을 참조하는 다른 필드를 정의할 때 order_items 뷰 내에서 ${sale_price} 구문을 사용하거나 ${order_items.sale_price} 구문을 사용하여 다른 뷰에서 sale_price 측정기준을 참조할 수 있습니다.
dimension: profit {
type: number
value_format_name: usd
sql: ${sale_price} - ${inventory_items.cost} ;;
description: "The difference between an item's sale price and an item's cost."
}
dimension: item_gross_margin {
type: number
value_format_name: percent_2
sql: 1.0 * ${profit}/NULLIF(${sale_price},0) ;;
}
measure: total_sale_price {
type: sum
value_format_name: usd
sql: ${sale_price} ;;
}
이 예에서 sale_price 측정기준의 열 이름이 변경되면 기본 sale_price 측정기준 정의에서 ${TABLE}.sale_price 참조를 한 번만 업데이트하면 됩니다. 이 변경사항은 profit, item_gross_margin, total_sale_price 필드는 물론 sale_price 측정기준을 참조하는 다른 모든 필드에 자동으로 적용됩니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-07-30(UTC)"],[],[],null,["# Maximizing code reusability with DRY LookML: Define fields once, use substitution operators everywhere\n\n| **Note:** This page is part of the [Looker cookbook: Maximizing code reusability with DRY LookML](/looker/docs/best-practices/dry-lookml-cookbook).\n\nLookML field definitions can change over time. For example, a database column name may change, or you may need to change the definition of a LookML field for other reasons.\n\nIf you use the syntax `{TABLE}.field_name` to reference a database column directly in multiple places, you must update each reference manually. Any fields that reference that dimension can break if you forget to update them, and Looker will display an error:\n\nTo make your LookML projects more efficient and easier to maintain, you can define fields in one place and use the substitution operator (`$`) to reference those fields everywhere else.\n\nThis page provides an [example of using the substitution operator](#example_referencing_the_underlying_database_column_only_once_for_a_dimension) (with the syntax `${field_name}`) to reference a single dimension in the definitions of multiple LookML fields.\n\nIngredients\n-----------\n\n- [Substitution operators](/looker/docs/sql-and-referring-to-lookml#substitution_operator_)\n- The LookML [`dimension`](/looker/docs/reference/param-field-dimension) parameter\n- The LookML [`sql`](/looker/docs/reference/param-field-sql) parameter\n- The LookML [`sql_table_name`](/looker/docs/reference/param-view-sql-table-name) parameter\n\nPrerequisites\n-------------\n\n- [A configured LookML model](/looker/docs/generating-a-model#when_you_need_to_configure_models)\n- [Permissions to develop LookML](/looker/docs/admin-panel-users-roles#permission_sets)\n\nExample: Referencing the underlying database column only once for a dimension\n-----------------------------------------------------------------------------\n\nDefine a database table column in a LookML project once using the syntax `${TABLE}.field_name` in the dimension's `sql` parameter. Then reference the dimension using the syntax `${field_name}` or `${view_name.field_name}` elsewhere in your project. This lets you maintain the LookML definition of the database column in one place (the original `${TABLE}.field_name` dimension), which is helpful if you need to reference it in multiple places in your project.\n\nAs an example, you can use the syntax `${TABLE}.sale_price` to define a base dimension called `sale_price` in a view called `order_items`: \n\n\n dimension: sale_price {\n type: number\n value_format_name: usd\n sql: ${TABLE}.sale_price ;;\n description: \"The price at which an item is set to sell.\"\n }\n\nWhen you define other fields that reference the `sale_price` dimension, can use the syntax `${sale_price}` within the `order_items` view (or use the syntax `${order_items.sale_price}` to reference the `sale_price` dimension in other views).\n**Important:** If you want to reference a field that is from another view, you need to make sure that view is joined to the [Explore](/looker/docs/reference/param-explore-explore) where the field is being used. \n\n\n dimension: profit {\n type: number\n value_format_name: usd\n sql: ${sale_price} - ${inventory_items.cost} ;;\n description: \"The difference between an item's sale price and an item's cost.\"\n }\n\n dimension: item_gross_margin {\n type: number\n value_format_name: percent_2\n sql: 1.0 * ${profit}/NULLIF(${sale_price},0) ;;\n }\n\n measure: total_sale_price {\n type: sum\n value_format_name: usd\n sql: ${sale_price} ;;\n }\n\nIn this example, if the column name for the dimension `sale_price` changes, you will only need to update the `${TABLE}.sale_price` reference once, in the definition of the base `sale_price` dimension. This change will then propagate automatically to the `profit`, `item_gross_margin`, and `total_sale_price` fields, as well as all other fields that reference the `sale_price` dimension."]]