# Tag Query History: get the last record before start datetime

**URL:** <https://forum.inductiveautomation.com/t/tag-query-history-get-the-last-record-before-start-datetime/83292>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [December 19, 2023, 1:41am UTC](https://forum.inductiveautomation.com/t/tag-query-history-get-the-last-record-before-start-datetime/83292 "2023-12-19T01:41:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [December 19, 2023, 1:41am UTC](https://forum.inductiveautomation.com/t/tag-query-history-get-the-last-record-before-start-datetime/83292/1 "2023-12-19T01:41:01Z")

</div>

given startDatetime = 1:00 pm and endDatetime = 5:00 pm, I am querying Machine State using `system.tag.queryTagHistory`  
Data returned like so:  
value='Run' at timestamp 1:30pm  
value='Stop' at timestamp 1:50pm  
etc.

How can I get the last row(record) before 1:30pm?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 19, 2023, 3:31am UTC](https://forum.inductiveautomation.com/t/tag-query-history-get-the-last-record-before-start-datetime/83292/2 "2023-12-19T03:31:06Z")

</div>

You can't, really. I was very surprised at the lack of interpolation / extrapolation offered in tag history queries to the point of finding it useless for many applications. You can set maximum time between samples in tag history properties but can't specify when the occur.

Take control! Create your own data table, create a gateway scheduled task to record tag value on the hour or whatever suits.

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [December 19, 2023, 3:49am UTC](https://forum.inductiveautomation.com/t/tag-query-history-get-the-last-record-before-start-datetime/83292/3 "2023-12-19T03:49:34Z")

</div>

Thanks.  
I was querying for last 24 hours machine state.  
I move my startDatetime 7 days back to the past to capture more records and get the last record before the initial startDatetime.

But really, what if there was no entry for the past 7 days.. Oh well, I can just put Unknown state when this happens.

---

<div class="post-metadata">

**Author:** ![Hayden\_Watson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/hayden_watson/32/48792_2.png) [@Hayden\_Watson](https://forum.inductiveautomation.com/u/Hayden_Watson)\
**Post date:** [December 19, 2023, 7:48am UTC](https://forum.inductiveautomation.com/t/tag-query-history-get-the-last-record-before-start-datetime/83292/4 "2023-12-19T07:48:47Z")

</div>

Maybe I'm wrong.

But would `system.tag.queryTagHistory` with a return size of 1 and an aggregate mode of `LastValue` not return the last value in a time range. Then set end date to whatever time you want?

---

<div class="post-metadata">

**Author:** ![Eugene49](https://avatars.discourse-cdn.com/v4/letter/e/c6cbf5/32.png) [@Eugene49](https://forum.inductiveautomation.com/u/Eugene49)\
**Post date:** [December 19, 2023, 4:53pm UTC](https://forum.inductiveautomation.com/t/tag-query-history-get-the-last-record-before-start-datetime/83292/5 "2023-12-19T16:53:00Z")

</div>

I am still not very familiar with tag historian querying.  
In my case the data are discrete, which is a string such as "Run", "Stop", "Standby".

And from querying it looks like it logs one row per state change.

I tried to set `startDatetime` and `endDatetime` to a time where it is between records and query returns nothing.
