# Problem with Map syntax

**URL:** <https://community.influxdata.com/t/problem-with-map-syntax/24538>\
**Category:** Fluxlang\
**Created:** [April 5, 2022, 11:51pm UTC](https://community.influxdata.com/t/problem-with-map-syntax/24538 "2022-04-05T23:51:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick808](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@Patrick808](https://community.influxdata.com/u/Patrick808)\
**Post date:** [April 5, 2022, 11:51pm UTC](https://community.influxdata.com/t/problem-with-map-syntax/24538/1 "2022-04-05T23:51:46Z")

</div>

I’m trying to do simple math on the results of a pivot and I am getting “null” values. Substituting in fixed integer or float values it works as expected, so I assume that I am not piping the values into the function, but I can’t figure out where I went wrong.

```auto
from(bucket: "IotaHourly")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["ct"] == "AC1" or r["ct"] == "AC2" or r["ct"] == "AC3" or r["ct"] == "PNL-Kitchen" or r["ct"] == "PNL-Laundry" or r["ct"] == "TeslaWC")
  |> drop(columns: ["device", "_measurement", "_start", "_stop", "_field"])
  |> pivot(rowKey: ["_time"], columnKey: ["ct"], valueColumn: "_value")
  |> map(fn: (r) => ({r with House: r._AC1 + r.AC2 + r.AC3 + r["PNL-Kitchen"] + r["PNL-Lauandry"] - r.TeslaWC }))**  

```

Any tips (and explanation) where I went wrong? Looking at the output as a table the input numbers all look good and it is clear that the output is “null”.

Everything was taken from the [Documentation Example](https://docs.influxdata.com/influxdb/cloud/query-data/common-queries/multiple-fields-in-calculations/#full-example-query)

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [April 6, 2022, 3:30pm UTC](https://community.influxdata.com/t/problem-with-map-syntax/24538/2 "2022-04-06T15:30:05Z")

</div>

Hello @Patrick808,  
I believe you’ll have to group your data before you pivot it if you want to create calculations across those different tags.

```auto
from(bucket: "IotaHourly")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["ct"] == "AC1" or r["ct"] == "AC2" or r["ct"] == "AC3" or r["ct"] == "PNL-Kitchen" or r["ct"] == "PNL-Laundry" or r["ct"] == "TeslaWC")
  |> drop(columns: ["device", "_measurement", "_start", "_stop", "_field"])
  |> group() 
  |> pivot(rowKey: ["_time"], columnKey: ["ct"], valueColumn: "_value")
  |> map(fn: (r) => ({r with House: r._AC1 + r.AC2 + r.AC3 + r["PNL-Kitchen"] + r["PNL-Lauandry"] - r.TeslaWC }))**  

```

---

<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:** [April 6, 2022, 3:37pm UTC](https://community.influxdata.com/t/problem-with-map-syntax/24538/3 "2022-04-06T15:37:46Z")

</div>

@Patrick808 It’s because you have `null` values in your math operations. If any operands in a mathematic operation are null, the operation returns `null`.

There are two things I can think of that will cause this:

1. You have points with null values (pre-pivot)
2. Your timestamps don’t perfectly align, so as a result of the pivot, some rows don’t have values for some of the columns used in your map operation.

I suspect it’s probably #2. There are a few solutions:

1. Use `fill()` to fill each of the columns in the pivoted data. Pivoted data does make this process a little cumbersome since `fill()` can only fill one column at a time:

2. You can normalize the timestamps to a precision where points should all align. For example, if you’re seeing sub-second differences in timestamps between all the different fields, you can normalize the timestamps to the second. To do this, use [`truncateTimeColumn()`](https://docs.influxdata.com/flux/v0.x/stdlib/universe/truncatetimecolumn/) before you pivot the data:

Truth be told, you may need to do both of these. If timestamps still don’t align after normalizing/truncating the time values, you will still have to fill all the `null` columns.

---

<div class="post-metadata">

**Author:** ![Patrick808](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@Patrick808](https://community.influxdata.com/u/Patrick808)\
**Post date:** [April 6, 2022, 4:48pm UTC](https://community.influxdata.com/t/problem-with-map-syntax/24538/4 "2022-04-06T16:48:01Z")

</div>

I don’t think either explanation checks out.

This is what the table from the pivot shows:

 ![FluxPivotMap](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/5/5c682fd0e8d7f67b2342d491b6ea524e69670000.jpeg)

---

<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:** [April 6, 2022, 5:14pm UTC](https://community.influxdata.com/t/problem-with-map-syntax/24538/5 "2022-04-06T17:14:14Z")

</div>

Ah, I see the issue. You have an underscore (`_`) in front of `AC1` in your `map()` call. That column doesn’t exist in the data, so it’s returning null. Remove the underscore and you should be good to go.

---

<div class="post-metadata">

**Author:** ![Patrick808](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@Patrick808](https://community.influxdata.com/u/Patrick808)\
**Post date:** [April 6, 2022, 6:34pm UTC](https://community.influxdata.com/t/problem-with-map-syntax/24538/6 "2022-04-06T18:34:58Z")

</div>

Doh… Thanks. So much for checking seven times!

---

<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:** [April 6, 2022, 6:38pm UTC](https://community.influxdata.com/t/problem-with-map-syntax/24538/7 "2022-04-06T18:38:10Z")

</div>

No problem. Happy to help!
