# Implementing the recursion using flux Query

As I have my data in different buckets based on the month and year.
So my data will be in the different buckets as follows:
For Jan 2023 in 012023
Dec 2022 in 122022
Nov 2022 in 112022
so if I query the data for large duration then data will be present in multiple buckets.
After getting the month and year from the start and stop time range I need to generate the bucket name dynamically and aggregate them and give the result.
The first approach will be using loops which going to be difficult to do in flux.
So I thought we can do this using Recursion.

I have written the first code below to achieve the recursion

``````
import "date"
start_month = int(v: date.month(t: v.timeRangeStart))
end_month = int(v:date.month(t: v.timeRangeStop ))

diff = end_month - start_month
diff_duration = if diff < 0 then end_month - start_month + 12 else end_month - start_month
start_year = int(v: date.year(t: v.timeRangeStart))
end_year = int(v:date.year(t: v.timeRangeStop ))

bucket_name =  string(v: start_year) + string(v:start_month)
f = (input_bucket) =>
( if diff_duration != 0 then f(input_bucket: bucket_name).unused else die(msg: "script stopped")

from(bucket: input_bucket)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "measurement")
|> filter(fn: (r) => r["tag1"] == "tag_value")
|> filter(fn: (r) => r["_field"] == "field_name")
|> aggregateWindow(every: 1d, fn: last, createEmpty: false)
|> map(fn: (r) => ({
//       r with
_value:r._value,
_field:r._field,
start_duration:v.timeRangeStart,
end_duration:v.timeRangeStop,
"bucket_name": bucket_name,
"start_month":start_month,
"end_month":end_month,
"diff_duration": diff_duration,
"diff": diff,
}))
|> yield(name: "last")
)

f( 	input_bucket: bucket_name)
``````

But this code giving the error as `unidentified unidentified f at line if diff_duration != 0 then f(input_bucket: bucket_name).unused else die(msg: "script stopped")`

I tried a different approach to achieve recursion using a different approach

``````import "date"
start_month = int(v: date.month(t: v.timeRangeStart))
end_month = int(v:date.month(t: v.timeRangeStop ))

diff = end_month - start_month
diff_duration = if diff < 0 then end_month - start_month + 12 else end_month - start_month
start_year = int(v: date.year(t: v.timeRangeStart))
end_year = int(v:date.year(t: v.timeRangeStop ))

bucket_name =  string(v: start_year) + string(v:start_month)

// g = (input_bucket) =>(
//

g = (input_bucket) =>{
unused = from(bucket: input_bucket)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "measurement")
|> filter(fn: (r) => r["tag1"] == "tag_value")
|> filter(fn: (r) => r["_field"] == "field_name")
|> aggregateWindow(every: 1d, fn: last, createEmpty: false)
|> map(fn: (r) => ({
//       r with
_value:r._value,
_field:r._field,
start_duration:v.timeRangeStart,
end_duration:v.timeRangeStop,
"bucket_name1": bucket_name,
"start_month1":start_month,
"end_month1":end_month,
"diff_duration1": diff_duration,
"diff1": diff,
}))
return {  unused: unused }

|> yield(name: "last")
}

f = (input_bucket) =>
( if diff_duration != 0 then g(input_bucket: bucket_name).unused else die(msg: "script stopped")

from(bucket: input_bucket)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "measurement")
|> filter(fn: (r) => r["tag1"] == "tag_value")
|> filter(fn: (r) => r["_field"] == "field_name")
|> aggregateWindow(every: 1d, fn: last, createEmpty: false)
|> map(fn: (r) => ({
//       r with
_value:r._value,
_field:r._field,
start_duration:v.timeRangeStart,
end_duration:v.timeRangeStop,
"bucket_name": bucket_name,
"start_month":start_month,
"end_month":end_month,
"diff_duration": diff_duration,
"diff": diff,
}))
|> yield(name: "last")
)

f(
input_bucket: bucket_name
)

//g(
//	input_bucket: bucket_name
//).unused

``````

I am getting this error

`type error @36:6-36:25: expected [A] but found {unused:[{start_month1:int, start_duration:time, end_month1:int, end_duration:time, diff_duration1:int, diff1:int, bucket_name1:string, _value:C, _field:B}]} (argument tables)`

@Anaisdg @Jay_Clifford
Is there any approach where I can implement the same?