> For the complete documentation index, see [llms.txt](https://dev.ubidots.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dev.ubidots.com/synthetic-variables/examples/special-functions.md).

# Special functions

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

<table><thead><tr><th width="588.6887159533075" align="center">Syntax</th><th width="244" align="center">Example</th></tr></thead><tbody><tr><td align="center"><em><strong><code>fill_missing (expression, &#x3C;first_fill>, &#x3C;last_fill>, &#x3C;fill_value>)</code></strong></em></td><td align="center"><span class="math">fill</span>_<span class="math">missing(A+B)</span></td></tr></tbody></table>

To better understand this function, suppose you have a set of variables that you want to sum.

Variables A, B, C, and D do not have data for all timestamps, as shown in the table below. Use the `fill_missing()` function to fill the gaps and perform the calculation. The following examples are based on this table:

<table><thead><tr><th width="230" align="center">Date</th><th width="150" align="center">A</th><th width="150" align="center">B</th><th width="150" align="center">C</th><th width="150" align="center">D</th><th data-hidden align="center">Output</th></tr></thead><tbody><tr><td align="center">07/12/2020 00:00:04</td><td align="center">5</td><td align="center">-</td><td align="center">4</td><td align="center">5</td><td align="center">16</td></tr><tr><td align="center">07/12/2020 00:00:03</td><td align="center">3</td><td align="center">2</td><td align="center">3</td><td align="center">4</td><td align="center">12</td></tr><tr><td align="center">07/12/2020 00:00:02</td><td align="center">-</td><td align="center">4</td><td align="center">-</td><td align="center">2</td><td align="center">15</td></tr><tr><td align="center">07/12/2020 00:00:01</td><td align="center">4</td><td align="center">3</td><td align="center">5</td><td align="center">-</td><td align="center">-</td></tr></tbody></table>

$$fill$$\_$$missing(A + B + C + D)$$

By default, the `fill_missing()` expression sets the *`first_fill`* parameter to *`"ffill"`*. This means the function fills the gaps forward, starting at a point where it has enough data to fill the gaps for all involved time series. The result is as follows:

<table><thead><tr><th width="230" align="center">Date</th><th width="150" align="center">A</th><th width="150" align="center">B</th><th width="150" align="center">C</th><th width="150" align="center">D</th><th width="199.11669921875" align="center">Output</th></tr></thead><tbody><tr><td align="center">07/12/2020 00:00:04</td><td align="center">5</td><td align="center">2</td><td align="center">4</td><td align="center">5</td><td align="center">16</td></tr><tr><td align="center">07/12/2020 00:00:03</td><td align="center">3</td><td align="center">2</td><td align="center">3</td><td align="center">4</td><td align="center">12</td></tr><tr><td align="center">07/12/2020 00:00:02</td><td align="center">4</td><td align="center">4</td><td align="center">5</td><td align="center">2</td><td align="center">15</td></tr><tr><td align="center">07/12/2020 00:00:01</td><td align="center">4</td><td align="center">3</td><td align="center">5</td><td align="center">-</td><td align="center">-</td></tr></tbody></table>

#### $$fill$$\_$$missing(A + B + C + D, first$$\_$$fill="bfill")$$

You can also set *`first_fill`* to *`"bfill"`*. In that case, the gaps are filled as follows:

<table><thead><tr><th width="225" align="center">Date</th><th width="150" align="center">A</th><th width="150" align="center">B</th><th width="150" align="center">C</th><th width="150" align="center">D</th><th width="150" align="center">Output</th></tr></thead><tbody><tr><td align="center">07/12/2020 00:00:04</td><td align="center">5</td><td align="center">-</td><td align="center">4</td><td align="center">5</td><td align="center">-</td></tr><tr><td align="center">07/12/2020 00:00:03</td><td align="center">3</td><td align="center">2</td><td align="center">3</td><td align="center">4</td><td align="center">12</td></tr><tr><td align="center">07/12/2020 00:00:02</td><td align="center">3</td><td align="center">4</td><td align="center">3</td><td align="center">2</td><td align="center">12</td></tr><tr><td align="center">07/12/2020 00:00:01</td><td align="center">4</td><td align="center">3</td><td align="center">5</td><td align="center">2</td><td align="center">14</td></tr></tbody></table>

#### $$fill$$\_$$missing(A + B + C + D, first$$\_$$fill="ffill",last$$\_$$fill="bfill")$$

Suppose you need to fill all gaps in the variables. In that case, set both `first_fill` and `last_fill` in the expression. The result is as follows:

<table><thead><tr><th width="224" align="center">Date</th><th width="150" align="center">A</th><th width="150" align="center">B</th><th width="150" align="center">C</th><th width="150" align="center">D</th><th align="center">Output</th></tr></thead><tbody><tr><td align="center">07/12/2020 00:00:04</td><td align="center">5</td><td align="center">2</td><td align="center">4</td><td align="center">5</td><td align="center">16</td></tr><tr><td align="center">07/12/2020 00:00:03</td><td align="center">3</td><td align="center">2</td><td align="center">3</td><td align="center">4</td><td align="center">12</td></tr><tr><td align="center">07/12/2020 00:00:02</td><td align="center">4</td><td align="center">4</td><td align="center">5</td><td align="center">2</td><td align="center">15</td></tr><tr><td align="center">07/12/2020 00:00:01</td><td align="center">4</td><td align="center">3</td><td align="center">5</td><td align="center">2</td><td align="center">14</td></tr></tbody></table>

$$fill$$\_$$missing(A + B + C + D, fill$$\_$$value=0)$$

You can also fill the gaps with a fixed value. In this case, the function sets `0` in every gap in the variables.

<table><thead><tr><th width="232" align="center">Date</th><th width="150" align="center">A</th><th width="150" align="center">B</th><th width="150" align="center">C</th><th width="150" align="center">D</th><th align="center">Output</th></tr></thead><tbody><tr><td align="center">07/12/2020 00:00:04</td><td align="center">5</td><td align="center">0</td><td align="center">4</td><td align="center">5</td><td align="center">14</td></tr><tr><td align="center">07/12/2020 00:00:03</td><td align="center">3</td><td align="center">2</td><td align="center">3</td><td align="center">4</td><td align="center">12</td></tr><tr><td align="center">07/12/2020 00:00:02</td><td align="center">0</td><td align="center">4</td><td align="center">0</td><td align="center">2</td><td align="center">6</td></tr><tr><td align="center">07/12/2020 00:00:01</td><td align="center">4</td><td align="center">3</td><td align="center">5</td><td align="center">0</td><td align="center">12</td></tr></tbody></table>

### $$cumsum()$$

|     Syntax    |          Example          | Output |
| :-----------: | :-----------------------: | :----: |
| $$cumsum(x)$$ | $$cumsum(\[0, 1, 2, 3])$$ |    6   |

### $$where()$$

<table><thead><tr><th width="257" align="center">Syntax</th><th width="353.33333333333337" align="center">Example</th><th align="center">Output</th></tr></thead><tbody><tr><td align="center"><em><strong><code>where(condition, operation if true, operation if false)</code></strong></em></td><td align="center"><p><em><strong>Step function:</strong></em></p><p><em>Assume a variable</em></p><p><span class="math">x = [-1, 2, 1]</span><span class="math">where(x >= 0, 1, 0)</span></p></td><td align="center"><span class="math">[0, 1, 1]</span></td></tr><tr><td align="center"></td><td align="center"><p><em><strong>Interval function:</strong></em></p><p><em>Assume a variable</em></p><p><span class="math">x = [-1, 2, 1]</span><span class="math">where(x &#x3C; 0, 0, where(x &#x3C; 1, 1, 2))</span></p></td><td align="center"><span class="math">[0, 2, 2]</span></td></tr></tbody></table>

### $$diff()$$

|               Syntax              |                            Example                           |    Output   |
| :-------------------------------: | :----------------------------------------------------------: | :---------: |
| ***`diff(<variable>, <steps>)`*** | *Assume a variable* $$x = \[14, 15, 17, 16]$$ $$diff(x, 2)$$ | $$\[3, 1]$$ |

### $$shift()$$

<table><thead><tr><th width="277.2330074351869" align="center">Syntax</th><th width="217.58718014524823" align="center">Example</th><th align="center">Output</th></tr></thead><tbody><tr><td align="center"><em><strong><code>shift(&#x3C;variable>,&#x3C;step>)</code></strong></em></td><td align="center"><p><em>Assume a variable</em> <span class="math">x = [-2, -1, 0, 1, 2]</span><span class="math">shift(x, -1)</span></p><p>This lets you retrieve the previous value of the variable by shifting the series to the left.</p></td><td align="center"><span class="math">[-1, 0, 1, 2]</span></td></tr><tr><td align="center"></td><td align="center"><p><em>Assume a variable</em> <span class="math">x = [-2, -1, 0, 1, 2]</span><span class="math">shift(x, 2)</span></p><p>Shifts the series to the right.</p></td><td align="center"><span class="math">[x, x, -2, -1, 0]</span></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://dev.ubidots.com/synthetic-variables/examples/special-functions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
