[[["容易理解","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 (世界標準時間)。"],[],[],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."]]