Query Error - db query error: mssql: Incorrect syntax near ')'

Grafana version 9.4.3

No errors when no data is available

Correct query defining no values

I get the error
db query error: mssql: Incorrect syntax near ‘)’.
when the facility is switched off, but only on some charts, even though the data is all from the same table.

if no data is available, I would like a blank chart

this is the query for the fixed data.
SELECT TimeDate, CAST(BattVoltage AS real) as ‘AGV 227’ FROM AGV.dbo.AGVbattery

WHERE AGV = ‘227’

ORDER BY TimeDate DESC
this works fine, even when the facility is turned off, but will just display no data.

this one gives the error stated above
SELECT TimeDate as time, AGV as ‘AGV’, cast(BattVoltage AS real) as ‘Voltage’

FROM AGV.dbo.AGVbattery

WHERE $__timeFilter(TimeDate) AND Program <> 0 AND Program in ($Program) AND AGV in ($AGV)

ORDER BY TimeDate desc

The chart seems to work fine, as soon as any data is available?

Can anyone explain where I need to look to understand the cause of the concern? Thank you

What does this mean? What is this facility?