How to add years to timestamp based on calculation result

Hello,

I would like to add a number of years on timestamp based on calculation.

Without calculation (fix number), I know how to do, using date.add and formula for below exemple add 1 year to ‘_time’

 |> map(
        fn: (r) => ({ r with
           Year : date.year(t:r._time),
           NB_Year : date.year(t: now()) - date.year(t:r._time),
           New_Date : date.add(d: 1y, to: r._time),
     }),
       )

Now I would like to add years based on the result calculation of NB_Year.
What is the syntax I will have to use ?
I’m sure it is basic but I do not know the syntax to use.
Thanks in advance for your help.

You are on the right path: date.year will return an integer; you need to convert it to a string then in to a duration type.

using string(v: value), then concatenate y using + “y”, then convert that string to a duration type using duration(d: value)

New_Date : date.add(d: duration(v: string(v: date.year(t: now()) - date.year(t:r._time) ) + "y"), to: r._time),,

1 Like

Thank you @fercasjr , I did not see these steps.

It works perfectly and fit exctly what I need.

Thanks a lot again :wink: