Pie chart shows value in last snapshot instead of last day of date range

Hi i have the following psql query

SELECT
DATE_TRUNC(‘${time_variable}’, account.created_shot) AS time,
‘Wired’ as metric,
COUNT(*) AS value
FROM
account
LEFT JOIN
customer c ON c.id_account = account.id
RIGHT JOIN
property ON property.id_customer = c.id
RIGHT JOIN
service ON service.id_property = property.id
WHERE
service.category = ‘test’
AND account.company = ‘testcompany’
AND property.iso_country IN ($country)
AND service.name IN ($pacchettotest)
AND service.name NOT IN (‘test1’,‘test2’,‘test3’,‘test4’)
AND (
‘${time_variable}’ NOT IN (‘month’, ‘week’, ‘year’)
OR (
‘${time_variable}’ = ‘month’ AND DATE_PART(‘day’, account.created_shot) = 1
)
OR (
‘${time_variable}’ = ‘week’ AND DATE_PART(‘dow’, account.created_shot) = 1
)
OR (
‘${time_variable}’ = ‘year’ AND DATE_PART(‘doy’, account.created_shot) = 1
)
)
GROUP BY
DATE_TRUNC(‘${time_variable}’, account.created_shot)
ORDER BY
time;

The query works properly but when i view it as a pie chart instead of showing the count in the last day of the date range selected it shows the count in the last available snapshot (in the time series it works properly only showing the line in the date range selected)

This is to show date range used

This is to show value on last snapshot

In the value option i have selected Last* and Numeric Field

Let me know if there is a solution. Have a nice day

Your query is independent from the time range selected in Grafana, right? Because you are not using ${__from} and ${__to} variables in it.

What is your expected result? What should be shown instead of 20261 for the sample data you provided?