[[["容易理解","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-09-04 (世界標準時間)。"],[],[],null,["# Tutorial: Create calculated fields in Looker Studio\n\nObjectives\n----------\n\nIn this tutorial, you'll accomplish the following:\n\n- Learn how to create an arithmetic calculated field.\n- Create a conditional expression to evaluate your data and return different results.\n- Learn different approaches to implementing calculated fields.\n\nScenario\n--------\n\nSuppose you manage an online pet supply store. You might measure sales data, such product codes (SKU), item names, price, and quantity sold. Here's an example:\n\nCalculated fields in Looker Studio can help you manipulate and extend this data. In this exercise, you'll use calculated fields to perform these tasks:\n\n- Calculate the total value of each order.\n- Apply a discount to orders over a certain amount.\n- Categorize sales into departments.\n\n| **Tip:** There can often be more than one way to achieve a particular result with calculated fields, depending on your needs and how you want to use those fields. This tutorial walks you through one approach (using separate fields for the calculations). At the end of this document, you'll find some [alternative solutions](#alternative-solutions).\n\nSample data\n-----------\n\nTo complete this exercise as written, use the following sample data. Otherwise, use your own data (adjusting the field names accordingly). You'll need access to Google Sheets. Alternatively, you could use the [File Upload connector](/looker/docs/studio/upload-csv-files-to-looker-studio) to import the data into Looker Studio.\n\nCopy and paste this data into a text file: \n\n```\nSKU,Item,Price,Qty Sold\nD-001,Healthy Dog Dog Food,79.96,16\nB-002,Parrot Perch,74.97,8\nB-001,Pretty Bird Bird Seed,31.96,20\nC-002,Hungry Kitty Cat Food,29.98,3\nD-002,Playful Puppy Toy,17.97,11\nC-001,Happy Cat Catnip,14.97,4\n```\n\nStep 1: Prepare the data\n------------------------\n\n| **Objective:** Create a Google Sheet using the sample data shown previously.\n\n1. Copy the sample data in the previous box.\n2. Open a text editor, paste in the sample data, and save the file as `pet-store-data.csv`. **Important:** Be sure to save this file as plain text; otherwise, Sheets won't be able to import the data.\n | - On Windows, you can use Notepad.\n | - On macOS, you can use TextEdit and then select **Format \\\u003e Make plain text**.\n | - On Chrome OS, you can use the Text app or Google Docs and then select **File \\\u003e Download** and choose **Plain Text**.\n\n- [Create a new Google Sheet](https://sheet.new).\n\n- In Sheets, select **File \\\u003e Import.**\n\n- Select **Upload** and then upload the sample data file you created in step 2.\n\n- Select **Replace spreadsheet**.\n\n- Name your spreadsheet `Pet Store`.\n\nStep 2: Create a report and add the data\n----------------------------------------\n\n| **Objective:** Connect a data source to the Pet Store spreadsheet and add it to a report.\n\n1. [Create a new report](/looker/docs/studio/tutorial-create-a-new-report) in Looker Studio.\n2. In the **Add data to report** panel, select the Google Sheets [connector](/looker/docs/studio/connector).\n3. Select the `Pet Store` spreadsheet and worksheet you just created.\n4. Leave the default options selected.\n5. Click **ADD**.\n\nIn a moment, you'll see the new report, with a table on the canvas that uses fields from the Pet Store data source.\n| **Tip:** If you don't see the `Pet Store` spreadsheet in the list, try reloading Looker Studio.\n\nStep 3: Create the subtotal field\n---------------------------------\n\n| **Objective:** Create a field that calculates the order subtotal.\n| **Tip:** You can create two kinds of calculated fields: *chart-specific calculated fields* and *data source calculated fields* . In this tutorial, you'll create data source calculated fields. The main benefits of data source calculated fields are that you can use them in any chart based on this data source and they can refer to other calculated fields.\n|\n| \u003cbr /\u003e\n|\n| Learn more [data source versus chart-specific calculated fields](/looker/docs/studio/about-calculated-fields#data_source_versus_chart-specific_calculated_fields).\n\n1. In the menu, select **Resource \\\u003e Manage added data sources**.\n\n2. Locate the **Pet Store** data source and then click **EDIT**.\n\n The **data source fields** panel appears.\n3. Click **ADD A FIELD**.\n\n The calculated field editor appears.\n4. In **Field Name** , enter `Subtotal`.\n\n5. In **Formula** , enter `Price * Qty Sold`.\n\n | **Tip:** The formula editor autocompletes dimension, metric, or function names as you type. You can also drag fields from the **Available Fields** list into your formula.\n6. Click **SAVE**.\n\n7. Return to the main data source editor by clicking **ALL FIELDS**.\n\nStep 4: Create the discount field\n---------------------------------\n\n| **Objective:** Create a field that applies a variable discount rate to orders of a certain value.\n\nSuppose that you want to give different discount rates depending on the value of the order. Create another calculated field, called `Discount`, that evaluates the **Subtotal** field and applies the following discounts.\n\n\u003e **Hints:**\n\u003e\n\u003e - In this step, you'll only calculate the discount. You'll apply it in the following step by multiplying the **Subtotal** field by the **Discount** field.\n\u003e - Use the [`CASE` expression](/looker/docs/studio/case-searched) to evaluate the **Subtotal** field.\n\u003e - Use a `WHEN` clause for each discount rate.\n\u003e - Use an `ELSE` clause to return the full price (0% discount).\n- Express the discount as a decimal value. For example, a 25% discount would be .25. \n\n#### Solution\n\n```component-pascal\nCASE\nWHEN Subtotal \u003e 499 THEN .9\nWHEN Subtotal \u003e 100 THEN .95\nELSE 1\nEND\n```\n| **Note:** `WHEN` clauses are evaluated in order, and the first one that is true is the one whose `THEN` clause is returned.\n\nStep 5: Create the total field\n------------------------------\n\n| **Objective:** Create a field that applies the discount rate to the subtotal.\n\nCreate another calculated field, called `Total`, that multiples the **Subtotal** field by the **Discount** field. \n\n#### Solution\n\n`Subtotal * Discount`\n\nStep 6: Create the department field\n-----------------------------------\n\n| **Objective:** Create a calculated field that categorizes sold items into departments.\n\nCreate a final calculated field, called `Department`, that parses the SKU field and assigns new values depending on the SKU. You'll group dog, cat, and bird related items into separate departments.\n**Hint:** Use the `CASE` statement again, this time with the [`STARTS_WITH`](/looker/docs/studio/startswith) (or [`REGEXP_MATCH`](/looker/docs/studio/regexpmatch)) function. \n\n#### Solution\n\n```component-pascal\nCASE\nWHEN STARTS_WITH(SKU, \"C\") THEN \"Cat\"\nWHEN STARTS_WITH(SKU, \"D\") THEN \"Dog\"\nWHEN STARTS_WITH(SKU, \"B\") THEN \"Bird\"\nELSE \"Misc\"\nEND\n```\n\nStep 7: See your data in a report\n---------------------------------\n\n| **Objective:** Visualize your calculated fields.\n\n1. After saving the **Department** field, on the left, return to the data source editor by clicking **ALL FIELDS**.\n2. Click **DONE** and then close the manage data sources panel.\n\nYou can now add your new calculated fields to charts by using these steps:\n\n1. Select the table.\n2. Add each calculated field you created to the table by dragging them from the **Data** panel onto the table or into the table's **Setup** panel.\n\nAlternative solutions\n---------------------\n\nThe previous solution breaks the problem up into individual chunks and creates discrete fields to handle each chunk, as you've done with the **Subtotal** , **Discount** , and **Total** fields.\n\nAnother approach is to perform all the calculations in a single calculated field. For example, you could combine steps 3 - 5 of this tutorial into one field: \n\n```component-pascal\nCASE\nWHEN Price * Qty Sold \u003e 499 THEN (Price * Qty Sold) * .9\nWHEN Price * Qty Sold \u003e 100 THEN (Price * Qty Sold) * .95\nELSE Price * Qty Sold\nEND\n```\n\nThere are advantages and disadvantages to each approach:\n\nBreaking down complex formulas into separate fields can make your formulas easier to read and write as well as less error-prone. Separate fields can also be useful in more contexts. For example, using a separate field for the discount rate lets you use that field in other calculations or display it in your reports. One drawback to creating formulas with lots of discrete calculated fields is that it may not be obvious what each one does, so you'll need to edit them individually to see how they are calculated.\n\nOn the other hand, centralizing all the logic into one field can make formulas easier to understand and edit. But you also might have to do a lot of repetitive typing, and a complex formula may be harder to extend or maintain. For example, say you had 10 product tiers with different order quantity thresholds. Or say you wanted to use the same discounts for a different product line. In these cases, it might be easier or more efficient to use separate fields for those thresholds and discounts.\n\n### The middle path\n\nThis solution blends the two approaches described previously. It keeps the main logic in a single field while breaking out some of the dependent logic into separate fields: \n\n```component-pascal\nCASE\nWHEN Subtotal \u003e Large Order THEN Subtotal * Large Order Discount\nWHEN Subtotal \u003e Medium Order THEN Subtotal * Medium Order Discount\nELSE Subtotal\nEND\n```\n\nFor this solution, we've created separate fields to hold values for large and medium orders and corresponding discounts.\n\nThis solution has the advantage of being easy to read, flexible, and relatively easy to adjust as needed.\n\nRelated resources\n-----------------\n\n- [About calculated fields](/looker/docs/studio/about-calculated-fields)\n- [Function list](/looker/docs/studio/function-list?ref_topic=7570421)\n- [Parameters](/looker/docs/studio/parameters)"]]