Calculate the difference time between two table entries

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

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.

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

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.

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

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

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

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:

    • Drag and drop a Button component onto your Perspective view.
  2. Configure the onClick Event:

    • Select the button and go to the Props panel.
    • Under Events, find the onClick event.
    • Click on the scripting icon to open the script editor.
  3. Write the Script:

    • Use the script editor to write a script that calculates the time difference between the selected rows in the table. For example:
    selectedRows = self.view.custom.MyTable.selectedRows
    
    if len(selectedRows) == 2:
        timestamp1 = selectedRows[0]['TimestampColumn']
        timestamp2 = selectedRows[1]['TimestampColumn']
        
        timeDifference = system.date.between(timestamp1, timestamp2)
        
        print(f"Time difference between selected rows: {timeDifference}")
    else:
        print("Select exactly two rows to calculate the time difference.")
    

    Replace MyTable with the actual name of your table and TimestampColumn with the name of the timestamp column.

  4. Run the Project:

    • Save the project and run the Perspective session.
  5. Select Two Rows and Click the Button:

    • Select exactly two rows in the table.
    • Click the button to execute the script.

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!

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

1 Like

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

See Wiki - how to post code on this forum.

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

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

1 Like

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

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 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 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.

2 Likes

thanks
the difference sum now works with fixed given rows

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

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

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?

1 Like

I have now managed to calculate the cycle time


Link Help