Calculated fields in custom reports

These are all of the functions you can use to create calculated fields and measures.

Absolute

Absolute(NumericExpression)
Returns the absolute value of a number or field, that is, the non-negative value of the number.
Example
Absolute("Transaction Amount")
Shows the magnitude of each transaction, regardless of whether the transaction is positive or negative.
"Commission Rate" * Absolute("Transaction Amount")
Computes a positive commission on all transactions, regardless of whether the transaction is positive or negative.

Attribute

Attribute('StringExpression1', ['StringExpression2' ])
Given the name of an attribute as the first argument and an optional category as the second argument, returns the attribute's current value as a String. If a category is specified (USER, TENANT, or SERVER), returns a categorical attribute; returns a hierarchical attribute otherwise.
This value can be cast to a different data type using one of the casting functions available in Custom Reports: Boolean(), Date(), Decimal(), Integer(), Time(), or Timestamp().
It's your responsibility to ensure the attribute's format and values work correctly with your view.
Example
Attribute('my_attribute')

Round

Round(NumericExpression[,Integer])
Rounds a number to a specified number of digits; default is zero (0) digits. Decimal values greater than 0.5 are rounded to the next largest whole number, and values less than 0.5 are rounded down.
Examples
Round("Sales")

Case

Case(Expression, ValueExpression1, ReturnExpression1, ValueExpression2, ReturnExpression2[,...,ValueExpressionN, ReturnExpressionN][,DefaultReturnExpression])
Takes 2N+1 or 2N+2 arguments: an expression followed by one or more value expression/return expression pairs, with an optional final return expression. Compares the expression in the first argument to each value expression in order of appearance. Returns the value of the expression immediately following the first value expression that matches. If no expression matches, returns the final DefaultReturnExpression if present, null otherwise.
The types of all the return expressions must be compatible. For example, can't mix numeric and text return value types.
Examples
Case("Shipped by", 1, 'FedEx', 2, 'UPS', 3, 'USPS', 'Unknown')

CountDistinct

CountDistinct(Expression[,'Level'])
Returns the distinct count of non-null items in the input. Always returns a non-negative integer. Level can be one of the following: Current (default), ColumnGroup, ColumnTotal, RowGroup, RowTotal, Total.
Examples
CountDistinct("Customer Name", 'Total')— Counts the number of distinct customers.

Boolean

Boolean('StringExpression')
Casting function that takes a String expression and converts it to a Boolean data type. The String can be any expression that returns a supported string, including a field value or an attribute retrieved with the Attribute() function. The Boolean() function requires one of the following Strings: true, false, True, False, TRUE, or FALSE. Other Strings will return an error.
Examples
Boolean('true')
Boolean(Attribute('my_boolean_attribute'))

Date

Date('StringExpression')
Casting function that takes a String expression and converts it to a Date data type. The String can be any expression that returns a supported string, including a field value or an attribute retrieved with the Attribute() function. The Date() function requires a String value formatted as 'yyyy-MM-dd'. Other Strings will return an error.
Examples
Date('2015-07-17')
Date(Attribute('my__date_attribute'))

CaseRange

CaseRange(NumericExpressionInput, NumericExpression1, ReturnExpression1, NumericExpression2,ReturnExpression2[,..., NumericExpressionN, ReturnExpressionN][,DefaultReturnExpression])
Takes 2N+1 or 2N+2 arguments: an expression followed by one or more numeric expression/return expression pairs, with an optional final return expression. Finds the first numeric expression that is greater than the input expression and returns the value of the corresponding return expression. If no expression is greater, returns the final DefaultReturnExpression if present, null otherwise.
The types of all the return expressions must be compatible. For example, you can't mix numeric and text return value types.
Examples
Case("Temperature", 60, 'too cold', 80, 'just right', 'too hot')
CaseRange(CountAll("Shipping charge") % CountAll("Shipping charge", 'Total'), 2.0, 'Less than 2%', 5.0, '2% - 5%', 'More than 5%')

CountAll

CountAll(Expression[,'Level')]
Returns the count of non-null items in a field or measure; note that CountAll always returns a non-negative integer. Level can be one of the following: Current (default), ColumnGroup, ColumnTotal, RowGroup, RowTotal, Total. See Levels in Aggregate Functions for more information.
Examples
CountAll("Transaction Amount", 'RowGroup') — Counts the total number of non-null transactions in the specified group.

IF

