Grafana - Absolute time range using var $__to generates invalid query

I’m trying to make a query with a fixed time range, based on the time selection.
As an example I want the last 3 days of data, starting from the maximum selected time.

Grafana provides something to address this out of the box, the relative-time, but sadly it works only as long as the time selection is relative (ie: last 5m|3h|2d), when the selection becomes absolute (from/to have a specific date-time) it does not work and fetch the data based on the actual selection.

Therefore I decided to make it manually since there are two variables that contain the time selection __from and __to, what I did is use $__to to define lower and upper date-time limit, the problem is that the query sent to influx is not valid,
I’ve tried the same in Chronograf and it works properly.

Those are my queries

#Chronograf Query - Working
SELECT
     mean("value") AS "Avg Size"
FROM "monitor"."short"."sqlserver_performance"
WHERE
     "sql_instance" =~ /^GSVSQL25$/
     AND "counter" = 'Data File(s) Size (KB)'
     AND "instance" !~ /^(master|model|mssqlsystemresource|Total)/
     AND "time" > (:upperDashboardTime: - 3d)
     AND "time" <= :upperDashboardTime:
GROUP BY
     time(1d)
     ,"instance"


#Grafana Query - Not Working
SELECT
     mean("value") AS "Avg Size"
FROM "sqlserver_performance"
WHERE
     "sql_instance" =~ /^$Var_Sql_Instance$/
     AND "counter" = 'Data File(s) Size (KB)'
     AND "instance" !~ /^(master|model|mssqlsystemresource|Total)/
     AND "time" > ($__to - 3d)
     AND "time" <= $__to
GROUP BY
     time(1d)
     ,"instance"

# What Grafana sends to InfluxDB
SELECT 
     mean("value") AS "Avg Size" 
FROM "sqlserver_performance" 
WHERE 
     "sql_instance" =~ /^GSVSQL25$/ 
     AND "counter" = 'Data File(s) Size (KB)' 
     AND "instance" !~ /^(master|model|mssqlsystemresource|Total)/ 
     AND "time" > (1584140399000 - 3d) 
     AND "time" <= 1584140399000 
GROUP BY 
     time(1d)
     ,"instance"

After some trial and error, I’ve discovered that this is the section that makes the query invalid

AND “time” > (1584140399000 - 3d)
AND “time” <= 1584140399000

Does anybody have suggestions about how to solve this kind of problem?