# Mathematical functions and operations

Several tasks such as **Aggregator**, **Condition,** and **Notification** are empowered by an extended mathematical editor that supports a powerful set of mathematical functions and operations.

## Mathematical functions

<figure><img src="/files/TtPAuN8CPHuTcs0nt104" alt=""><figcaption></figcaption></figure>

Most probably you are already familiar with them and used these ones in **SQL databases**, **Microsoft Excel**, or **Google Spreadsheets**.&#x20;

### Array functions

| Formula                | Meaning                               | Example     |
| ---------------------- | ------------------------------------- | ----------- |
| **AVG(field\_name)**   | Average value in the input data set   | AVG(amount) |
| **SUM(field\_name)**   | A sum of values in the input data set | SUM(amount) |
| **COUNT()**            | Number of rows in the input data set  |             |
| **FIRST(field\_name)** | First value in the input data set     |             |
| **LAST(field\_name)**  | Last value in the input data set      |             |
| **MAX(field\_name)**   | Max value in the input data set       | MAX(amount) |
| **MIN(field\_name)**   | Min value in the input data set       | MIN(amount) |

### Single number functions

<table><thead><tr><th>Formula</th><th>Meaning</th><th>Example</th></tr></thead><tbody><tr><td><strong>ROUND(field_name, precision, "floor|ceil")</strong></td><td>Round a number up or down with a certain precision</td><td>ROUND(tax, 2, "floor")</td></tr><tr><td><strong>ABS(field_name)</strong></td><td>The absolute value of a number</td><td></td></tr><tr><td><strong>NEG(field_name)</strong></td><td>Inverts the sign of a number. From "-" to "+" and other way around</td><td></td></tr><tr><td><strong>SIGN(field_name)</strong></td><td><p>Returns:</p><pre><code>-1 if d &#x3C;  0
 0 if d == 0
+1 if d >  0
</code></pre></td><td></td></tr><tr><td><strong>STRING(field_name)</strong></td><td>Converts number to string</td><td></td></tr><tr><td><strong>INT(field_name)</strong></td><td>Returns an integer part of a decimal number</td><td></td></tr></tbody></table>

### Other functions

| Formula     | Meaning                                                     | Example                 |
| ----------- | ----------------------------------------------------------- | ----------------------- |
| **ENV()**   | Use environment variable in a format of ENV\_variable\_name | ENV\_REVENUE\_THRESHOLD |
| **NOW()**   | Current timestamp                                           |                         |
| **INPUT()** | The entire input data set as JSON                           |                         |

### Metric functions

Also, there's an additional set of functions available for [Metrics](/metrics/metric-management.md) only:

<figure><img src="/files/Yr9dD0Uv9M7cgrdxHLs9" alt=""><figcaption></figcaption></figure>

| Formula                                         | Meaning                                                                                                                                               | Example                                                                      |
| ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| **METRIC\_AVG("period", duration)**             | This function returns an average value of this metric within a selected period of time                                                                | <p>METRIC\_AVG("day", 5)<br>or <br>METRIC\_AVG("month", 3)<br>etc.</p>       |
| **METRIC\_QUANTILE(level, "period", duration)** | This function returns a metric quantile within a selected period of time                                                                              | METRIC\_QUANTILE(0.25, "day", 7)                                             |
| **METRIC\_MEDIAN("period", duration)**          | <p>This function returns a median metric value within a selected period of time.</p><p></p><p>Alias for METRIC\_QUANTILE(0.5, "period", duration)</p> | <p>METRIC\_MEDIAN("day", 5)<br>or <br>METRIC\_MEDIAN("month", 3)<br>etc.</p> |

## Arithmetical and logical operations

The following list is supported:

### Arithmetic

+\
-\
/\
\*\
%\
\*\*

### Comparison

\==\
\===\
!=\
!==\
\>\
<\
\>=\
<=\
\>==\
<==

### Boolean operators

||\
&&\
!

### Bitmasking

|\
&\
^\
<<\
\>>

## Regular expression comparison

The following two operations are supported for searching in strings with regular expressions

\=\~     // means a regular expression match is found\
!\~      // means a regular expression match is not found

For example, the following string placed in the Condition or Aggregator will return `true`

```regex
"Mountaineering is the best sport in the world. Or engineering?" =~ "[(Engin|Mountain)]eering\\s"
```

Supported syntax for regular expressions: <https://pkg.go.dev/regexp/syntax>.

## Formatting of dates

Comparison operations can also be used for comparing dates. The following formats are supported:

* time.UnixDate,&#x20;
* time.RubyDate,&#x20;
* time.Kitchen,&#x20;
* time.RFC3339,&#x20;
* time.RFC3339Nano,&#x20;
* "2006-01-02", // RFC 3339&#x20;
* "2006-01-02 15:04", // RFC 3339 with minutes&#x20;
* "2006-01-02 15:04:05", // RFC 3339 with seconds&#x20;
* "2006-01-02 15:04:05-07:00", // RFC 3339 with seconds and timezone&#x20;
* "2006-01-02T15Z", // ISO8601 with hour without UTC offset&#x20;
* "2006-01-02T15:04Z", // ISO8601 with minutes without UTC offset&#x20;
* "2006-01-02T15Z0700", // ISO8601 with hour&#x20;
* "2006-01-02T15:04:05Z", // ISO8601 with seconds without UTC offset&#x20;
* "2006-01-02T15:04Z0700", // ISO8601 with minutes&#x20;
* "2006-01-02T15:04:05Z0700", // ISO8601 with seconds&#x20;
* "2006-01-02T15:04:05.999999999Z", // ISO8601 with nanoseconds without UTC offset&#x20;
* "2006-01-02T15:04:05.999999999Z0700", // ISO8601 with nanoseconds


---

# 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://docs.ylem.co/pipelines/mathematical-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.
