Flux Map with whitespace

Hello! I’m having to work on some fields that contain white spaces. When I ran into this issue before, I just dropped the measurement, replaced all of the whitespaces with underscores and started over. I really don’t want to do that again and am hoping to find a solution. I’ve tried escaping the whitespace with a backslash like below with no luck.

from(bucket: "telegraf/autogen")
  |> range(start: dashboardTime)
  |> filter(fn: (r) => r._measurement == "PM03" and r._field == "Primary system power")
  |> map(fn: (r) => ({r with _value: (r.Primary\ system\ power * .001732)}))

Hi there,
Interestingly enough I had a similar issue when collecting data from my Jetson device. Though it does not specifically answer your question I believe we can bypass the issue altogether.
Here is an example:

from(bucket: "Jetson")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "exec_jetson")
  |> filter(fn: (r) => r["_field"] == "jetson_Temp AO")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")
  |> map(fn: (r) => ({ r with tempMod : r._value * .001732}))

In this case when using the map function we specify the new column name (in my case tempMod). Secondly we specify the r._value column as this is where the value we want to modify is stored.

This will the provide the following result:

I hope this helps. Let me know if you need any further support.

Thanks,
Jay

2 Likes

Thank you, Jay!
This definitely answers my question for a single field.
I do have some scenarios where there are multiple fields with whitespace I need to combine. Do you have any ideas on this? Below is an example:

  |> map(fn: (r) => ({ r with _value: r.USI1 UPS225 1 POWER + r.USI1 UPS225 2 POWER + r.USI1 UPS250 1 POWER PHA + r.USI1 UPS250 1 POWER PHB + r.USI1 UPS250 1 POWER PHC}))

This is actually one where I went in and dropped the entire measurement record, replaced all of the whitespace with underscores and restarted. It was painful, but I’m slowly grinding my way up the learning curve!

No worries at all. Taking my Jetson example again here is a Flux query using two different data points (GPU and CPU temperature):

raw = from(bucket: "Jetson")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "exec_jetson")
  |> filter(fn: (r) => r["_field"] == "jetson_Temp CPU" or r["_field"] == "jetson_Temp GPU")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

  raw
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with transformed: r["jetson_Temp CPU"] * r["jetson_Temp CPU"] }))

As you can see we do the following in this example:

  1. We first filter by the _field names we would like to use in our calculation. Note: You do not need to store your result in a variable like I have. I have purely done this to highlight the part of interest (pivot and map)

  2. Next we can perform a pivot on the table which will transform our _field names into columns.

  3. Lastly we perform our map function. After a little digging I found out you can define your recored notation like so r["column name"] , which means white spaces within your column names should no longer be an issue.

The resulting table looks like this:

I hope this helps. Let me know if I can be of further assistance.

Thanks,
Jay

1 Like

Awesome, this is exactly what I needed.
Thanks again for showing me this, Jay. It’s going to make this much easier!

1 Like

No worries at all! Happy to help anytime! :slight_smile: