Search
Clear search
Close search
Google apps
Main menu
true

About calculated fields

Transform, categorize, and do math with your data.

Calculated fields let you create new metrics and dimensions in your data source that perform arithmetic, apply mathematical formulae, extract or transform text, or return new information based on logical comparisons. You can use the new dimensions and metrics in charts and controls, just like regular fields.

In this article:

Watch a video

This video explains the basics of creating calculated fields, including using simple math, CASE statements, and the REGEXP_MATCH function.

Use calculated fields

How calculated fields work

Calculated fields appear as new fields in your data source. If the result of the formula is an aggregated number of some kind, the new field is a metric. If the end result is a string (text), a date, or an unaggregated number, the new field is a dimension.

You can use a calculated field in charts and controls, just like other fields. You can also include a calculated field in another calculated field.

A calculated field adds a new column to your data. How this new data displays depends on how you use it in your reports. The example in the screenshot above creates a new Total field that multiplies the quantity sold field (Qty Sold) by the unit price field (Price). When used in a table, the calculated Total field shows the product of that multiplication for each row.

Example table showing total price calculated field.

Example of using a calculated field in a table.

When used in scorecard, the Total field displays the sum of the products for all rows in which quantity is multiplied by price.

Example of using a calculated field in a scorecard.

Just like normal fields, calculated fields are subject to any filters or date ranges in effect in the report.

What you can do with calculated fields

Calculated fields let you do the following kinds of things.

Do basic math with numeric fields

You can do simple arithmetic calculations using the normal operators:

  • Addition: +
  • Subtraction: -
  • Division: /
  • Multiplication: *

You can construct calculated field using any of the operators above in combination with static numeric values and unaggregated numeric fields from your data source. Use parentheses to force calculation order.

Examples

Users / New Users

(Price * Quantity) * .085

Apply functions to calculated fields

Functions let you create more advanced calculated fields. Data Studio supports the following kinds of functions:

  • Aggregation functions perform calculations over multiple rows of your data. Examples include SUM(), AVG(), MIN(), MAX().
  • Arithmetic functions apply advanced mathematical calculations to your data. Examples include LOG(), POWER(), ROUND().
  • Date functions let you manipulate and transform time data. Examples include TODATE(), DATE_DIFF(), YEAR().
  • Geo functions let you transform geographic location data. Examples include TOCITY(), TOCOUNTRY(), TOREGION().
  • Text functions let you manipulate string data. Examples include CONCAT(), REGEXP_MATCH(), SUBSTR().

Function arguments

All functions expect input, called an argument, that tells the function what to act upon. Arguments can be field names or expressions. An expression can be a number, literal text, or a statement that evaluates to a field name in your data source.
Be sure to enclose literal text in single or double quotes.

Date functions assume UTC as their timezone. Date functions can optionally take either an input or output (or both) format string.

Date formats

  • 'BASIC' - Supported format YYYY/MM/DD-HH:MM:SS
  • 'DEFAULT_DASH' - Supported format YYYY-MM-DD [HH:MM:SS[.uuuuuu]]
  • 'DEFAULT_SLASH' - Supported format YYYY/MM/DD [HH:MM:SS[.uuuuuu]]
  • 'DEFAULT_DECIMAL' - Supported format YYYYMMDD [HH:MM:SS[.uuuuuu]]
  • 'RFC_1123' - Sample format: Sat, 24 May 2008 20:09:47 GMT
  • 'RFC_3339' - Sample format: 2008-05-24T20:09:47Z
  • 'SECONDS' - Seconds since epoch
  • 'MILLIS' - Millis since epoch
  • 'MICROS' - Micros since epoch
  • 'NANOS' - Nanos since epoch
  • 'JULIAN_DATE' - Days since epoch
  • 'DECIMAL_DATE' - Same as 'DEFAULT_DECIMAL'
  • Any valid strptime format as string

Geo functions require a supported geo code as input.

Geo input codes

  • 'CITY_ID'
  • 'CONTINENT_CODE'
  • 'COUNTRY_ISO_CODE'
  • 'REGION_ISO_CODE'
  • 'SUB_CONTINENT_CODE'

The valid geo codes are defined here:

The input code is optional if the field expression is a derived column already containing semantic geo information; otherwise it is required.

Use branching logic in calculated fields

CASE statements let you perform branching "if/then/else" style logic in your calculated fields. For example, the following CASE formula categorizes countries into regions:

CASE 
    WHEN Country IN ("USA","Canada","Mexico") THEN "North America" 
    WHEN Country IN ("England","France") THEN "Europe" 
    ELSE "Other" 
END

Learn more about CASE.

Data type

Just like regular fields, calculated fields have a specific semantic data type. For example, if your calculated field uses arithmetic or aggregation functions, the calculated field's data type is Number. If the calculated field uses a text function, the data type is Text.

Most date functions return a Number, except for the TODATE function, which returns a Date type.

You can change the data type of your calculated fields using the Type drop-down menu in the data source editor.

Aggregation

A field's aggregation determines how it is summarized in your reports. Data Studio tries to assign the most appropriate aggregation to a calculated field. For certain functions, the aggregation is implicit: e.g., MEDIAN, AVG, SUM. In this case, the aggregation is set to Auto and cannot be changed. For other functions, you can specify the aggregation type using the Aggregation drop-down menu.

Data type and aggregation drop-down menus

Was this article helpful?
How can we improve it?
false
What's new in Data Studio

Learn about new features and recent changes.