# Timestamp for last sample in a signal

## Recommended Posts

Hi - I am wanting to get a time value for the last sample in a signal. What is the formula for this?

I am using it to create a 24hr average from the last value in a signal, and have this updating as the signal is updated. the now() function takes the current time, not the last sample time.

##### Share on other sites

Hi Ruben,

you can do it by using this formula. The formula gets the values for the last eight hours, picks the last sample and gets its key (timestamp). Based on that timestamp the condition of the previous 24 hours is build.

You can also use pick(-1) instead of last(). You should keep in mind that the capsule itself is uncertain because of the use of now().

Regards,

Thorsten

##### Share on other sites

• 1 year later...
• Seeq Team

Post below documents how you could pull the last sample's timestamp into a Scorecard - in case all you wanted was to just view that timestamp.

Enter the following into a new Formula:

```//In this formula, we will create a signal of all the timestamps for each sample point coming into Seeq

//Create a condition with tiny capsules for each sample point
// NOTE - to the get the accurate timezone date/time, I need to delay my signal according to my UTC-6h adjustment for Mountain timezone
//        if you are in Central for instance, you would do UTC-5h, or .delay(-5h)
\$sampleCapsules = \$signal.delay(-6h).toCapsules()

//Find the timestamp of each recorded sample point
// NOTE - you may need to change the 1d to a larger value, as this is the interpolation length
//        so if your samples are spaced by more than 1d, increase to 2d and see if this interpolates
\$sampleCapsules.toSignal('End').toLinear(1d).toString()

//Below transform is Optional to view the timestamp in a neater format of MM/DD/YYYY HH:MM
.replace('/(?<year>....)-(?<month>..)-(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' ,
'\${month}/\${day}/\${year} \${hour}:\${minute}')```

Then, in a new Scorecard Metric, grab the Value at End. Every time you return to Seeq, you can "step to now" and get the latest timestamp for the last sample, corrected for your timezone per the delay above.

##### Share on other sites

• 2 years later...

How to customize timestamp format, if I want to display like this "20 April 2022"?

##### Share on other sites

• Seeq Team

Hi Nurhaz,

The easiest will be to configure at the header. In this example, I used Simple Table >> Last Value, followed by customizing the header as showed in the second screenshot. If you click at the '?' beside the Date Format it will provide you more formatting options. Finally, you can add another column and name it 'Last Sample Time".

This method is applicable still if you prefer to use Scorecard Metric.

The difference between this method and the one discussed above is this method will generate the last value too.

##### Share on other sites

Oh sorry, my mistake. I mean this formula.

```//Below transform is Optional to view the timestamp in a neater format of MM/DD/YYYY HH:MM
.replace('/(?<year>....)-(?<month>..)-(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' ,
'\${month}/\${day}/\${year} \${hour}:\${minute}')```

I want to show the scorecard value as a date "20 April 2022" like this.

Edited by nurhazx
##### Share on other sites

• Seeq Team

Yes, understood but it will not be straightforward though thus I recommended formatting the header. Can you please let me know the background of your use case? It it still in the context you would like to display the time of the last value? If yes, may I know why you do not prefer to configure the header.

##### Share on other sites

I created scorecard when the signal reach high alarm.

\$f=(\$signal < \$alarm).afterStart(1s)

\$f.toSignal('End').toLinear(1d).toString()
.replace('/(?<year>....)-(?<month>..)-(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' ,
'\${month}-\${day}-\${year} \${hour}:\${minute}')

The value comes out like this

I want to change the date format

##### Share on other sites

• Seeq Team

Hi Nurhaz,

This is one possible method:-

Step 1 : Create a new formula where we convert the month format based on your preference

```\$month_numeric =

\$f
.toSignal('End')
.toString()
.replace('/(?<year>....)-(?<month>..)-(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' ,
'\${month}')

//changing the name format

\$month_alphabet =

experimental_lookupTable(
"[
['01', 'Jan'],
['02', 'Feb'],
['03', 'March'],
['04', 'April'],
['05', 'May'],
['06', 'June'],
['07', 'July'],
['08', 'August'],
['09', 'September'],
['10', 'October'],
['11', 'November'],
['12', 'December']
]",
InterpolationMethod.Discrete)
\$month_alphabet.experimental_lookup(\$month_numeric, 'B')```

Step 2 : Configure the sequence/format based on your preference

```\$extract = \$f.toSignal('End').toString()

\$day = \$extract
.replace('/(?<year>....)-(?<month>..)-(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' ,
'\${day}')

\$YYYYhhmm = \$extract
.replace('/(?<year>....)-(?<month>..)-(?<day>..)T(?<hour>..):(?<minute>..):(?<sec>..)(?<dec>.*)Z/' ,
'\${year} \${hour}:\${minute}')

\$final = \$day + ' ' +\$month_alphabet+ ' ' +\$YYYYhhmm
return \$final```

Hope this helps.

Edited by Sharlinda Salim
##### Share on other sites

Yups, it works.

Many thanks Sharlinda😄

##### Share on other sites

• Super Seeqer

A newer and simpler method for this using the Table view and conditions is linked below