# Special functions

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

Many synthetic expressions require two or more variables. In those cases, each of the values to be computed must have the same timestamp, otherwise the engine will not compute the expression. ***Fill missing()*** can help in these cases, as it fills the gaps where there is a missing value in any of the variables used within the expression. The syntax for this function is as follows:

&#x20;     ***`fill_missing (expression, <first_fill>, <last_fill>, <fill_value>)`***

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

### Examples

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

### $$where()$$

A conditional expression is required in some cases to generate an outcome. 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 for this function is as follows:

&#x20;       ***`where(condition, operation if true, operation if false)`***

It computes the operation *operation\_if\_true*, if the condition is true. Otherwise it will execute the *operation\_if\_false.*

### $$diff()$$

This function calculates the difference starting at the last element in a time series and the next separated by a specified number of steps. The syntax of this function is as follows.

&#x20;                                                                 ***`diff(<variable>,<steps>)`***

| Parameter |                                                                                                                                       Description                                                                                                                                       |                                     Value                                    | Mandatory |
| :-------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------: | :-------: |
|  Variable | It shows 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's identified by a variable label. |                                Variable label                                |    YES    |
|   Steps   |                                                                                           Number of places separating two components in a time series, starting with the most recent element.                                                                                           | Any positive integer no bigger than the total of elements in the time series |    YES    |

### $$shift()$$

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

&#x20;                                                             ***shift(\<variable>,\<step> )***

| Parameter |                                                                                                                                       Description                                                                                                                                       |             Value            | Mandatory |
| :-------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------: | :-------: |
|  Variable | It shows 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's identified by a variable label. |        Variable label        |    YES    |
|    Step   |                                                                                                                Number of places (+/-) the time series will be shifted to.                                                                                                               | Positive or negative integer |    YES    |

### $$Cumsum()$$

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

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