Tutorial: Create calculated fields

Learn by doing: calculate an extended price with a discount for large orders.


  • Learn how to create a simple arithmetic calculated field.
  • Learn how to break more complicated calculations into simpler steps.
  • Create a conditional expression to evaluate your data and return different results.


Suppose you manage an online pet supply store. You might measure sales data, such product codes (SKU), item names, price, and quantity sold. Here's a simple example:

SKU Item Price Qty Sold
D-001 Healthy Dog Dog Food



B-002 Parrot Perch



B-001 Pretty Bird Bird Seed



C-002 Hungry Kitty Cat Food



D-002 Playful Puppy Toy




Happy Cat Catnip




Calculated fields in Data Studio can help you manipulate and extend your data. In this exercise, you'll use calculated fields to:

  • calculate the total value of each order
  • apply a discount to orders over a certain amount
  • categorize sales into departments.

Sample data

To complete this exercise as written, use the sample data below. 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 to import the data into Data Studio.

Copy and paste this data into a text file:
SKU,Item,Price,Qty Sold
D-001,Healthy Dog Dog Food,79.96,16
B-002,Parrot Perch,74.97,8
B-001,Pretty Bird Bird Seed,31.96,20
C-002,Hungry Kitty Cat Food,29.98,3
D-002,Playful Puppy Toy,17.97,11
C-001,Happy Cat Catnip,14.97,4


1Prepare the data

Goal: Create a Google Sheet using the sample data above.
  1. Copy the sample data in the box above.
  2. Open a text editor, paste in the sample data, then save the file as pet-store-data.csv.
    • Be sure to save this file as plain text, otherwise Sheets won't be able to import the data.

      • On Windows, you can use Notepad.

      • On Mac OS, you can use TextEdit, then select Format > Make plain text.

      • On Chrome OS, you can use the Text app, or Docs and then select File > Download and choose Plain Text.

  • Create a new Google Sheet.
  • In Sheets, select Fileand thenImport.
  • Select Upload, then upload the sample data file you created in step 2.
  • Select Replace spreadsheet.
  • Name your spreadsheet Pet Store.

2Create a report and add the data

Goal: Connect a data source to the Pet Store spreadsheet and add it to a report.
  1. Create a new report in Data Studio.
  2. In the Add data to report panel, select the Google Sheets connector.
  3. Select the Pet Store spreadsheet and worksheet you just created.
  4. Leave the default options selected.
  5. In the lower right, click ADD.

In a moment, you'll see the new report, with a table on the canvas that uses fields from the Pet Store data source.

If you don't see the Pet Store spreadsheet in the list, try reloading Data Studio.

3Create the subtotal field

Goal: Create a field that calculates the order subtotal.
You can create 2 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 use them in any chart based on this data source, and that they can refer to other calculated fields.
Learn more about the difference here.
  1. In the menu, select Resource > Manage added data sources.
  2. To the right of the Pet Store data source, click Create icon.EDIT.
    The data source fields panel appears.
  3. On the top right, click "Create new" button ADD A FIELD.
    The calculated field editor appears.
  4. In Field Name, enter Subtotal.
  5. In Formula, enter Price * Qty Sold.
    The formula editor autocompletes dimension, metric, or function names as you type. You can also drag fields from the Available Fields list on the left into your formula.
  6. On the bottom right, click SAVE.
  7. On the left, return to the main data source editor by clicking ALL FIELDS.

4Create the discount field

Goal: Create a field that applies a variable discount rate to orders of a certain value.

Let's say 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.

Subtotal value Discount rate
0 - 99 0
100 - 499


500 or more 10%



  • 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.
  • Use the CASE expression to evaluate the Subtotal field.
    • Use a WHEN clause for each discount rate.
    • Use an ELSE clause to return the full price (0% discount).
  • Express the discount as a decimal value. For example, a 25% discount would be .25.


WHEN Subtotal > 499 THEN .9
WHEN Subtotal > 100 THEN .95

Note: WHEN clauses are evaluated in order, and the first one that is true is the one whose THEN clause is returned.

5 Create the total field

Goal: Create a field that applies the discount rate to the subtotal.

Create another calculated field, called Total, that multiples the Subtotal field by the Discount field.


Subtotal * Discount
Note that you could also have accomplished the same thing by including the subtotal calculation directly:
(Price * Qty Sold) * Discount
But because of a limitation in CASE expressions, you can't combine the discount evaluation and the total calculation in one field. For example, the following WILL NOT WORK:
Error: THEN/ELSE statements cannot contain functions or mathematical operations.
Understanding how and when you can accomplish your goal in a single calculated field, and when you'll need to break the calculation into separate steps, will make you more successful with calculated fields.

6Create the department field

Goal: Create a calculated field that categorizes items sold into departments.

Create a final calculated field, called Department, that parses the SKU field and assigns new values depending on the SKU. You'd like to group dog, cat, and bird related items into separate departments.

Hint: use the CASE statement again, this time, with the REGEXP_MATCH function.


ELSE "Misc"

7 See your data in a report

Goal: Visualize your calculated fields.
  1. After saving the Department field, on the left, return to the data source editor by clicking ALL FIELDS.
  2. In the upper right, click DONE.
  3. On the right close the manage data sources panel.

You can now add your new calculated fields to charts:

  1. Select the table.
  2. On the right, drag each calculated field you created from the Available Fields list to the dimension and metrics section of the Data panel, as shown below:

Calculated field tutorial example.

Was this helpful?
How can we improve it?

Need more help?

Sign in for additional support options to quickly solve your issue