# Limit() with multiple optional fields (limit limits each field separately)

**URL:** https://community.influxdata.com/t/limit-with-multiple-optional-fields-limit-limits-each-field-separately/15922
**Category:** Fluxlang
**Tags:** influxdb, flux
**Created:** [September 10, 2020, 9:43am UTC](https://community.influxdata.com/t/limit-with-multiple-optional-fields-limit-limits-each-field-separately/15922 "2020-09-10T09:43:36Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![databender](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@databender](https://community.influxdata.com/u/databender)
#### Post date: [September 10, 2020, 9:43am UTC](https://community.influxdata.com/t/limit-with-multiple-optional-fields-limit-limits-each-field-separately/15922/1 "2020-09-10T09:43:36Z")

</div>

Hello,

I have a multivariate time series with a flexible field set:

- 1991-01-01T00:00:00Z: a=1
- 1992-01-01T00:00:00Z: a=2, b=2.5
- 1993-01-01T00:00:00Z: a=3, b=3.5

As you can see, variable b only gets measured from 1992 onwards.

Now, when I want to paginate through all my records with a limit of 2 with Flux, I do:

```
from(bucket: "mybucket")
    |> range(start: 1970-01-01T00:00:00Z)
    |> filter(fn: (r) => r._measurement == "mymeasurement")
    |> limit(n: 2)
    |> yield()

```

**Expected:**  
I would expect to get the records for 1991 and 1992.

- 1991-01-01T00:00:00Z: a=1
- 1992-01-01T00:00:00Z: a=2, b=2.5

**Actual:**  
However, it seems that Influx limits each field separately. Because these are the FluxTables’ records I get:

```
{result=_result, table=0, _start=1970-01-01T00:00:00Z, _stop=2020-09-10T09:40:34.496818215Z, _time=1991-01-01T00:00:00Z, _value=1, _field=a, _measurement=mymeasurement}
{result=_result, table=0, _start=1970-01-01T00:00:00Z, _stop=2020-09-10T09:40:34.496818215Z, _time=1992-01-01T00:00:00Z, _value=2, _field=a, _measurement=mymeasurement}
{result=_result, table=1, _start=1970-01-01T00:00:00Z, _stop=2020-09-10T09:40:34.496818215Z, _time=1992-01-01T00:00:00Z, _value=2.5, _field=b, _measurement=mymeasurement}
{result=_result, table=1, _start=1970-01-01T00:00:00Z, _stop=2020-09-10T09:40:34.496818215Z, _time=1993-01-01T00:00:00Z, _value=3.5, _field=b, _measurement=mymeasurement}

```

Which essentially is:

- 1991-01-01T00:00:00Z: a=1
- 1992-01-01T00:00:00Z: a=2, b=2.5
- 1993-01-01T00:00:00Z: b=3.5

Is there any way that I can only get all available fields for the first 2 timestamps?

---

<div class="post-metadata">

### Author: ![mhall119](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mhall119/32/3898_2.png) [@mhall119](https://community.influxdata.com/u/mhall119)
#### Post date: [September 11, 2020, 2:50pm UTC](https://community.influxdata.com/t/limit-with-multiple-optional-fields-limit-limits-each-field-separately/15922/2 "2020-09-11T14:50:00Z")

</div>

The default way that Flux processes data from InfluxDB is one field at a time. To do what you want, use the [fieldsAsCols()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/influxdb-v1/fieldsascols/) function to transform the data so that all of the fields for a measurement are in the same row.

```auto
import "influxdata/influxdb/v1"

from(bucket: "mybucket")
    |> range(start: 1970-01-01T00:00:00Z)
    |> filter(fn: (r) => r._measurement == "mymeasurement")
    |> v1.fieldsAsCols()
    |> limit(n: 2)
    |> yield()

```

Should do what you want

---

<div class="post-metadata">

### Author: ![databender](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@databender](https://community.influxdata.com/u/databender)
#### Post date: [September 21, 2020, 9:45am UTC](https://community.influxdata.com/t/limit-with-multiple-optional-fields-limit-limits-each-field-separately/15922/3 "2020-09-21T09:45:12Z")

</div>

Thank you! That does exactly what I want and is also much faster and simpler than the one-field-at-time approach.

I was sceptical whether I should use a “v1” function as it sounds like that would be deprecated and removed pretty soon.  
But `v1.fieldsAsCols()` seems to be built on the regular `pivot()` function so it should be here to stay, right?
