# How to unpivot influx data so that to(bucket: ...) doesn't complain?

**URL:** <https://community.influxdata.com/t/how-to-unpivot-influx-data-so-that-to-bucket-doesnt-complain/32402>\
**Category:** Tasks\
**Created:** [December 5, 2023, 12:28pm UTC](https://community.influxdata.com/t/how-to-unpivot-influx-data-so-that-to-bucket-doesnt-complain/32402 "2023-12-05T12:28:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![itsmaxdd](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/itsmaxdd/32/11023_2.png) [@itsmaxdd](https://community.influxdata.com/u/itsmaxdd)\
**Post date:** [December 5, 2023, 12:28pm UTC](https://community.influxdata.com/t/how-to-unpivot-influx-data-so-that-to-bucket-doesnt-complain/32402/1 "2023-12-05T12:28:18Z")

</div>

Hello,  
I have a task extracting some data which is run periodically due to its size.  
Basically i’m creating a discharge and a charge power/energy over a period of time out of voltage and current (and its sign) which i would like to then push into a bucket so that grafana can just query that simple bucket  
Of course once data is pivoted, the “to” instruction complaints about the fact that it misses a lot of columns such as “\_time”, “\_field”, “\_measurements”.  
Ideally i should have a “\_field” column under which i differentiate between Discharge\_Power and Charge\_Power whose value is stored in “\_value”.  
Unfortuately i’m failing to achieved that and for whatever reason even if “import experimental” and use it as “experimental.unpivot()” it complains about it not being valid apparently.

```auto
import "date"
import "timezone"

option location = timezone.location(name: "Europe/Rome")
option task = {name: "Monthly Battery Power", cron: "0 5 * * *"}

array = [
    "Jan",
    "Feb",
    "Mar",
    "Apr",
    "May",
    "Jun",
    "Jul",
    "Aug",
    "Sep",
    "Oct",
    "Nov",
    "Dec",
]

A =
    from(bucket: "PVdb")
        |> range(start: -15d)
        |> filter(fn: (r) => r._measurement == "bms" and r._field == "I" or r._field == "V")
        |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

B =
    A
        |> map(fn: (r) => ({r with _value: if r.I >= 0 and r.I <= 100 then r.V * r.I else 0.0}))
        |> window(every: 1mo)
        |> integral(unit: 1h)
        |> map(fn: (r) => ({r with _name: array[date.month(t: r._start) - 1]}))
        |> group()
        |> drop(columns: ["_field", "_measurement"])

C =
    A
        |> map(fn: (r) => ({r with _value: if r.I <= 0 and r.I >= -50 then (-r.V) * r.I else 0.0}))
        |> window(every: 1mo)
        |> integral(unit: 1h)
        |> map(fn: (r) => ({r with _name: array[date.month(t: r._start) - 1]}))
        |> group()
        |> drop(columns: ["_field", "_measurement"])

join(tables: {Charge: B, Discharge: C}, on: ["_start", "_name", "_stop"], method: "inner")
    |> map(fn: (r) => ({r with _time: r._start}))
    |> to(bucket: .......)

```

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [December 5, 2023, 4:29pm UTC](https://community.influxdata.com/t/how-to-unpivot-influx-data-so-that-to-bucket-doesnt-complain/32402/2 "2023-12-05T16:29:32Z")

</div>

@itsmaxdd What version of InfluxDB are you using?

---

<div class="post-metadata">

**Author:** ![itsmaxdd](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/itsmaxdd/32/11023_2.png) [@itsmaxdd](https://community.influxdata.com/u/itsmaxdd)\
**Post date:** [December 5, 2023, 4:44pm UTC](https://community.influxdata.com/t/how-to-unpivot-influx-data-so-that-to-bucket-doesnt-complain/32402/3 "2023-12-05T16:44:12Z")

</div>

InfluxDB v2.7.4  
Server: 19e5c0e  
Frontend: 9d25a2f

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [December 5, 2023, 4:59pm UTC](https://community.influxdata.com/t/how-to-unpivot-influx-data-so-that-to-bucket-doesnt-complain/32402/4 "2023-12-05T16:59:56Z")

</div>

What specific error are you getting? It should support `experimental.unpivot()`. Another option is using [`influxdb.wideTo()`](https://docs.influxdata.com/flux/v0/stdlib/influxdata/influxdb/wideto/) to write the unpivoted data to InfluxDB.

---

<div class="post-metadata">

**Author:** ![itsmaxdd](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/itsmaxdd/32/11023_2.png) [@itsmaxdd](https://community.influxdata.com/u/itsmaxdd)\
**Post date:** [December 10, 2023, 3:10pm UTC](https://community.influxdata.com/t/how-to-unpivot-influx-data-so-that-to-bucket-doesnt-complain/32402/5 "2023-12-10T15:10:05Z")

</div>

Solved by recreating the db, not sure what happened
