How to create dynamic scripted field based on date-time range selection

I want to create dynamic scripted field (Avg(NA-NB)), which is the average of NA-NB field values based on the date-time range selection.

Currently the average is calculated on the total rows of the NA-NB. What I want is that, if I select the date-time from "2025-01-09 07:30:00" to "2025-01-09 07:30:08", so the average should be calculated on the total of first 9 rows, not all the rows. OR
can we calculate average in Kibana lens or TSVB?

I assume for each @timestamp bucket there's probably an average to compute (here is every 3 hours). Next to it the overall_average(...) formula is used to compute across the entire table data: this will dynamically change based on the date -time range.