# Calculate the difference time between two table entries

**URL:** <https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [January 15, 2024, 3:49pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274 "2024-01-15T15:49:31Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johann\_Simetsberger](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Johann\_Simetsberger](https://forum.inductiveautomation.com/u/Johann_Simetsberger)\
**Post date:** [January 15, 2024, 3:49pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/1 "2024-01-15T15:49:31Z")

</div>

Hello  
I am a beginner and need some info on how to calculate the difference time between two table entries / table rows? (for cycle time calculation)  
For example row one to row two.

It would also be ideal if I could calculate the average cycle time within a period.

Thank's

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/d/d/dd66d660a60d2befd755b82948fa5acb7b1ea67d.png)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [January 15, 2024, 3:54pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/2 "2024-01-15T15:54:08Z")

</div>

Were do you want to display the result - as another column in the table? Assuming you are using a named query you can leverage a window function and some date arithmetic to do it directly in your query. Checkout out the `LAG` window function.

---

<div class="post-metadata">

**Author:** ![Eagel007](https://avatars.discourse-cdn.com/v4/letter/e/7bcc69/32.png) [@Eagel007](https://forum.inductiveautomation.com/u/Eagel007)\
**Post date:** [January 15, 2024, 3:54pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/3 "2024-01-15T15:54:42Z")

</div>

I achieved something like this with a DATEDIFF function in sql query and creating new column.

Example DATEDIFF(M, START DATE, END DATE) AS COLUMN\_NAME

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [January 15, 2024, 3:56pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/4 "2024-01-15T15:56:45Z")

</div>

They will need `DATEDIFF` but what you're describing is comparing two columns in the same row. It seems like they want to compare the same date column, the current row compared to the previous one (or perhaps the one in front of it). They will need something like `LAG` or `LEAD` incorporated into this to work properly and compare one row to the next.

---

<div class="post-metadata">

**Author:** ![Johann\_Simetsberger](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Johann\_Simetsberger](https://forum.inductiveautomation.com/u/Johann_Simetsberger)\
**Post date:** [January 15, 2024, 4:02pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/5 "2024-01-15T16:02:49Z")

</div>

Hello  
Thanks for the quick feedback  
I would like to calculate the difference time between two rows in a column.  
The value should then simply be displayed in a label

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/5/8/5895230955f21e0d76945986905155bf9564e49a.png)

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [January 15, 2024, 4:06pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/6 "2024-01-15T16:06:41Z")

</div>

Is it always ONLY going to be the top two date's of the table compared, and then put into a single label component? You don't care about the differences between any subsequent row date values

---

<div class="post-metadata">

**Author:** ![Johann\_Simetsberger](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Johann\_Simetsberger](https://forum.inductiveautomation.com/u/Johann_Simetsberger)\
**Post date:** [January 15, 2024, 4:10pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/7 "2024-01-15T16:10:36Z")

</div>

The two rows should be selectable or simply the selected row should always be compared with the last but one row

---

<div class="post-metadata">

**Author:** ![Johann\_Simetsberger](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Johann\_Simetsberger](https://forum.inductiveautomation.com/u/Johann_Simetsberger)\
**Post date:** [January 15, 2024, 4:16pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/8 "2024-01-15T16:16:08Z")

</div>

I got the following answer from ChatGPT but I can't get it implemented or apply it.

Assuming you have a table named `MyTable` and a timestamp column named `TimestampColumn`:

1. **Create a Button Component:**

2. **Configure the onClick Event:**

3. **Write the Script:**

4. **Run the Project:**

5. **Select Two Rows and Click the Button:**

This script checks if exactly two rows are selected in the specified table. If two rows are selected, it retrieves the timestamps from the selected rows and calculates the time difference between them using the `system.date.between` function.

Feel free to adapt the column names and script logic based on your actual table structure and requirements. If you have any specific questions or encounter difficulties, feel free to ask!

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [January 15, 2024, 4:27pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/9 "2024-01-15T16:27:35Z")

</div>

I assume you don't want it on button press right? Ask it to do it again for you using the expression language.

---

<div class="post-metadata">

**Author:** ![Johann\_Simetsberger](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Johann\_Simetsberger](https://forum.inductiveautomation.com/u/Johann_Simetsberger)\
**Post date:** [January 15, 2024, 5:22pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/10 "2024-01-15T17:22:16Z")

</div>

I have now received this suggestion from CGPT

if(len({Root Container.MyTable.selectedRows}) == 2,  
dateDiff({Root Container.MyTable.selectedRows}[1].TimestampColumn, {Root Container.MyTable.selectedRows}[0].TimestampColumn, 'seconds'),  
'Select exactly two rows to calculate the time difference.'  
)

I tried it but got an error message

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/d/3de02186ab64c6107942f2402c5da3296bfd9e21.png)

---

<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:** [January 15, 2024, 6:15pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/11 "2024-01-15T18:15:30Z")

</div>

See [Wiki - how to post code on this forum](https://forum.inductiveautomation.com/t/wiki-how-to-post-code-on-this-forum/77831).

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [January 15, 2024, 6:23pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/12 "2024-01-15T18:23:19Z")

</div>

Maybe ask chatgpt how to post code correctly in a forum. 😂

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [January 15, 2024, 7:23pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/13 "2024-01-15T19:23:39Z")

</div>

Ok I only said that as a point - ChatGPT can be helpful if you already know what you are doing and have it do grunt work but otherwise you should get familiar with reading documentation, it will take you much farther and just give you the right answer if you're willing to read for it - [Expression Language and Syntax - Ignition User Manual 8.1 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC81/Expression+Language+and+Syntax#ExpressionLanguageandSyntax-DatasetAccess)

---

<div class="post-metadata">

**Author:** ![Johann\_Simetsberger](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Johann\_Simetsberger](https://forum.inductiveautomation.com/u/Johann_Simetsberger)\
**Post date:** [January 15, 2024, 7:56pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/14 "2024-01-15T19:56:57Z")

</div>

Thanks for the feedback  
I haven't solved the problem yet but I will try to find a solution or help in the documentation.

---

<div class="post-metadata">

**Author:** ![Felipe\_CRM](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@Felipe\_CRM](https://forum.inductiveautomation.com/u/Felipe_CRM)\
**Post date:** [January 15, 2024, 8:50pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/15 "2024-01-15T20:50:38Z")

</div>

Some of the issues I see:

- Your code is missing the entire last part of the `if` function so that's definitely going to cause an error.
- Check [here](https://docs.inductiveautomation.com/display/DOC81/dateDiff) in particular to see what `dateDiff` is expecting for its parameters ("seconds" is not quite right).
- As bkarabinchak.psi hinted, your dataset access syntax isn't correct either (thanks a lot ChatGPT) although it certainly looks convincing at first.
- `Table.props.selectedRows` isn't even a real property that exists.

Check [the properties of the `selection` prop](https://docs.inductiveautomation.com/display/DOC81/Perspective+-+Table#PerspectiveTable-Properties) for the right name of the item that you'll want to check for length and also to access your dates from.

After you fix this, there may be other things wrong. If you hover over the error message it will often give you a more detailed message that will give you a hint to what is wrong.

---

<div class="post-metadata">

**Author:** ![Johann\_Simetsberger](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Johann\_Simetsberger](https://forum.inductiveautomation.com/u/Johann_Simetsberger)\
**Post date:** [January 15, 2024, 10:20pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/16 "2024-01-15T22:20:22Z")

</div>

thanks  
the difference sum now works with fixed given rows

`dateDiff({../Table.props.data}[4, "Prüfzeit"], {../Table.props.data}[3, "Prüfzeit"], "seconds")`

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/3/6397def33c0cdd0e78cdad0fe085bc38618e4098.png)

The question now is how do I get a flexible row selection?  
With this expression I get the value for a selected row in a label  
It would be nice when I could unse the same expression for an other label but with the value from one row before or after  
Then I could put the Values from the two labels into the dateDiff

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/f/b/fbd41a9178b050ae1dde6f2fb01e425c1ab4cb3e.png)

---

<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:** [January 15, 2024, 10:29pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/17 "2024-01-15T22:29:21Z")

</div>

I think it would be much more useful to your users to have the difference displayed in the table so that the user doesn't have to click around so much. The extra column(s) and calculations can be added using a script transform on the data binding.  
Would you be interested in this?

---

<div class="post-metadata">

**Author:** ![Johann\_Simetsberger](https://avatars.discourse-cdn.com/v4/letter/j/9dc877/32.png) [@Johann\_Simetsberger](https://forum.inductiveautomation.com/u/Johann_Simetsberger)\
**Post date:** [January 17, 2024, 3:17pm UTC](https://forum.inductiveautomation.com/t/calculate-the-difference-time-between-two-table-entries/84274/18 "2024-01-17T15:17:10Z")

</div>

I have now managed to calculate the cycle time

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/7/07371b9c4d6f4592bfc49f5784e3ec457861fcd0.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/4/64f66df6acb85aa0e8289a08ccdb0995e253d4dd.png)

[Link Help](https://www.sqltutorial.org/sql-window-functions/sql-lag/)
