# Special functions

### $$fill$$\_$$missing()$$

Many synthetic expressions require two or more variables. In these cases, each value must have the same timestamp. Otherwise, the engine cannot compute the expression. ***fill\_missing()*** helps by filling gaps where any variable in the expression has a missing value. The syntax is as follows:

***`fill_missing(expression, <first_fill>, <last_fill>, <fill_value>)`***

|  Parameters  |                                                                                     Description                                                                                     |                                                                                                                               Value                                                                                                                              | Mandatory |
| :----------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :-------: |
|  Expression  |                                                                              The expression to compute                                                                              |                                                                                                                                N/A                                                                                                                               |    YES    |
| `first_fill` |                                                     Determines how the gap should be filled. By default, it is set to $$ffill$$.                                                    |          <p><span class="math">ffill</span>: Fills gaps forward, starting from the most recent data point.</p><p><span class="math">bfill</span>: Fills gaps backward, starting from the oldest data point.</p><p><em>None</em>: No fill is applied.</p>         |     NO    |
|  `last_fill` | Determines how the gap should be filled at the end of the time series. This is useful when you need to fill all gaps in the variables. By default, the function is set to $$none$$. | <p><span class="math">ffill</span>: Fills gaps forward, starting from the most recent data point.</p><p><span class="math">bfill</span>: Fills gaps backward, starting from the oldest data point.</p><p><span class="math">none</span>: No fill is applied.</p> |     NO    |
| `fill_value` |                                                                     Value used to fill blanks in the variables.                                                                     |                                                                                                                       Any integer or float.                                                                                                                      |     NO    |

### Examples

|                           Expression                           |                                                                             Description                                                                             |
| :------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------: |
|             `fill_missing(temperature * humidity)`             |           Fills missing gaps, starting from the most recent data point in the `temperature` and `humidity` time series, and then calculates their product.          |
|     `fill_missing(temperature * humidity, fill_value = 1)`     | Fills missing gaps with the value `1`, starting from the most recent data point in the `temperature` and `humidity` time series, and then calculates their product. |
| `fill_missing(temperature * humidity, "first_fill" = "bfill")` |             Fills missing gaps, starting from the oldest data point in the `temperature` and `humidity` time series, and then calculates their product.             |

### $$where()$$

Some use cases require a conditional expression. The ***where()*** function is an if-else statement that executes one of two actions depending on whether the input condition is true or false. The syntax is as follows:

***`where(condition, operation if true, operation if false)`***

It computes *operation\_if\_true* when the condition is true. Otherwise, it executes *operation\_if\_false*.

### $$diff()$$

This function calculates the difference between an element in a time series and another element separated by a specified number of steps. The syntax is as follows:

***`diff(<variable>,<steps>)`***

| Parameter |                                                                                                                                         Description                                                                                                                                        |                                         Value                                        | Mandatory |
| :-------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------: | :-------: |
|  Variable | Represents the measurements of a sensor as a [time series](https://docs.ubidots.com/v1.6/reference/time-series) of data points that vary over time. Also known as ["dots"](https://help.ubidots.com/en/articles/636672-plans-billing-what-are-dots), it is identified by a variable label. |                                    Variable label                                    |    YES    |
|   Steps   |                                                                                           Number of positions separating two elements in the time series, starting from the most recent element.                                                                                           | Any positive integer no greater than the total number of elements in the time series |    YES    |

### $$shift()$$

This function returns the variable's values in the time series shifted by the given number of steps. The syntax is as follows:

***`shift(<variable>, <step>)`***

| Parameter |                                                                                                                                         Description                                                                                                                                        |             Value            | Mandatory |
| :-------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------: | :-------: |
|  Variable | Represents the measurements of a sensor as a [time series](https://docs.ubidots.com/v1.6/reference/time-series) of data points that vary over time. Also known as ["dots"](https://help.ubidots.com/en/articles/636672-plans-billing-what-are-dots), it is identified by a variable label. |        Variable label        |    YES    |
|    Step   |                                                                                                               Number of positions (+/-) by which the time series is shifted.                                                                                                               | Positive or negative integer |    YES    |

### $$cumsum()$$

This function returns the cumulative sum of a time series. The syntax is:

$$cumsum(x)$$, where $$x$$ is the variable to calculate.


---

# 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://dev.ubidots.com/synthetic-variables/expressions/special-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.
