# How to subtract results of two flux queries within the same script

**URL:** <https://community.influxdata.com/t/how-to-subtract-results-of-two-flux-queries-within-the-same-script/32926>\
**Category:** Fluxlang\
**Created:** [January 23, 2024, 10:52pm UTC](https://community.influxdata.com/t/how-to-subtract-results-of-two-flux-queries-within-the-same-script/32926 "2024-01-23T22:52:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mufika](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mufika/32/9476_2.png) [@Mufika](https://community.influxdata.com/u/Mufika)\
**Post date:** [January 23, 2024, 10:52pm UTC](https://community.influxdata.com/t/how-to-subtract-results-of-two-flux-queries-within-the-same-script/32926/1 "2024-01-23T22:52:49Z")

</div>

How can I make the following code work? It defines a function to calculate the cost over two periods into two variables and then substract the values of the two variables to produce a result that I want to show.

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

option location = timezone.location(name: "Africa/Johannesburg")

// Define start times
startTime = date.truncate(t: now(), unit: 1mo)
startToday = date.truncate(t: now(), unit: 1d)

// Function to calculate cost
calculateCost = (start, stop) => {
  return from(bucket: "rpict3t1_1sData")
    |> range(start: start, stop: stop)
    |> filter(fn: (r) => r["_measurement"] == "rpict3t1_01")
    |> filter(fn: (r) => r["_field"] == "P1" or r["_field"] == "P2" or r["_field"] == "P3")
    |> integral(unit: 1s)
    |> map(fn: (r) => ({ _value: (r._value / 1000.0 / 3600.0 * 220.0) }))
    |> sum()
    |> map(fn: (r) => ({ r with _value: ((float(v: r._value <= 600) * r._value * 1.8536) + (float(v: r._value > 600) * ((600.0 * 1.8536) + (r._value - 600.0) * 2.9516)) + float(v: date.monthDay(t: now())) * 4.80) * 1.15 }))
}

// Function Difference between two values
substract = (x, y) => x * y 

// Calculate total cost up to the day before
totalCostBeforeToday = calculateCost(startTime, startToday)

// Calculate total cost up to the current time
totalCostCurrent = calculateCost(startTime, now())

// Calculate the additional cost for the current day
additionalCostToday = totalCostCurrent - totalCostBeforeToday

// Output the additional cost for the current day
additionalCostToday

```

When i run above code I get the following error:

invalid: compilation failed: error @29:20-29:51: expected comma in property list, got LPAREN error @29:48-29:50: unexpected token for property key: LPAREN (()

---

<div class="post-metadata">

**Author:** ![AVVS\_Sudheer](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/avvs_sudheer/32/13075_2.png) [@AVVS\_Sudheer](https://community.influxdata.com/u/AVVS_Sudheer)\
**Post date:** [January 25, 2024, 4:26am UTC](https://community.influxdata.com/t/how-to-subtract-results-of-two-flux-queries-within-the-same-script/32926/2 "2024-01-25T04:26:10Z")

</div>

Hi,

Try the code as below

> map(fn: (r) =\> ({r with \_value: (r.\_value\_t1-r.\_value\_t2)}))

where t1 and t2 are two different tags

---

<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:** [February 8, 2024, 3:45pm UTC](https://community.influxdata.com/t/how-to-subtract-results-of-two-flux-queries-within-the-same-script/32926/3 "2024-02-08T15:45:38Z")

</div>

@Mufika There were a few syntax errors in your original query. Flux doesn’t support positional parameters, only named parameters so you have to include the parameter names in your functions calls. For example:

```diff
- calculateCost(startTime, startToday)
+ calculateCost(start: startTime, stop: startToday)

```

As your query stands, your custom `calculateCost()` function returns a stream of tables. To do the operation you’re hoping to do, you need to update the function to return a scalar value. You can use `findColumn` to return an array of values in the `_value` column and then reference an element in that array.

With that in place, you can use the array package and `array.from` to build and return an ad hoc table with the computed value:

```javascript
import "array"
import "date"

// Define start times
startTime = date.truncate(t: now(), unit: 1mo)
startToday = date.truncate(t: now(), unit: 1d)

// Function to calculate cost
calculateCost = (start, stop=now()) => {
    valueArr = from(bucket: "rpict3t1_1sData")
        |> range(start: start, stop: stop)
        |> filter(fn: (r) => r["_measurement"] == "rpict3t1_01")
        |> filter(fn: (r) => r["_field"] == "P1" or r["_field"] == "P2" or r["_field"] == "P3")
        |> integral(unit: 1s)
        |> map(fn: (r) => ({ _value: (r._value / 1000.0 / 3600.0 * 220.0) }))
        |> sum()
        |> map(fn: (r) => ({ r with _value: ((float(v: r._value <= 600) * r._value * 1.8536) + (float(v: r._value > 600) * ((600.0 * 1.8536) + (r._value - 600.0) * 2.9516)) + float(v: date.monthDay(t: now())) * 4.80) * 1.15 }))
        |> findColumn(column: "_value", fn: (key) => true)
    
    return valueArr[0]
}

// Calculate total cost up to the day before
totalCostBeforeToday = calculateCost(start: startTime, stop:startToday)

// Calculate total cost up to the current time
totalCostCurrent = calculateCost(start: startTime)

// Calculate the additional cost for the current day
additionalCostToday = totalCostCurrent - totalCostBeforeToday

// Output the additional cost for the current day in a table
array.from(rows: [{_time: today(), additionalCostToday: additionalCostToday}])

```

**Note:** I made a few other updates to your script:

- Removed the `timezone` package. This only affects the behavior of `window()` and how it defines window boundaries across time changes. You don’t use `window()` in your query so there’s no need for it.
- Updated the `calculateCost()` function to include a default value for the `stop` parameter.
- Removed the `subtract()` custom function since you weren’t using it.
