# Creating a new tag from queries and calculation

**URL:** <https://community.influxdata.com/t/creating-a-new-tag-from-queries-and-calculation/21426>\
**Category:** Fluxlang\
**Tags:** flux\
**Created:** [August 27, 2021, 1:35pm UTC](https://community.influxdata.com/t/creating-a-new-tag-from-queries-and-calculation/21426 "2021-08-27T13:35:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bmenard](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@bmenard](https://community.influxdata.com/u/bmenard)\
**Post date:** [August 27, 2021, 1:35pm UTC](https://community.influxdata.com/t/creating-a-new-tag-from-queries-and-calculation/21426/1 "2021-08-27T13:35:37Z")

</div>

Here’s an example so far of what i want to achieve:

//Return Water Temp  
RWT = from(bucket: “Bacnet\_Network”)  
|\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
|\> filter(fn: (r) =\> r[“\_measurement”] == “048\_STEM”)  
|\> filter(fn: (r) =\> r[“sensor”] == “470500\_AI\_1103\_STEM\_LVL0\_CWRT”)  
|\> filter(fn: (r) =\> r[“\_field”] == “value”)  
|\> aggregateWindow(every: 1h, fn: mean, createEmpty: false)  
|\> yield(name: “RWT”)

//Supply Water Temp  
SWT = from(bucket: “Bacnet\_Network”)  
|\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
|\> filter(fn: (r) =\> r[“\_measurement”] == “048\_STEM”)  
|\> filter(fn: (r) =\> r[“system”] == “470000\_STEM\_ROUTER\_PANEL”)  
|\> filter(fn: (r) =\> r[“unit”] == “°C”)  
|\> filter(fn: (r) =\> r[“sensor”] == “470000\_AV\_75\_STEM\_PWP\_CHIL\_SWT\_XFER\_AV”)  
|\> filter(fn: (r) =\> r[“\_field”] == “value”)  
|\> aggregateWindow(every: 1h, fn: mean, createEmpty: false)  
|\> yield(name: “SWT”)

//Chilled Flow  
Flow = from(bucket: “Bacnet\_Network”)  
|\> range(start: v.timeRangeStart, stop: v.timeRangeStop)  
|\> filter(fn: (r) =\> r[“\_measurement”] == “048\_STEM”)  
|\> filter(fn: (r) =\> r[“system”] == “470500\_STEM\_HYD\_BSMT”)  
|\> filter(fn: (r) =\> r[“unit”] == “°C” or r[“unit”] == “g/min”)  
|\> filter(fn: (r) =\> r[“sensor”] == “470500\_AI\_1104\_STEM\_CWR\_FM”)  
|\> filter(fn: (r) =\> r[“\_field”] == “value”)  
|\> aggregateWindow(every:1h, fn: mean, createEmpty: false)  
|\> yield(name: “Flow”)

//Need to create new tag and timestamp in database with this calculation  
//New Value = (RWT - SWT) \* 1.8 \* Flow \* 500.0 / 1000.0

I look at this solution but i can’t make it work:  
[Create new column to store calculation of two fields from different buckets](https://community.influxdata.com/t/create-new-column-to-store-calculation-of-two-fields-from-different-buckets/20446).

Any help would be appreciated thx!

---

<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:** [August 30, 2021, 3:21pm UTC](https://community.influxdata.com/t/creating-a-new-tag-from-queries-and-calculation/21426/2 "2021-08-30T15:21:41Z")

</div>

Hello @bmenard,  
I would recommend doing the following:

```auto
from(bucket: “Bacnet_Network”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == “048_STEM”)
|> filter(fn: (r) => r[“system”] == “470500_STEM_HYD_BSMT”)
|> filter(fn: (r) => r[“sensor”] == “470500_AI_1104_STEM_CWR_FM” or r[“sensor”] == “470000_AV_75_STEM_PWP_CHIL_SWT_XFER_AV” or r[“sensor”] ==“470500_AI_1103_STEM_LVL0_CWRT”)
|> filter(fn: (r) => r["_field"] == “value”)
|> aggregateWindow(every:1h, fn: mean, createEmpty: false)
|> pivot(rowKey:["_time"], columnKey: ["sensor"], valueColumn: "_value")
|> map(fn: (r) => ({ r with _value: (r["“470500_AI_1103_STEM_LVL0_CWRT"] - r["“470000_AV_75_STEM_PWP_CHIL_SWT_XFER_AV”]) * 1.8 * r[“470500_AI_1104_STEM_CWR_FM”] *500.0/1000.0}))
|> to(bucket:"new bucket")
|> yield(name: “Flow”)

```

Are hoping to write that value back to the same database?  
If so I’d also use the keep() function to just keep the new calculated value, measurement, and time column.

I made some assumptions about your data so it’s possible that this isn’t a perfect fit. Let me know if you run into any problems.

---

<div class="post-metadata">

**Author:** ![bmenard](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@bmenard](https://community.influxdata.com/u/bmenard)\
**Post date:** [August 31, 2021, 11:05am UTC](https://community.influxdata.com/t/creating-a-new-tag-from-queries-and-calculation/21426/3 "2021-08-31T11:05:03Z")

</div>

Sadly, it returned a Null value. I post the before pivot and after with new calculated value csv file if it can help you saving me.

Thx for helping me.

Before Pivot:

 ![Before Pivot](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/1/1f4b98ce689cb001a1b78c25feaae741494cfc5f.jpeg)

Result:

 ![New calculated value](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/b/b8dc6a272587f5d93df7a9074a2ab59021c854e0.jpeg)

---

<div class="post-metadata">

**Author:** ![simon38](https://avatars.discourse-cdn.com/v4/letter/s/aca169/32.png) [@simon38](https://community.influxdata.com/u/simon38)\
**Post date:** [September 9, 2021, 3:29pm UTC](https://community.influxdata.com/t/creating-a-new-tag-from-queries-and-calculation/21426/4 "2021-09-09T15:29:30Z")

</div>

I think the problem is that “system” is part of the group key, and therefore the results are grouped by “system” and “unit”. I think you need to change the group key by using the group function before pivoting, maybe simply calling group() to eliminate all keys.

---

<div class="post-metadata">

**Author:** ![bmenard](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@bmenard](https://community.influxdata.com/u/bmenard)\
**Post date:** [September 14, 2021, 2:24pm UTC](https://community.influxdata.com/t/creating-a-new-tag-from-queries-and-calculation/21426/5 "2021-09-14T14:24:53Z")

</div>

@Anaisdg @simon38 Thx to both of you it solved my problem!

---

<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:** [September 14, 2021, 3:05pm UTC](https://community.influxdata.com/t/creating-a-new-tag-from-queries-and-calculation/21426/6 "2021-09-14T15:05:42Z")

</div>

Of course @bmenard! Thank you for sharing your solution. 🙂
