When referring to another field's ID in a formula, prefix it with a $. For
example, if your field ID is t_celsius, it should be referred to as
$t_celsius in the formula.
Type for calculated fields
Type is supported for calculated fields, but the output of the formula must
match the expected format for that Type.
For example, for the formula:
"HYPERLINK($url, $description)"
The Type should be HYPERLINK.
aggregation for calculated fields
If your formula involves an aggregation, the aggregation should be set to
AUTO. Check the type column in the Function List to see if a function
returns an aggregation. If your formula does not involve an aggregation, set it
to NONE.
Formula
aggregation
"SUM($cost) / SUM($units)"
AUTO
"$cost + $units"
NONE
Examples of calculated fields in schema
functiongetSchema(request){varcc=DataStudioApp.createCommunityConnector();varfields=cc.getFields();vartypes=cc.FieldType;varaggregations=cc.AggregationType;fields.newDimension().setId('date').setName('Date').setDescription('Date of measurement').setType(types.YEAR_MONTH_DAY);fields.newMetric().setId('t_celsius').setName('Temperature (Celsius)').setDescription('Temperature in Celsius').setType(types.NUMBER).setAggregation(aggregations.NONE);fields.newMetric().setId('t_fahrenheit').setName('Temperature (Fahrenheit)').setDescription('Temperature in Fahrenheit').setFormula('$t_celsius / 5 * 9 + 32').setType(types.NUMBER).setAggregation(aggregations.NONE);fields.newMetric().setId('average_temperature (Celsius)').setName('Average Temperature (Celsius)').setDescription('Temperature in Fahrenheit').setFormula('AVG($t_celsius)').setType(types.NUMBER).setAggregation(aggregations.AUTO);fields.newMetric().setId('average_temperature (Celsius)').setName('Average Temperature (Celsius)').setDescription('Temperature in Fahrenheit').setFormula('AVG($t_celsius / 5 * 9 + 32)').setType(types.NUMBER).setAggregation(aggregations.AUTO);fields.newDimension().setId('feels_like').setName('Feels Like').setDescription('What it feels like outside.').setFormula('CASE WHEN $t_celsius < 0 THEN "Freezing" WHEN $t_celsius > 40 THEN "Too warm" ELSE "Not bad" END').setType(types.TEXT);return{'schema':fields.build()};}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-09-18 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eformula\u003c/code\u003e field property enables the addition of calculated fields to your connector's schema using supported functions.\u003c/p\u003e\n"],["\u003cp\u003eWhen referencing other fields within a formula, prefix their \u003ccode\u003eID\u003c/code\u003e with a \u003ccode\u003e$\u003c/code\u003e symbol (e.g., \u003ccode\u003e$fieldName\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eType\u003c/code\u003e property for calculated fields should align with the formula's output format (e.g., \u003ccode\u003eHYPERLINK\u003c/code\u003e for hyperlink formulas).\u003c/p\u003e\n"],["\u003cp\u003eFormulas using aggregation functions require setting the \u003ccode\u003eaggregation\u003c/code\u003e property to \u003ccode\u003eAUTO\u003c/code\u003e, while those without aggregations should use \u003ccode\u003eNONE\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eA comprehensive function list and documentation for specific functions like \u003ccode\u003eCASE\u003c/code\u003e, \u003ccode\u003eCAST\u003c/code\u003e, \u003ccode\u003eTODATE\u003c/code\u003e, and others are available for reference.\u003c/p\u003e\n"]]],[],null,["The [`formula`](/looker-studio/connector/reference#field.formula) field property can be used to add [calculated fields](https://support.google.com/looker-studio/answer/6299685) to your\nconnector's schema.\n\nThe full list of functions that are supported in formulas can be found at\n[Function List](https://support.google.com/looker-studio/table/6379764). Additionally, there is further documentation for:\n\n- [CASE](https://support.google.com/looker-studio/answer/7020724)\n- [CAST](https://support.google.com/looker-studio/answer/7280720)\n- [TODATE](https://support.google.com/looker-studio/answer/7037300)\n- [HYPERLINK](https://support.google.com/looker-studio/answer/7431836)\n- [IMAGE](https://support.google.com/looker-studio/answer/7570489)\n- [REGEXP_EXTRACT](https://support.google.com/looker-studio/answer/7050487)\n- [REGEXP_MATCH](https://support.google.com/looker-studio/answer/7064476)\n- [REGEXP_REPLACE](https://support.google.com/looker-studio/answer/7067058)\n\n| **Caution:** These function names cannot be used as the `ID` for a field.\n\nWhen referring to another field's `ID` in a formula, prefix it with a `$`. For\nexample, if your field `ID` is `t_celsius`, it should be referred to as\n`$t_celsius` in the formula.\n\n`Type` for calculated fields\n\n`Type` is supported for calculated fields, but the output of the formula must\nmatch the expected format for that `Type`.\n\nFor example, for the formula: \n\n \"HYPERLINK($url, $description)\"\n\nThe `Type` should be `HYPERLINK`.\n\n`aggregation` for calculated fields\n\nIf your formula involves an aggregation, the `aggregation` should be set to\n`AUTO`. Check the `type` column in the [Function List](https://support.google.com/looker-studio/table/6379764) to see if a function\nreturns an aggregation. If your formula does not involve an aggregation, set it\nto `NONE`.\n\n| Formula | `aggregation` |\n|------------------------------|---------------|\n| `\"SUM($cost) / SUM($units)\"` | `AUTO` |\n| `\"$cost + $units\"` | `NONE` |\n\nExamples of calculated fields in schema \n\n function getSchema(request) {\n var cc = DataStudioApp.createCommunityConnector();\n var fields = cc.getFields();\n var types = cc.FieldType;\n var aggregations = cc.AggregationType;\n\n fields.newDimension()\n .setId('date')\n .setName('Date')\n .setDescription('Date of measurement')\n .setType(types.YEAR_MONTH_DAY);\n\n fields.newMetric()\n .setId('t_celsius')\n .setName('Temperature (Celsius)')\n .setDescription('Temperature in Celsius')\n .setType(types.NUMBER)\n .setAggregation(aggregations.NONE);\n\n fields.newMetric()\n .setId('t_fahrenheit')\n .setName('Temperature (Fahrenheit)')\n .setDescription('Temperature in Fahrenheit')\n .setFormula('$t_celsius / 5 * 9 + 32')\n .setType(types.NUMBER)\n .setAggregation(aggregations.NONE);\n\n fields.newMetric()\n .setId('average_temperature (Celsius)')\n .setName('Average Temperature (Celsius)')\n .setDescription('Temperature in Fahrenheit')\n .setFormula('AVG($t_celsius)')\n .setType(types.NUMBER)\n .setAggregation(aggregations.AUTO);\n\n fields.newMetric()\n .setId('average_temperature (Celsius)')\n .setName('Average Temperature (Celsius)')\n .setDescription('Temperature in Fahrenheit')\n .setFormula('AVG($t_celsius / 5 * 9 + 32)')\n .setType(types.NUMBER)\n .setAggregation(aggregations.AUTO);\n\n fields.newDimension()\n .setId('feels_like')\n .setName('Feels Like')\n .setDescription('What it feels like outside.')\n .setFormula('CASE WHEN $t_celsius \u003c 0 THEN \"Freezing\" WHEN $t_celsius \u003e 40 THEN \"Too warm\" ELSE \"Not bad\" END')\n .setType(types.TEXT);\n\n return { 'schema': fields.build() };\n }\n\n| **Key Point:** `aggregation` is set to `AUTO` for formulas involving aggregations, and `NONE` otherwise."]]