IF(BooleanExpression,ExpressionWhenTrue[ExpressionWhenFalse])
Given a Boolean field or calculation as the first argument, returns the second argument if true; optionally returns the third argument if false. Returns null if the first argument is null.
ExpressionWhenFalse must be of the same type as ExpressionWhenTrue. For example, if ExpressionWhenTrue is a date, ExpressionWhenFalse must be a date in the same format. If ExpressionWhenFalse is not set, then a false result returns a null value.
    • You can create a BooleanExpression using the comparison operators (“==”, “!=”, “>”, “>=”, “<”, “<=”); any functions that return Boolean values (StartsWith, EndsWith, IsNull, Contains) and logical operators (and, or, not).
    • When dates are used in comparisons or the IF function, they must be the same data type, (date only, date/time, or time only). Make sure to use the correct modifier (d, ts, t) when using date constants in comparisons.
Examples
IF(Contains("Product Name", 'Soda'), 'Yes', 'No')— Uses the Contains function to see whether the product name contains the string "Soda"; if it does, sets the field value to Yes.

StartsWith

StartsWith(TextExpression1, TextExpression2)
Boolean that returns true if the first text input starts with the string specified in the second input; false otherwise.
Examples
StartsWith("Product Name", 'Q')

Average

Average(NumericExpression[,'Level'])
Returns the average value of a measure or numeric field, based on an optional level. Null values are not included.
Examples
Average("Salary", 'RowGroup')

CaseWhen

CaseWhen(BooleanExpression1, ReturnExpression1, BooleanExpression2, ReturnExpression2[,...,BooleanExpressionN, ReturnExpressionN][, DefaultReturnExpression])
Takes 2N or 2N+1 arguments: one or more pairs of Boolean expressions followed by return expressions, with an optional final return expression. Returns the expression immediately following the first true Boolean expression. If no expression is true, returns the final DefaultReturnExpression if present, null otherwise. This is the most flexible construct.
The types of all the return expressions must be compatible. For example, you can't mix numeric and text return value types.
Examples
CaseWhen("Shipped by" == 1, 'FedEx', "Shipped by" == 2, 'UPS', "Shipped by"== 3, 'USPS', 'Unknown')
CaseWhen("Temperature" <= 60, 'too cold', "Temperature" > 80, 'too hot', 'just right')

Contains

Contains(TextExpression1 ,TextExpression2])
Boolean that returns true if the first string contains the second, false otherwise.
Examples
Contains("Product Name", 'Soda')

Concatenate

Concatenate(TextExpression1[ ,TextExpression2,...,TextExpressionN])
Combines multiple text strings and/or fields into a single text field. Text strings are enclosed in single quotes; labels for fields or measures in Ad Hoc are enclosed in straight quotes.
Examples
Concatenate("Last Name", ' , ', "First Name")
Concatenate("Product Category", ' -- ', "Product Name")

Mid

Mid(TextExpression,Integer1,Integer2)
Given a text string, returns the substring starting at Integer1 with length Integer2.
Examples
Mid("Phone", 1, 3) — Given an American phone number starting with a 3-digit area code, extracts the area code.

Sum

Sum(NumericExpression[,'Level'])
The sum of all values in the range. Null values are excluded. Level can be one of the following: Current(default), ColumnGroup, ColumnTotal, RowGroup, RowTotal, Total.
Examples
Sum("Sales",'RowGroup')

Range

Range(NumericExpression[,'Level'])
The difference between the largest and smallest values of the given input.
Examples
Range("Salary",'ColumnGroup')

Time

Time('StringExpression')
Casting function that takes a String expression in the format 'HH:mm:ss.SSS' and converts it to a Time data type. The String can be any expression that returns a valid String, including a field value or an attribute retrieved with the Attribute() function.
Examples
Time('17:12:33:147')
Time(Attribute('my_time_attribute'))

Max

Max(NumericExpression|DateExpression[,'Level'])
Returns the maximum value reached by the specified field or calculation. Level can be one of the following:
    • Current (default)
    • ColumnGroup
    • ColumnTotal
    • RowGroup
    • RowTotal
    • Total
Examples
Max("Salary")

Decimal

Decimal('StringExpression')
Casting function that takes a String expression and converts it to a Decimal data type. The String can be any expression that returns a supported string, including a field value or an attribute retrieved with the Attribute() function. The Decimal() function requires a String value in decimal format, for example, '123.45'. Other Strings will return an error.
Examples
Decimal('1234.567')

Integer

Integer('StringExpression')
Casting function that takes a String expression and converts it to a Integer data type. The String can be any expression that returns a supported string, including a field value or an attribute retrieved with the Attribute() function. The Integer() function requires a String value that can be read as an integer, such as '123'. Other Strings will return an error.
Examples
Integer('123')
Integer(Attribute('my_integer_attribute'))

StdevP

StdevP(NumericExpression[,'Level'])
Standard deviation based on the entire population, taken over the values at the specified (optional) level. Null values are excluded. Level can be one of the following: Current (default), ColumnGroup, ColumnTotal, RowGroup, RowTotal, Total.
Examples
StdevP("Sales",'RowTotal')

