[[["容易理解","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-31 (世界標準時間)。"],[],[],null,["# Maximizing code reusability with DRY LookML: Defining reusable measures for complex calculations\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\nWhen you're defining complex calculations in LookML, it can be helpful to break them down into intermediate steps that involve simpler calculations. By creating intermediate measures, you make your calculations more readable, easier to maintain, and less error-prone, since you only need to ensure that each intermediate calculation is correct in one place.\n\nThis page provides an [example](#example_breaking_a_complex_calculation_into_intermediate_measures) of how you can make your calculations in LookML more readable and maintainable by defining intermediate measures to break complex calculations into smaller, more manageable steps.\n\nIngredients\n-----------\n\n- The LookML [`measure`](/looker/docs/reference/param-field-measure) parameter\n- The [`sql`](/looker/docs/reference/param-field-sql) parameter of a measure\n- A measure of [`type: sum`](/looker/docs/reference/param-measure-types#sum)\n- A measure of [`type: number`](/looker/docs/reference/param-measure-types#number)\n- The LookML [`hidden`](/looker/docs/reference/param-field-hidden) parameter (for fields)\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: Breaking a complex calculation into intermediate measures\n------------------------------------------------------------------\n\nSuppose you have a company that sells products online, and you want to define measures to calculate total profit and shareholder dividends. One way to do this would be to define two measures: a `total_profit` measure and a `shareholder_dividends` measure, as follows: \n\n\n measure: total_profit {\n type: number\n sql: SUM(${orders.sale_price}) - SUM(${employees.salary}) - SUM(${products.cost}) ;;\n }\n\n measure: shareholder_dividends\n description: \"We give shareholders 60% of our total profits.\"\n type: number\n sql: 0.6 * (SUM(${orders.sale_price}) - SUM(${employees.salary}) - SUM(${products.cost})) ;;\n\nIn this example, the calculation `SUM(${orders.sale_price}) - SUM(${employees.salary}) - SUM(${products.cost})` is reused in the `sql` parameter for both measures. If you need to update the definition of this calculation, such as to correct an error, you would have to update the calculation manually for both measures.\n\nYou can make these measure definitions easier to maintain by reusing the `total_profit` measure within the calculation in the `shareholder_dividends` measure: \n\n\n measure: total_profit {\n type: number\n sql: SUM(${orders.sale_price}) - SUM(${employees.salary}) - SUM(${products.cost}) ;;\n }\n\n measure: shareholder_dividends\n description: \"We give shareholders 60% of our total profits.\"\n type: number\n sql: 0.6 * ${total_profit} ;;\n\nYou may want to break the calculation in `total_profit` into even simpler measures that can be reused in other calculations. For example, you can create measures of `type: sum` called `total_sales`, `total_revenue`, `total_cost`, and `total_salary`: \n\n\n measure: total_sales {\n hidden: yes\n type: sum\n sql: ${orders.sale_price} ;;\n }\n\n measure: total_revenue {\n hidden: yes\n type: number\n sql: ${total_sales} ;;\n }\n\n measure: total_cost {\n hidden: yes\n type: sum\n sql: ${products.cost} ;;\n }\n\n measure: total_salary {\n hidden: yes\n type: sum\n sql: ${employees.salary} ;;\n }\n\n| **Note:** You can set the `hidden` parameter to `yes` to hide intermediate fields that you don't want to surface in the Explore UI for users.\n\nYou can then reuse the intermediate fields you have defined as follows: \n\n\n measure: total_expenses {\n type: number\n sql: ${total_cost} + ${total_salary} ;;\n }\n\n measure: total_profit {\n type: number\n sql: ${total_revenue} - ${total_expenses} ;;\n }\n\n measure: shareholder_dividends {\n description: \"We give shareholders 60% of our total profits.\"\n type: number\n sql: 0.6 * ${total_profit} ;;\n }\n\nAlthough you have defined more measures, these intermediate measures can be reused within other calculations, and it will be easier correct a mistake or make any changes to calculations that are used in multiple measures."]]