# How to get Latest Timestamp of Tag

**URL:** <https://forum.inductiveautomation.com/t/how-to-get-latest-timestamp-of-tag/90393>\
**Category:** Ignition\
**Created:** [May 14, 2024, 10:45pm UTC](https://forum.inductiveautomation.com/t/how-to-get-latest-timestamp-of-tag/90393 "2024-05-14T22:45:32Z")\
**Posts on this page:** 6\
**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:** [May 14, 2024, 10:45pm UTC](https://forum.inductiveautomation.com/t/how-to-get-latest-timestamp-of-tag/90393/1 "2024-05-14T22:45:32Z")

</div>

Hi,

Is there a "direct" way to get the timestamp of the latest row return by queryTagHistory?  
The not so direct way is to query with some arbitrary range.

Background:

I have a Discrete Tag.  
This Tag has timestamp property, but I do not want to get this timestamp.  
When this tag quality changes it will update this timestamp property of Tag.

What I do want is, the timestamp of the last change which is stored in the (latest row) historian table.

---

<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:** [May 14, 2024, 11:05pm UTC](https://forum.inductiveautomation.com/t/how-to-get-latest-timestamp-of-tag/90393/2 "2024-05-14T23:05:53Z")

</div>

I think you're bumping into another limitation of the Historian.

Generally when configuring tag history you'll set the Max Time Between Samples so that you don't have extended periods of time in tag history with no record of the tag. This might be once every day for monthly pruning, etc.

The problem now is that the latest value isn't the last change. It may never change but there will be a reading every time Max Time elapses.

It may be time to do your own logging or create a DateTime memory tag to be updated every time the original tag changes state.

---

<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:** [May 14, 2024, 11:14pm UTC](https://forum.inductiveautomation.com/t/how-to-get-latest-timestamp-of-tag/90393/3 "2024-05-14T23:14:18Z")

</div>

In this case, I am good, because the nature of this data is discrete. So the historian will log the value every time it change.

Just thinking if there is a built in function I can use, like "select top 1". - i guess this is the solution, to write a sql select query on the historian.

But you are right, a robust option is to create a logging mechanism myself.

---

<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:** [May 14, 2024, 11:18pm UTC](https://forum.inductiveautomation.com/t/how-to-get-latest-timestamp-of-tag/90393/4 "2024-05-14T23:18:13Z")

</div>

> [@Eugene49](#):
>
> ... to write a SQL select query on the historian.

If your Historian configuration uses pruning this gets messy very quickly as the data is split across monthly tables.

---

<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:** [May 14, 2024, 11:33pm UTC](https://forum.inductiveautomation.com/t/how-to-get-latest-timestamp-of-tag/90393/5 "2024-05-14T23:33:08Z")

</div>

you are right, I need to know the table name, as it changed from time to time.

🤔  
If I log myself, I need to setup a pruning logic for my table.. which is above my experience.

I guess back to using `system.tag.queryTagHistory`

---

<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:** [May 15, 2024, 6:51am UTC](https://forum.inductiveautomation.com/t/how-to-get-latest-timestamp-of-tag/90393/6 "2024-05-15T06:51:58Z")

</div>

The pruning logic could be as simple as a gateway scheduled task to run a query once a day (or whatever) to delete all records older than one year (or whatever). And you have a year to figure it out!