Timestamp

Timestamp('StringExpression')
Casting function that takes a String expression in the format 'yyyy-MM-dd HH:mm:ss.SSS' and converts it to a Timestamp data type. The String can be any expression that returns a valid String, including a field value or an attribute retrieved with the Attribute() function.
Examples
Timestamp('2015-07-17 17:12:33:147')
Timestamp(Attribute('my_timestamp_attribute'))

Rank

Rank(NumericExpression)
Returns the position of each value relative to the other values after all the values are listed in order. For example, the top ten in sales are the top ten in rank. Null values are ignored.
Examples
Rank("Store Sales")

IsNull

IsNull(Expression)
Boolean that returns true if the field value is null; false otherwise.
Examples
IsNull("First Name")

MonthNumber

MonthNumber(DateExpression)
Returns the number of the month, with January = 1 and December = 12. Null values return null.
Examples
MonthNumber("Order Date")

DayName

DayName(DateExpression)
Given a date field, returns a text field with the name of the day of the week.
Examples
DayName("Open Date")— Displays the day of the week on which a store was opened.
Mode(DayName("Open Date"), 'Total')— The day of the week on which the most stores were opened.

DayNumber

DayNumber(DateExpression)
Numeric field that returns the day of the month from a date field.
Examples
DayNumber("Open Date")— Displays the day of the month on which a store was opened.

Length

Length(TextExpression)
Given a text string, returns its length. Null values return null.
Examples
Length("First Name")

Median

Median(NumericExpression|DateExpression[,'Level'])
For an odd number of values, returns the middle value after all values are listed in order. For an even number of values, returns the average of the middle two values. For example, if a field has only five instances, with values {1,1,3,10,20}, the median is 3. Level can be one of the following:
  • Current (default)
  • ColumnGroup
  • ColumnTotal
  • RowGroup
  • RowTotal
  • Total
Examples
Median("Salary")

Min

Min(NumericExpression|DateExpression[,'Level'])
Returns the minimum value reached by the specified field or calculation based on an optional level. Level can be one of the following:
  • Current (default)
  • ColumnGroup
  • ColumnTotal
  • RowGroup
  • RowTotal
  • Total
Examples
Min("Salary")

MonthName

MonthName(DateExpression)
Returns a text field with the name of the month.
Examples
MonthName("Order Date" )

PercentOf

PercentOf(NumericExpression[,'Level'])
Returns the value as a percent of total for the specified level. Null values are ignored. Note that possible values for Level are ColumnGroup, ColumnTotal, RowGroup, RowTotal, Total (default).
Calculated fields using the PercentOf function should not be used as filters; if PercentOf is used as a filter, then the total percent may not be 100.
Examples
PercentOf("")

Mode

Mode(Expression[,'Level'])
Returns the most frequent value reached by the specified input, based on an optional level. For example, if a field has only five instances with values {1,2,2,4,5}, the mode is 2. Level can be one of the following:
  • Current (default)
  • ColumnGroup
  • ColumnTotal
  • RowGroup
  • RowTotal
  • Total
Examples
Mode(DayName("Order Date",RowGroup)) — For each row group, returns the day of the week on which the most orders were placed.

Year

Year(DateExpression)
Given a date field, returns the year.
Examples
Year("Order Date" )

WeightedAverage

WeightedAverage(NumericExpression1,NumericExpression2,'Level')
Returns the weighted average for the first input weighted with respect to the second input, calculated at an optional level. Null values are excluded. Level can be one of the following: Current (default), ColumnGroup, ColumnTotal, RowGroup, RowTotal, Total.
Examples
WeightedAverage("Price","Units", 'Current') — The extended price based on the number of units.
WeightedAverage("Price","Units", 'RowGroup') — The sum of the extended price for all units in the row group.

Timestamp

Timestamp('StringExpression')
Casting function that takes a String expression in the format 'yyyy-MM-dd HH:mm:ss.SSS' and converts it to a Timestamp data type. The String can be any expression that returns a valid String, including a field value or an attribute retrieved with the Attribute() function.
Examples
Timestamp('2015-07-17 17:12:33:147')
Timestamp(Attribute('my_timestamp_attribute'))

StdevS

StdevS(NumericExpression[,'Level'])
Standard deviation based on a sample, taken over the values at the specified level. Null values are excluded. Level can be one of the following: Current (default), ColumnGroup, ColumnTotal, RowGroup, RowTotal, Total.
Examples
StdevS("Sales",'RowTotal')

EndsWith

EndsWith(TextExpression1, TextExpression2)
Boolean that returns true if the first text input ends with the string specified in the second input; false otherwise.
Examples
EndsWith("Product Name", 's')

Related content