---
title: filter() function
description: filter() filters data based on conditions defined in a predicate function (fn).
url: https://docs.influxdata.com/flux/v0/stdlib/universe/filter/
estimated_tokens: 2700
product: Flux
version: v0
---

# filter() function

-   Flux 0.7.0+
-   View InfluxDB support

`filter()` filters data based on conditions defined in a predicate function (`fn`).

Output tables have the same schema as the corresponding input tables.

##### Function type signature

```js
(<-tables: stream[A], fn: (r: A) => bool, ?onEmpty: string) => stream[A] where A: Record
```

For more information, see [Function type signatures](/flux/v0/function-type-signatures/).

## Parameters

### fn

(Required) Single argument predicate function that evaluates `true` or `false`.

Records representing each row are passed to the function as `r`. Records that evaluate to `true` are included in output tables. Records that evaluate to *null* or `false` are excluded from output tables.

### onEmpty

Action to take with empty tables. Default is `drop`.

**Supported values**:

-   **keep**: Keep empty tables.
-   **drop**: Drop empty tables.

### tables

Input data. Default is piped-forward data (`<-`).

## Examples

-   [Filter based on InfluxDB measurement, field, and tag](#filter-based-on-influxdb-measurement-field-and-tag)
-   [Keep empty tables when filtering](#keep-empty-tables-when-filtering)
-   [Filter values based on thresholds](#filter-values-based-on-thresholds)

### Filter based on InfluxDB measurement, field, and tag

```js
from(bucket: "example-bucket")
    |> range(start: -1h)
    |> filter(
        fn: (r) => r._measurement == "cpu" and r._field == "usage_system" and r.cpu == "cpu-total",
    )
```

### Keep empty tables when filtering

```js
import "sampledata"
import "experimental/table"

sampledata.int()
    |> filter(fn: (r) => r._value > 18, onEmpty: "keep")
```

[](#view-example-input-and-output)

View example input and output

#### Input data

| _time | _value | *tag |
| --- | --- | --- |
| 2021-01-01T00:00:00Z | -2 | t1 |
| 2021-01-01T00:00:10Z | 10 | t1 |
| 2021-01-01T00:00:20Z | 7 | t1 |
| 2021-01-01T00:00:30Z | 17 | t1 |
| 2021-01-01T00:00:40Z | 15 | t1 |
| 2021-01-01T00:00:50Z | 4 | t1 |

| _time | _value | *tag |
| --- | --- | --- |
| 2021-01-01T00:00:00Z | 19 | t2 |
| 2021-01-01T00:00:10Z | 4 | t2 |
| 2021-01-01T00:00:20Z | -3 | t2 |
| 2021-01-01T00:00:30Z | 19 | t2 |
| 2021-01-01T00:00:40Z | 13 | t2 |
| 2021-01-01T00:00:50Z | 1 | t2 |

| _time | _value | *tag |
| --- | --- | --- |

#### Output data

| _time | _value | *tag |
| --- | --- | --- |
| 2021-01-01T00:00:00Z | 19 | t2 |
| 2021-01-01T00:00:30Z | 19 | t2 |

### Filter values based on thresholds

```js
import "sampledata"

sampledata.int()
    |> filter(fn: (r) => r._value > 0 and r._value < 10)
```

[](#view-example-input-and-output)

View example input and output

#### Input data

| _time | _value | *tag |
| --- | --- | --- |
| 2021-01-01T00:00:00Z | -2 | t1 |
| 2021-01-01T00:00:10Z | 10 | t1 |
| 2021-01-01T00:00:20Z | 7 | t1 |
| 2021-01-01T00:00:30Z | 17 | t1 |
| 2021-01-01T00:00:40Z | 15 | t1 |
| 2021-01-01T00:00:50Z | 4 | t1 |

| _time | _value | *tag |
| --- | --- | --- |
| 2021-01-01T00:00:00Z | 19 | t2 |
| 2021-01-01T00:00:10Z | 4 | t2 |
| 2021-01-01T00:00:20Z | -3 | t2 |
| 2021-01-01T00:00:30Z | 19 | t2 |
| 2021-01-01T00:00:40Z | 13 | t2 |
| 2021-01-01T00:00:50Z | 1 | t2 |

#### Output data

| _time | _value | *tag |
| --- | --- | --- |
| 2021-01-01T00:00:20Z | 7 | t1 |
| 2021-01-01T00:00:50Z | 4 | t1 |

| _time | _value | *tag |
| --- | --- | --- |
| 2021-01-01T00:00:10Z | 4 | t2 |
| 2021-01-01T00:00:50Z | 1 | t2 |

#### Related

-   [Query fields and tags](/influxdb/v2/query-data/flux/query-fields/)
-   [Query using conditional logic](/influxdb/v2/query-data/flux/conditional-logic/)
-   [Check if a value exists](/influxdb/v2/query-data/flux/exists/)
-   [InfluxQL – SELECT](/influxdb/v1/query_language/explore-data/#the-basic-select-statement)

[transformations](/flux/v0/tags/transformations/) [filters](/flux/v0/tags/filters/)
