How-to
Get the timestamp of a max value in a historian report query
Short answer
A tag history aggregation returns the maximum value alone and drops the row that set it, so there is no timestamp to bind. Add a Script data source below the Tag Historian Query, scan the returned rows with a strict greater-than comparison that carries each row's t_stamp, and bind the report to that pair; or query the history partitions directly with the tie rule written into the SQL.
On this page
A tag history aggregation returns the maximum as one number and drops the row that produced it, so the report has no timestamp to bind next to it. Get that timestamp by keeping the Tag Historian Query that returns the day's rows and adding a Script data source below it: the script scans the returned rows, keeps the t_stamp of the row that sets the maximum, and hands the report a value-plus-timestamp pair it can bind. When the day has too many samples to pull into the report, query the historian's storage tables directly instead, across every partition the reporting interval spans, with the tie rule written into the query. No aggregation setting returns the winning row's timestamp; the row has to be recovered by one of those two paths.
Why the calculation query has no timestamp to bind #
The Tag Historian Query's max aggregation collapses every sample in the range to a single value. The preview XML still shows t_stamp and value because those are the query's rows, and seeing t_stamp in the Key Browser under the datasource confirms the column exists — it does not mean the aggregation result carries the winning row. The aggregation output is a scalar; the timestamps live only on the rows.
That distinction decides where to fix the report. If t_stamp is missing from the datasource's available columns, the query needs attention first. If the column is there but the report shows a timestamp that does not belong to the maximum, the binding has paired the aggregate with an unrelated row — placing the calculated maximum beside the t_stamp of whatever row the report happens to iterate is the classic failure. Recover the row that set the maximum; do not rearrange fields.
Choose the path #
| Path | Where it runs | Use it when |
|---|---|---|
| Script data source scan | Directly below the Tag Historian Query in the datasource chain | The query already returns the day's samples at a row count the report can carry |
| Direct history SQL | Against the historian's storage tables | The day has too many samples to pull in, or several tags need a maximum with its timestamp in one query |
The scan reuses rows the report fetches anyway and leaves nothing tied to one storage schema. The direct query returns value and timestamp in a single result set but owns partition coverage and the tie rule itself.
Wire the Script data source under the query #
- Keep the Tag Historian Query as the top datasource and set its range to the reporting day. Configure it to return the sample rows you intend to scan, not a pre-aggregated curve — the scan is only as complete as the rows this query returns.
- Run the query preview and confirm every row carries both
t_stampandvaluebefore writing any script. - Add a Script data source below the query in the datasource chain. The reporting manual documents it as a way to add or replace report data with a Python script, and includes an example of looping through a previous datasource; have the script read the upstream query by name, not the preview XML, which is a design-time view rather than the report's data.
- In the script, take the candidate maximum and its
t_stampfrom the first valid row, then walk the remaining rows and replace the pair only when a row's value is strictly greater than the candidate. Strict greater-than keeps the first occurrence when the maximum repeats; greater-or-equal keeps the last. Skip rows that carry no usable value instead of comparing them. - Return the maximum and its paired
t_stampas the Script data source's output fields, and bind the report components to those fields.
# Illustrative scan. Variable names here are examples for this
# article; the manual's Script data source example shows how the
# previous datasource's rows reach the script on your version.
best_value = None # float, process value in engineering units
best_stamp = None # t_stamp of the row that set best_value
for row in rows:
if row.value is None:
continue
if best_value is None or row.value > best_value:
best_value = row.value
best_stamp = row.t_stamp
The pairing is the whole point: value and t_stamp move together inside one comparison. Taking the maximum in one pass and a timestamp from a different pass or a different row breaks the association and puts a plausible-looking wrong time on the report. The worked example below runs this exact logic on six rows and checks the pair it returns.
Worked example: daily maximum and its timestamp for six sample rows #
The reporting day is 00:00 to 24:00 on a discharge-pressure tag, and the Tag Historian Query returns six rows. The rows are example preview data in bar — they illustrate the scan and are not measurements from a plant.
t_stamp |
value (bar) |
|---|---|
| 06:00 | 12.4 |
| 08:00 | 13.9 |
| 10:00 | 15.2 |
| 12:00 | 15.2 |
| 14:00 | 14.8 |
| 16:00 | 11.6 |
The rule is one pass and one candidate pair, replaced only on strictly greater, with every replacement carrying its own row's timestamp:
- The 06:00 row sets the initial candidate: 12.4 at 06:00.
- 08:00: 13.9 is greater than 12.4, so the candidate moves to 13.9 at 08:00.
- 10:00: 15.2 is greater than 13.9, so the candidate moves to the 10:00 row.
- 12:00: 15.2 is not greater than 15.2, so the candidate keeps the 10:00 row.
- 14:00 and 16:00 are smaller, so nothing changes.
Result: the maximum is 15.2 bar and its t_stamp is 10:00, the first of the two rows holding that value.
Decision: with strict greater-than the report reads 15.2 bar at 10:00; switching the comparison to greater-or-equal makes the identical scan return the 12:00 row. Pick the operator that matches the plant's rule and leave it fixed — the tie decision belongs in the comparison, never in row order.
Query the history tables directly #
A direct query returns the maximum and the winning t_stamp in one result set: filter the history rows to the reporting interval, take the maximum, then select the timestamp of the row whose value equals it.
The Ignition historian writes tag history into partitioned sqlt_data tables; the knowledge-base page on querying Ignition tag historian t_stamp and sqlt_data partitions covers that layout and the timestamp column. A maximum computed against one partition is that partition's maximum only, and a reporting day can span several tables, so the query must cover every partition the interval touches before its answer is the day's maximum. Before writing SQL, confirm where your install writes history — on an Edge install the location differs, and where the Ignition Edge tag historian writes its data names it. Run the query under a read-only login so a report can never modify history.
Equal maxima need an explicit rule here too: a filter of value equal to the interval maximum returns every tying row, so the query must state earliest or latest rather than trusting the order the database returns.
Verify the timestamp end to end #
- Compare the script's maximum against the largest value among the query's preview rows for the same day, and confirm the returned
t_stampbelongs to a row that holds that value. - Check the row count. A Tag Historian Query that stops at its row limit hands the scan a partial day and the maximum quietly becomes the maximum of the returned rows; how a query row limit flattens a historian trend shows that failure without an error message.
- Prove the tie rule on purpose: run a day with a repeated maximum and a second day whose maximum falls at a different hour, and confirm the report shows the occurrence your rule selects.
Then separate the two failure layers. If the datasource preview is right and the rendered report is wrong, a report field is bound to the wrong datasource or column — point it at the Script data source's outputs. If the datasource preview is wrong, fix the range, the row limit, or the scan; the binding was never the problem.
The decision for each case #
- The report already fetches the day's samples and row counts are modest: use the Script data source scan. Nothing is tied to one historian's storage schema, the tie rule lives in a single comparison, and the report works against any storage the query can reach.
- The sample count is too large to pull into the report, or several tags need a maximum with its timestamp in one pass: query the storage tables directly, cover every partition the interval spans, write the tie rule into the SQL, and verify against a day you checked by hand.
In both cases the timestamp must come from the row that set the maximum, and the report binds to that pair. Placing the aggregate value next to a timestamp drawn from any other layer is exactly the failure this avoids.
Questions asked
Is t_stamp in the report local time or UTC?
In Ignition tag history, t_stamp is stored as milliseconds since the Unix epoch in UTC, an absolute instant that does not shift with the report server's timezone. The same instant renders differently depending on the formatting applied, so convert and format at display time with a date-format expression in the report rather than shifting the stored number. If a report and a trend disagree by whole hours, compare their timezone settings before touching the query.
How do I write the tie rule into a direct SQL query?
Filter the rows to the reporting interval, compute the maximum over that interval, then select the rows whose value equals it. MIN over their timestamps returns the earliest occurrence and MAX returns the latest, so write the plant's rule as an aggregate over the timestamps instead of trusting the order the database returns rows. Confirm the behavior on a day with a known repeated maximum before the report goes into service.
Does the same scan work for a daily minimum?
Yes. Keep the timestamp update inside the same comparison and change the test from strictly greater to strictly less, with the candidate initialized from the first valid row. Strict less keeps the first occurrence of the minimum and less-or-equal keeps the last. The same pattern also covers first-value and last-value reports, which reduce to taking the timestamp of the first or last usable row in the interval.
Why does the report's maximum differ from the trend's maximum?
Both numbers are only as good as the rows each one reads. The report scans the rows its query returned, so a row limit, an aggregation mode, or a shorter range changes the maximum, and the trend applies its own aggregation on top. Compare the two over the same interval with aggregation off on both sides; if they still differ, check the query's row limit and the tag's deadband settings before suspecting the scan.
What should the report show when the day has no rows?
Return an empty result and let the report show its no-data state. Initializing the candidate from the first valid row and skipping rows without a usable value keeps the scan from inventing a maximum on an empty or partially bad day. A fallback such as zero would render as a real-looking maximum with a meaningless timestamp, which is worse on an operator report than an empty field.