Building Expressions
About Building Expressions
Expressions are used in XM Discover to filter your data and to transform data fields. For example, you can use expressions to change date field formatting, evaluate data for empty records, and perform mathematical calculations.
Expressions can contain 4 components:
- Constants: You can use constant numbers, text strings, and dates to perform math and comparison operations.
- Fields: You can use numeric, text, and date fields available via a specific data connector to perform math and comparison operations on those fields.
- Functions: You can use functions to perform transformations on your fields and constants.
- Operators: You can use operators to perform math operations and compare your fields and constants.
Constants
Constants are numbers, text strings, and dates that you can use to perform math operations and comparisons. For example, you can use a constant date, such as the date you launched your annual CX survey, to calculate how old a survey taker’s response is.
Notes on using constants:
- Text strings are treated as case-sensitive. Use UPPER or LOWER functions as a workaround for case-sensitivity.
- Text strings and dates must be put inside quotation marks.
- Numbers don’t need quotation marks (a number inside quotation marks is treated as text).
Fields
Fields are the data fields available from your inbound data connector. This includes numeric, text string, and date fields, which you can use to perform math operations and comparisons. For example, if your dataset contains a field for a customer’s birthday, you can calculate how old the customer is.
To add a field, drag it from the Fields section onto the Expression box.
Field types are labeled with the following icons:
Functions
You can use numeric, text, and date functions listed in this section.
To add a function, drag it from the Functions section onto the Expression box.
Function types are labeled with the following icons:
ABS
Returns the absolute value of a number.
Syntax: ABS(number)
COALESCE
Returns the first non-null value in a list.
Syntax: COALESCE(value1, value2, value3)
CEILING
Returns the rounded up value of a number.
Syntax: CEILING(number)
CONCAT
Joins several text strings into one.
Syntax: CONCAT(“string 1”, “string 2”, “string 3”)
CONTAINS
Returns true if a text string (“within text”) contains another text string (“find text”). Otherwise returns false.
Syntax: CONTAINS(“within text”, “find text”)
CONVERT_TO_TIMEZONE
Converts date and time from one time zone to another.
Syntax: CONVERT_TO_TIMEZONE(“date and time”, “time zone to convert from”, “time zone to convert to”)
Notes on date formatting:
- Provide the time zones using their tz database names.
- Provide the date and time in ISO 8601 or as yyyy-mm-dd hh:mm:ss.
- If date and time are provided with time zone information, it is ignored in favor of the “time zone to convert from” parameter.
- The converted date uses the ISO 8601 format. Use the TODATE function if you need to change that.
DATETONUMBER
Converts a date field to a Unix timestamp.
Syntax: DATETONUMBER(“date”)
DATETOTEXT
Converts a date field to a date string in the requested format.
Syntax: DATETOTEXT(“date”, “date format”)
DAYOFWEEK
Returns the day of the week corresponding to a date.
Syntax: DAYOFWEEK(“date”)
DYNAMICLOOKUP
Replaces field’s values using a dynamically loaded lookup table. Lookup values are taken from the field specified in the first argument (“field name”).
Syntax: DYNAMICLOOKUP(“field name”, “value”)
ELEMENTOFARRAY
Extract the nth value from an array, then map array values to attributes in XM Discover.
Syntax: ELEMENTOFARRAY([“array element 1”, “array element 2”, “array element 3”], number)
Notes:
- If the input is not an array, the function throws an error.
- If the number is greater than the size of an array, the function returns null. Numbers in increasing order start from 0.
FIELD
Wraps field names that contain spaces or dots.
Syntax: FIELD(“field name”)
FIND
Locates one text string (“find text”) within another text string (“within text”) and returns the number of the starting position of “find text” from the first character of “within text” (starting from 1).
Allows specifying the order of the character in “within text” at which to start the search (starting from 1). If you omit order_number, it is assumed to be 1.
Returns -1 if “find text” is not found in “within text.”
Syntax: FIND(“find text”, “within text”, order_number)
FINDFIRST
Locates the first occurrence of a text string (“find text”) within another text string (“within text”) and returns the number of the starting position of “find text” from the first character of “within text” (starting from 1).
Syntax: FINDFIRST(“within text”, “find text”)
FINDLAST
Locates the last occurrence of a text string (“find text”) within another text string (“within text”) and returns the number of the starting position of “find text” from the first character of “within text” (starting from 1).
Syntax: FINDLAST(“within text”, “find text”)
FLOOR
Returns the rounded down value of a number.
Syntax: FLOOR(number)
GENERATE_ID
Generates a unique ID. This function does not require additional parameters.
Syntax: GENERATE_ID()
GETDAYSBETWEEN
Returns the number of days between two dates.
Syntax: GETDAYSBETWEEN(“start date”, “end date”)
GETHOURSBETWEEN
Returns the number of hours between two dates.
Syntax: GETHOURSBETWEEN(“start date”, “end date”)
GETMINUTESBETWEEN
Returns the number of minutes between two dates.
Syntax: GETMINUTESBETWEEN(“start date”, “end date”)
IF
Returns one value if the statement is true, and another value if the statement is false.
Syntax: IF(statement, “value if true”, “value if false”)
ISBLANK
Returns TRUE when a field is empty, and FALSE when a field is not empty.
Syntax: ISBLANK(“field name”)
JOINARRAY
Joins an array of items into a single string with a specified delimiter.
Synax: JOINARRAY(array, delimiter, escape, skipNull, removeDuplicates)
In the above syntax:
- array: array of strings or numbers to join.
- delimiter: delimiter character to use when joining the array.
- escape: if true, preserve quoted elements.
- skipNull: if true, skip elements that are null.
- removeDuplicated: If true, remove duplicate elements.
LEN
Returns the number of letters in a text string.
Syntax: LEN(“text”)
Syntax: LEN([“text1”, text2])
LOWER
Converts text to lowercase.
Syntax: LOWER(“text”)
MD5HASH
Applies MD5 hash to the input data to generate unique IDs based on that data.
Syntax: MD5HASH(“text”)
MID
Returns a specific number of characters from a text string, starting at the position you specify.
The position (order_number) starts from 1.
Syntax: MID(“text”, order_number, characters_number)
MOD
Returns the remainder when a number is divided by a divisor.
Syntax: MOD(number, divisor)
NOW
Returns the current date and time. This function does not require additional parameters.
Syntax: NOW()
NUMBERTODATE
Converts a Unix timestamp to a date field.Unix timestamp is the number of milliseconds that have elapsed since January 1, 1970.
Syntax: NUMBERTODATE(number)
NUMBERTOTEXT
Converts a number to text.
Syntax: NUMBERTOTEXT(number)
POW
Returns the result of x to the power of y.
Syntax: POW(x, y)
PROPERCASE
Capitalizes the first letter of a text string.
Syntax: PROPERCASE(“text”)
Example: PROPERCASE(“practice makes perfect”) resolves to Practice makes perfect.
RANDOM
Returns a random real number between 0 and 1. This function does not require additional parameters.
Syntax: RANDOM()
RANDOMBETWEEN
Returns a random integer number between two numbers.
Syntax: RANDOMBETWEEN(minimum number, maximum number)
REMOVEHTMLTAGS
Removes HTML tags from a text string.
Syntax: REMOVEHTMLTAGS(“text”)
REPLACE
Replaces part of a text string (“old text”) with a different text string (“new text”) a set number of times (number_of_replacements) based on the occurrence number of “old text” within “text” (occurrence_number).
Syntax: REPLACE(“text”, “old text”, “new text”, occurrence_number, case_sensitivity, number_of_replacements)
REPLACEBYINDEX
Replaces part of a text string (“old text”) with a different text string (“new text”), based on the number of characters you specify.
The position (order_number) starts from 1.
Syntax: REPLACE(“old text”, order_number, characters_number, “new text”)
REPLACEBYREGEXP
Replaces text values using regular expressions.
Syntax: REPLACEBYREGEXP(“text”, “regular expression for pattern matching”, “regular expression for replacement value”)
ROUND
Returns the value of a number rounded to the nearest integer.
Syntax: ROUND(number)
SEARCH
Locates one text string (“find text”) within another text string (“within text”) and returns the number of the starting position of “find text” from the first character of “within text” (starting from 1).
Allows specifying the order of the character in “within text” at which to start the search (starting from 1). If you omit order_number, it is assumed to be 1.
Returns -1 if “find text” is not found in “within text.”
Syntax: SEARCH(“find text”, “within text”, order_number)
SPLIT
Splits a given string into an array of substrings based on a specified delimiter. This function can be used as a parameter for JOINARRAY or ELEMENTOFARRAY functions since the output value must be a string.
Syntax: SPLIT(“string_to_split”, “_”)
SUBSTITUTE
Substitutes “old text” with “new text” in a text string.
Allows specifying which occurrence of “old text” you want to replace. If you specify occurrence_number, only that instance of “old text” is replaced. Otherwise, every occurrence of “old text” in text is changed to “new text.”
Syntax: SUBSTITUTE(“text”, “old text”, “new text”, occurrence_number)
SUBSTR
Returns a portion of a text string starting from the first character of the string and up to the specified number of characters (starting from 1).
Syntax: SUBSTR(“text”, order_number)
TITLECASE
Capitalizes the first letter of each word in a text string.
Syntax: TITLECASE(“text”)
TEXTTODATE
Converts a date string to a date field in the following format: YYYY-MM-DDThh:mm:ssZ.
Syntax: TEXTTODATE(“date”, “date format”)
TEXTTONUMBER
Converts text to number.
Syntax: TEXTTONUMBER(text)
TRANSLATE
Translates inbound data.
Syntax: TRANSLATE (“source language code”, “target language code”, “text”).
Notes:
- This function is only available for accounts with Google Translate API credentials
- For a full list of supported language codes, please refer to Google Cloud Translation documentation.
TRIMLEFT
Removes spaces from the left end of a text string.
Syntax: TRIMLEFT(“text”)
TRIMRIGHT
Removes spaces from the right end of a text string.
Syntax: TRIMRIGHT(“text”)
UPPER
Converts text to uppercase.
Syntax: UPPER(“text”)
Operators
You can use arithmetic and comparison operators listed in the following tables.
Arithmetic Operators
Operator | Description |
+ | Add |
– | Subtract or negate |
* | Multiply |
/ | Divide |
For example, the following expression will return the average of the two numeric values.
(RATING1 + RATING2) / 2
Comparison Operators
Operator | Description |
== | Equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
!= | Not equal to |
For example, the following expression will return “bad rating” when RATING is less than or equal to 3, and “good rating” when RATING is greater than 3.
IF(RATING <= 3, “bad rating”, “good rating”)
Logical Operators
Use logical operators to specify multiple conditions when using the IF function or job filters.
Operator | Description |
and | Condition is true if all the conditions separated by “and” are true. |
in | Condition is true if there is a match with any of the listed values.
Syntax: in[“value 1”, “value 2”, “value 3”] Qtip: Use this instead of multiple “or” operators.
|
or | Condition is true if any of the conditions separated by “or” are true. |
For example, the following expression will return “Mediterranean” if the COUNTRY field contains any of the specified values. Otherwise it will return “Non-Metiterranean.”
IF(LOWER(COUNTRY) in [“france”, “portugal”, “italy”, “spain”, “greece”, “malta”, “cyprus”], “Mediterranean”, “Non-Mediterranean”)
Syntax Tips
Follow these tips to avoid invalid expressions:
- When combining several functions in one expression, make sure the number of opening and closing parentheses matches.
Example: IF(GETDAYSBETWEEN(feedback_date, response_date) > 3, “delayed”, “not delayed”).
- For each function used in the expression, all required arguments need to be present. For example, the TODATE function requires both the date and the date format arguments to work correctly.
- Be sure to use the correct data type supported by a function or an arithmetic or comparison operation. For example, the ABS function needs a number to work correctly and will not work on a text or date field.
- If there are dots (“.”) in your expression, wrap them with the FIELD function and quotation marks (either ‘single’ or “double”). For example, if you have a field named “agentParticipants.0.agentLoginName,” here’s how you wrap it in a potential transformation:
IF(CONTAINS(LOWER(FIELD(“agentParticipants.0.agentLoginName”)), “bot”),”YES”, “NO”).