# Supported transformation functions - Data cleansing

In the formula input, you can can write basic or complex expression to transform the data.\
\
Expressions are composed of either a value, a function, an operation, or another expression in parenthesis. See below for the description of each one of them.

{% hint style="info" %}
Functions can be nested, ex: `SHA256(LOWER(user.email))`
{% endhint %}

## Available functions

| FUNCTION NAME                                                                | WHAT IT DOES                                                                                                                                                                                                                                                   | Example                                                                                                                                                                                                                                                                                                                                     |
| ---------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| EXTRACT(text, delimiter, position)                                           | Extracts from the given text the substring at the given position, after splitting by the given delimiter.                                                                                                                                                      | <p><code>EXTRACT("a-b-c", "-", 1)</code><br><em>returns <code>"b"</code></em></p><p><em><code>EXTRACT("a-b-c", "-", 0)</code></em><br><em>returns <code>"a"</code></em></p>                                                                                                                                                                 |
| SUBSTITUTE(text, search\_text,substitute\_text)                              | Substitutes new\_text for old\_text in a text string. Use SUBSTITUTE when you want to replace specific text in a text string.                                                                                                                                  | `SUBSTITUTE("The-Clone-Wars", "-", ".")`returns `"The.Clone.Wars"`                                                                                                                                                                                                                                                                          |
| REPLACE(sourceStr, pattern, replaceStr)                                      | Replaces every match of pattern (a regex expression) in sourceStr.                                                                                                                                                                                             | <p><code>REPLACE("The Clone Wars", "\s", ".")</code><br>returns <code>"The.Clone.Wars"</code><br></p><p><code>REPLACE('0123456789', '(\[0-9]{2})', '$1-')</code><br>returns</p><p><code>01-23-45-67-89</code></p>                                                                                                                           |
| SHA256(stringToHash)                                                         | Returns an SHA256 hash of the given string                                                                                                                                                                                                                     | <p><code>SHA256("test")</code><br>returns <code>"9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"</code></p>                                                                                                                                                                                                               |
| MD5(stringToHash)                                                            | Returns an MD5 hash of the given string                                                                                                                                                                                                                        | <p><code>MD5("test")</code><br>returns<br><code>"098f6bcd4621d373cade4e832627b4f6"</code></p>                                                                                                                                                                                                                                               |
| COALESCE(value1, value2, ...)                                                | Returns the first value from the list that isn’t empty                                                                                                                                                                                                         | <p><code>COALESCE("123", "hello")</code><br>returns<br><code>"123"</code></p>                                                                                                                                                                                                                                                               |
| CONCAT(string1, string2, ...)                                                | <p>Concatenate multiple values into one property. Separators could be specified:<br>CONCAT(prop1, "</p>                                                                                                                                                        | ", prop2, "                                                                                                                                                                                                                                                                                                                                 |
| TRIM(text)                                                                   | Removes all spaces, new lines, tabulations at the beginning and at the end of a text.                                                                                                                                                                          | <p><code>TRIM(" \Hello World \n")</code><br>returns<br><code>"Hello World"</code></p>                                                                                                                                                                                                                                                       |
| SELECT(sourceStr, pattern, \<position>)                                      | <p>Returns the first match of the pattern (a regex expression) in sourceStr.<br>If the third parameter is set, it will return the corresponding group inside the match.</p>                                                                                    | <p><code>SELECT("From A to Z alphabet", 'A(.\*?)Z")</code><br>returns<br><code>"A to Z"</code></p>                                                                                                                                                                                                                                          |
| LOWER(text)                                                                  | Converts all uppercase letters in a text string to lowercase                                                                                                                                                                                                   | <p><code>LOWER("ABC")</code><br>returns<br><code>"abc"</code></p>                                                                                                                                                                                                                                                                           |
| UPPER(text)                                                                  | Converts all lowercase letters in a text string to uppercase                                                                                                                                                                                                   | <p><code>UPPER("abc")</code><br>returns<br><code>"ABC"</code></p>                                                                                                                                                                                                                                                                           |
| NUMBER(value1)                                                               | Converts string to number                                                                                                                                                                                                                                      | <p><code>NUMBER("123.456")</code> returns<br><code>123.456</code></p><p><code>NUMBER("not a number")</code><br>returns<br><code>null</code></p>                                                                                                                                                                                             |
| STRING(value)                                                                | Converts a number or any value to string                                                                                                                                                                                                                       | <p><code>STRING(123.456)</code> returns<br><code>"123.456"</code></p><p><code>STRING(true)</code><br>returns<br><code>"true"</code></p><p><code>STRING(null)</code><br>returns<br><code>""</code></p>                                                                                                                                       |
| ENCODE\_BASE64(value1)                                                       | Encode a string to base64                                                                                                                                                                                                                                      | <p><code>ENCODE\_BASE64("hello")</code><br>returns<br><code>"aGVsbG8="</code></p>                                                                                                                                                                                                                                                           |
| DECODE\_BASE64(value1)                                                       | Decode a string from base64                                                                                                                                                                                                                                    | <p><code>DECODE\_BASE64("aGVsbG8=")</code><br>returns<br><code>"hello"</code></p>                                                                                                                                                                                                                                                           |
| TIMESTAMP()                                                                  | Returns the current date timestamp                                                                                                                                                                                                                             | `TIMESTAMP ()` returns a value like `1674832519845` (changes with the current time)                                                                                                                                                                                                                                                         |
| LEFT(text, length)                                                           | Returns the requested number of characters from the beginning of a string or number.                                                                                                                                                                           | <p><code>LEFT("Hello world", 3)</code><br>returns<br><code>"Hel"</code></p>                                                                                                                                                                                                                                                                 |
| RIGHT(text, length)                                                          | Returns the requested number of characters from the end of a string or number.                                                                                                                                                                                 | <p><code>RIGHT("Hello world", 2)</code><br>returns<br><code>"ld"</code></p>                                                                                                                                                                                                                                                                 |
| CHAR(text, position)\*                                                       | Returns the character at the specified position (start at 0).                                                                                                                                                                                                  | <p><code>CHAR("Hello world", 5)</code><br>returns<br><code>"o"</code></p>                                                                                                                                                                                                                                                                   |
| IF(condition,resultIfTrue,resultIfFalse)                                     | Returns the second argument if the first argument is true, or the third argument otherwise                                                                                                                                                                     | `IF(city="Paris", ListA, ListB)`                                                                                                                                                                                                                                                                                                            |
| SWITCH(expression, value1, result1, \[value2, result2], ..., \[default])     | Evaluates the expression against a list of values and returns the result corresponding to the first match. If no match is found, returns the optional default value, or null if no default is provided.                                                        | <p><code>SWITCH(country, "FR", "France", "US", "United States", "Unknown")</code><br>returns <code>"France"</code> if country is <code>"FR"</code>, <code>"Unknown"</code> if no match</p><p><code>SWITCH(LOWER(status), "active", 1, "inactive", 0)</code><br>returns <code>1</code> if status is <code>"Active"</code></p>                |
| ISEMPTY(value)                                                               | Returns whether the value is empty or not                                                                                                                                                                                                                      | <p>ISEMPTY("abc")<br>returns<br>false</p>                                                                                                                                                                                                                                                                                                   |
| SIZE(array or object)                                                        | Returns the number of elements in the list                                                                                                                                                                                                                     | <p><code>SIZE(items)</code><br>returns the number of items</p>                                                                                                                                                                                                                                                                              |
| LENGTH(string)                                                               | Returns the number of characters in the string                                                                                                                                                                                                                 | <p><code>LENGTH("abc")</code><br>returns<br><code>3</code></p>                                                                                                                                                                                                                                                                              |
| SUBSTRING(text, pos1, pos2)                                                  | Returns a new string composed of the characters between the two position defined by pos1 and pos2. The positions start at 0. If a negative number is given, the positions start from the end of the string. If pos2 is omitted, the end of the string is used. | <p><code>SUBSTRING("hello beautiful world", 6, 14)</code><br>returns<br><code>"beautiful"</code></p>                                                                                                                                                                                                                                        |
| JSON\_PARSE(string)                                                          | <p>Parse a JSON string (works for both objects and arrays).<br>Useful to create an object or array property that will contain all the properties or elements present inside a stringified JSON you may have inside another event property</p>                  | <p><code>JSON\_PARSE('{"optin":1,"message":"ok"}')</code><br>returns an object with an <code>optin</code> key equals to <code>1</code> and <code>message</code> key equals to <code>"ok"</code></p><p><code>JSON\_PARSE('\["morgan","justine"]')</code><br>returns an array containing <code>"morgan"</code> and <code>"justine"</code></p> |
| JSON\_STRINGIFY(value, defaultValue)                                         | <p>Stringify a value into JSON.<br>(Optional) You can specify a <code>defaultValue</code> in case <code>value</code> is <code>undefined</code></p>                                                                                                             | <p><code>JSON\_STRINGIFY({"message":"ok"})</code><br>returns a string that equals <code>'{"message":"ok"}'</code></p>                                                                                                                                                                                                                       |
| VALUE\_FROM\_JSON(string, path)                                              | Extract a value from a data string formatted in JSON                                                                                                                                                                                                           | <p><code>VALUE\_FROM\_JSON('{"info":{"code":42},"message":"ok"}', 'info.code')</code></p><p>returns</p><p><code>42</code></p>                                                                                                                                                                                                               |
| GET(property name)                                                           | <p>GET(name) return the value of the given event property.<br>Useful for property names that contains a special character like "-"</p>                                                                                                                         | <p><code>GET("droid-type")</code></p><p>returns</p><p><code>"r2d2"</code><br><br><code>GET("invalidProperty")</code><br>returns<br><code>null</code></p>                                                                                                                                                                                    |
| GETHEADER(name)                                                              | GETHEADER(name) return the value of the given http header                                                                                                                                                                                                      | <p><code>GETHEADER("Accept-Language")</code></p><p>returns</p><p><code>"fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3"</code></p>                                                                                                                                                                                                                     |
| GETCOOKIE(name)                                                              | GETCOOKIE(name) return the value of the given http cookie                                                                                                                                                                                                      | <p><code>GETCOOKIE("cart")</code></p><p>returns</p><p><code>"empty"</code></p>                                                                                                                                                                                                                                                              |
| DATEPARSE(str, format)                                                       | Takes a date in a specific format and converts it to a proper date                                                                                                                                                                                             | <p><code>DATEPARSE('2023-05-23', 'yyyy-MM-dd')</code> returns<br><code>2023-05-23T00:00:00.000+01:00</code><br><a href="/pages/fDzulmwgww0t74EaJip7">see more about format</a></p>                                                                                                                                                          |
| DATEFORMAT(date, format)                                                     | Takes a date in a ISO-8601 or timestamp format and converts it to the specified format                                                                                                                                                                         | <p><code>DATEFORMAT('2023-01-31T23:59:00.000+01:00', 'yyyy-MM-dd HH:mm')</code> returns<br><code>2023-01-31 23:59</code><br><a href="/pages/fDzulmwgww0t74EaJip7">see more about format</a></p>                                                                                                                                             |
| DATEADD(date, interval, unit)                                                | Adds the interval as a unit (years, months, weeks, days, hours, minutes, seconds)                                                                                                                                                                              | <p><code>DATEADD('2023-01-23T00:05:00.000+01:00', 3, 'days')</code><br>returns<br><code>2023-01-26T00:05:00.000+01:00</code></p>                                                                                                                                                                                                            |
| DATESUB(date, interval, unit)                                                | Removes the interval as a unit (years, months, weeks, days, hours, minutes, seconds)                                                                                                                                                                           | <p><code>DATESUB('2023-01-23T00:05:00.000+01:00', 3, 'days')</code><br>returns<br><code>2023-01-20T00:05:00.000+01:00</code></p>                                                                                                                                                                                                            |
| DATEDIFF(date1, date2, unit)                                                 | Returns the interval between two dates in unit                                                                                                                                                                                                                 | <p><code>DATEDIFF('2023-01-20T00:05:00.000+01:00', '2023-01-23T00:05:00.000+01:00', 'days')</code><br>returns<br><code>0</code></p>                                                                                                                                                                                                         |
| AGE(date, unit)                                                              | Returns the elapsed time since the given date in unit (if unit is not specified, years by default)                                                                                                                                                             | <p><code>AGE('2022-01-20T00:05:00.000+01:00')</code><br>returns<br><code>1</code> in 2023</p>                                                                                                                                                                                                                                               |
| YEAR(date), MONTH(date), DAY(date), HOUR(date), MINUTES(date), SECONDS(date) | Returns the dates specified unit                                                                                                                                                                                                                               | <p><code>YEAR('2023-01-20T00:05:00.000+01:00')</code><br>returns<br><code>2023</code>, <code>MONTH</code> for the same date returns <code>1</code>, etc</p>                                                                                                                                                                                 |
| TIMEZONE(date)                                                               | Returns the timezone in UTC                                                                                                                                                                                                                                    | <p><code>TIMEZONE('2023-01-20T00:05:00.000+01:00')</code><br>returns<br><code>UTC+1</code></p>                                                                                                                                                                                                                                              |
| TODAY()                                                                      | Returns the current date in ISO-8601                                                                                                                                                                                                                           | `TODAY()` might return `2023-01-20T00:05:00.000+01:00`, assuming the current date is the 20th of January 2023                                                                                                                                                                                                                               |

## Operators

| **Operator**                            | **Description**                                                                                                                                                                                                      | **Example**                   |
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- |
| =, ==                                   | <p>Return true if the left part is equal to the right part.<br>If one of the part is an array, check if some values are in equal between the two parts.</p>                                                          | `gender = "F"`                |
| !=, <>                                  | Inverse of the above                                                                                                                                                                                                 | `gender != "F"`               |
| AND or &&                               | Do a boolean AND between the two parts                                                                                                                                                                               | `age >18 AND gender = "M"`    |
| OR or \|\|                              | Do a boolean OR between the two parts                                                                                                                                                                                | `age >18 OR gender = "M"`     |
| NOT(expression)                         | Do a boolean NOT on the expression                                                                                                                                                                                   | `NOT(age>18)`                 |
| IN()                                    | Returns true if the left is equals to at least one value on the right.                                                                                                                                               | `currency IN("€", "$", "£")`  |
| !IN                                     | Returns true if none of the value on the right is equal to the value on the left.                                                                                                                                    | `currency !IN("€", "$", "£")` |
| <, >, <=, or >=                         | <p>Compare two values<br>If one of the values is an array, check that at least one of the value match.</p>                                                                                                           | `age > 18`                    |
| BETWEEN()                               | Check if the left value is between the two values passed as arguments.If the left value is an array, check that at least one of the value match.                                                                     | `age BETWEEN(7, 80)`          |
| EXISTS()                                | Check if the property exists                                                                                                                                                                                         | `EXISTS(my_property)`         |
| \~ or !\~                               | <p>Check if the left value match the regex in the right value (or doesn’t match if !\~). The regex language is the javascript one.<br>If the left value is an array, check that at least one of the value match.</p> | `email ~ "@example.com"`      |
| STARTSWITH(), ENDSWITH(), or CONTAINS() | <p>Check if the left value starts with, ends with, or contains the right value.<br>If the left value is an array, check that at least one of the value match.</p>                                                    | `lastname STARTSWITH("DE")`   |
| \* or /                                 | Multiplication or division                                                                                                                                                                                           | `3*5`                         |
| + or -                                  | Addition / concatenation or substraction                                                                                                                                                                             | `1+2`                         |

## Aggregations

| FUNCTION NAME              | WHAT IT DOES                                                                                                   | Example                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| -------------------------- | -------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| AVERAGE(array, expression) | Calculates the average of values in an array or from an expression. The expression parameter is optional.      | <p><code>AVERAGE(\[0, null, 10])</code><br>Returns 5 (null values are ignored)</p><p><code>AVERAGE(items, price)</code><br>Returns average of price of each items (null values are ignored)</p><p><code>AVERAGE(items, EXTRACT(price, "€", 0))</code><br>Extract numeric value before € and calculate average</p><p><code>AVERAGE(items, IF(returned\_quantity = 0, NUMBER(EXTRACT(product.price, "€",0)), 0))</code><br>Average price only for non-returned items</p> |
| MIN(array, expression)     | Returns the minimum value from an array or from an expression. The expression parameter is optional.           | <p><code>MIN(\[null, 15, 8])</code><br>Returns 8 (ignores null)</p><p><code>MIN(orders, DATEPARSE(order\_date, "yyyy-MM-dd"))</code><br>Find the earliest order date</p><p><code>MIN(items, IF(status = "active", price, 999999))</code><br>Minimum price among active items only</p>                                                                                                                                                                                  |
| MAX(array, expression)     | Returns the maximum value from an array or from an expression. The expression parameter is optional.           | <p><code>MAX(\[null, 15, 8])</code><br>Returns 15 (ignores null)</p><p><code>MAX(users, AGE(birth\_date))</code><br>Find the age of the oldest user</p><p><code>MAX(transactions, IF(type = "purchase", amount, 0))</code><br>Maximum purchase amount</p>                                                                                                                                                                                                              |
| SUM(array, expression)     | Calculates the sum of values in an array or from an expression. The expression parameter is optional.          | <p><code>SUM(items, quantity \* NUMBER(EXTRACT(unit\_price, "$", 0)))</code><br>Calculate total value of all items</p><p><code>SUM(\[5, null, 15])</code><br>Returns 20 (ignores null)</p><p><code>SUM(items, IF(returned\_quantity = 0, NUMBER(EXTRACT(product.price, "€",0)), 0))</code><br>Sum prices only for non returned quantity items condition</p>                                                                                                            |
| COUNT(array, expression)   | Counts the number of non empty values in an array or from an expression. The expression parameter is optional. | <p><code>COUNT(users, IF(ISEMPTY(email), null, email))</code><br>Count customers with non empty email condition</p><p><code>COUNT(items, IF(status = "completed" AND amount > 100, 1, null))</code><br>Count completed items over 100</p>                                                                                                                                                                                                                              |

## Examples

1. Scenario: Create a Flag that shows if Consumers’ Primary Address is in California\
   `IF(EXTRACT(city_state, '-', 1) == " CA", "TRUE", "FALSE")`
2. Scenario: Set a value with multiple conditions. The value equals "12345" for country FR and environment prod, else equals 98888 for country DE and environment dev: \\

{% code overflow="wrap" %}

````
```xquery
IF(country = "FR" AND environment_work = "prod", "12345", IF(country = "DE" AND environment_work = "dev", "98888")
```
````

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.commandersact.com/features/data-quality/data-cleansing/supported-transformation-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
