Converting dates and timestamp fields in QuickSight

Date and timestamp fields can be represented in a seemingly endless number of formats. Many of these are automictically recognised by QuickSight, but if your date and timestamp fields use some of the more complex or non-standard formats you will need to convert them in QuickSight while preparing your dataset to a Date datatype (which can include either dates or dates plus times).

Using the supported date formats you can easily convert these timestamp “strings” into a supported datatype by using the Change data type option in the dataset editor:

image

This gives you the ability to specify the correct format and test it using the Validate option, for example:
image

The ability to specify the date format works well for many incoming dates, such as:

Timestamp Format
280123 ddMMyy
28012023 ddMMyyyy
28-01-2023 dd-MM-yyyy
28/01/2023 dd/MM/yyyy
28.01.23 12:00 dd.MM.yy hh:mm

But some timestamps can prove more challenging, such as handling additional characters like commas, or T’s and Z’s as per the ISO 8601 date format:

Timestamp Format
2023, Jan 28 12:01:23 yyyy, MMM dd HH:mm:ss
28-01-2023T12:00:00Z dd-MM-yyyy'T'HH:mm:ss'Z'
28-01-2023T12:00:00.000Z dd-MM-yyyy'T'hh:mm:ss.SSS'Z'

However, some timestamps are formatted in such a way that you cannot just edit the format in the QuickSight dataset, in certain situations you may need to invoke the parseDate() function in a calculated field to handle additional components of a timestamp, such as AM/PM:

Timestamp Calculated Field
28/01/2023 06:00 AM parseDate({InputDate},'dd/MM/yyyy hh:mm a')
28/01/2023 06:00:00 PM parseDate({InputDate},'dd/MM/yyyy hh:mm:ss a')

Then, there are cases where unsupported components of a timestamp (those not supported in QuickSight by the Joda DateTimeFormat class) need to be handled. For example, take the following examples for ordinal day numbers, i.e. those containing “st”, “nd”, “rd” or “th”:

Timestamp
1st Jan 2023 12:00:00
2nd Jan 2023 12:00:00
3rd Jan 2023 12:00:00
14th Jan 2023 12:00:00
21st Jan 2023 12:00:00

The above timestamps can be converted by first removing the day number suffixes and then parsing the timestamp with parseDate(), I created 2 calculated fields to achieve this. The first, DateStripped, locates the suffix with substring() and then removes the suffix using concat() and substring() together. The second, DateParsed, then uses parseDate() on DateStripped to handle single or double-digit days in the timestamp:

Calculated Field Formula
DateStripped ifelse(
    substring({InputDate},3,2)='th' OR
    substring({InputDate},3,2)='nd' OR
    substring({InputDate},3,2)='st' OR
    substring({InputDate},3,2)='rd',
    concat(substring({InputDate},1,2),substring({InputDate},5,strlen({InputDate})-4)), ifelse(
      substring({InputDate},2,2)='th' OR
      substring({InputDate},2,2)='nd' OR
      substring({InputDate},2,2)='st' OR
      substring({InputDate},2,2)='rd',
      concat(substring({InputDate},1,1),substring({InputDate},4,strlen({InputDate})-3)), NULL
    )
)
DateParsed ifelse(
    parseDate({DateStripped},'dd MMM yyyy HH:mm:ss')=NULL,
    parseDate({DateStripped},'d MMM yyyy HH:mm:ss'),
    parseDate({DateStripped},'dd MMM yyyy HH:mm:ss')
)

Finally, some timestamps are stored as an epoch value, which is an integer representation of a date as the number of seconds since 00:00:00 UTC on January 1, 1970, to convert these, there is a dedicated function epochDate you can use in a calculated field:

Timestamp Calculated Field
1675122072 epochDate(InputDate)

These are just some examples of how to handle incoming dates and convert them to Date datatypes in QuickSight.

Feel free to comment and/or add your own examples below to make this a living article that benefits the wider community.

Hope this helps,
Andrew

@abacon - thanks very much for this. Date fields can be quite tricky…

I have a question on the use of epoch dates. I have a field in my upstream data which I believe is a Bitwise date - an example below:

6951142541622250792

Converted in exel : =INT((A1/(2^22)))/1000

Result - 1657281528, then converted to Epoch is 08/07/2022.

My data is stored in S3 and I will be moving it into Athena via Glue - Should I convert this to a human readable date along the way, or am I better off passing the epoch result (1657281528), then using the epochDate in a calculated field? If the latter, does this have speed of query impacts on the dashboard as it is having to constanctly calculate on the fly (my data is time series based)?

Thanks,
Q

1 Like

Hi @QSCommUser,
If your requirement is for dashboards to be as performant as possible, then can I assume you are using SPICE? If so, SPICE encodes datetimes as 64 bit integers. As you identified, if you choose to store the epoch result instead, you would have to make an additional call to epochDate to convert it. This could add computational overhead to your dashboards. Therefore, I would ensure that datetimes emitted from Athena can be ingested by QuickSight directly into its native Date data type.
Are you needing to perform any calculations on the dates or are they simply being used as a dimension?
Thanks,
Andrew

1 Like

Hi @abacon,

My data is in ISO 8601 date format. Ex timestamp: 28-01-2023T12:00:00.000Z; I tried changing the data type to Date by specifying the format as: dd-MM-yyyy’T’hh:mm:ss.SSS’Z’, but its not accepting it. Please refer screenshot attached.

dateFormat

Also, this particular format is not listed among the supported date formats.

Thanks,
Shailesh

Hi @gshaileb,

I just re-ran using my data and this works, can you check if you are using the correct single apostrophe character? e.g.

Use (U+0027 apostrophe) and not (U+2019 right single quotation mark)

Note the very subtle difference.

Many Thanks,
Andrew

1 Like

Thank you this solved it.

1 Like

Hi @abacon,

I have tried the following format, with the apostrophe, but its not working.

yyyy-MM-dd’T’hh:mm:ss.SSS’Z’

I’m still getting the same"We encountered an error trying to validate the data format. Please try again".

Is there anything that I’m missing?

Thanks.

Hi @iamtarun,

If you copy and paste the formatting string from my post, then unfortunately apostrophe’s (U+0027) are converted to right single quotation mark (U+2019) when I post my reply here.

Please ensure that you are using the apostrophe character and not the right single quotation mark. You can check which character you are using by pasting it into the “Type Here” field on this website.

Many Thanks,
Andrew

Hi @abacon,

Thanks for the response. I tried again, this time I’ve checked that I’m pasting the correct one (U+0027) using the website you’ve shared. But still I get the same error in QS. Is there any other way to input this character without pasting it?

Hi @iamtarun,

Are you able to share a screenshot (as per gshaileb’s post above) so that I can see the error and the “Source data” snippet?

Many Thanks,
Andrew

Hi there @abacon I am having the same problem as iamatarun above.

Steps to reproduce:

  1. Change field from type String to type Date.
  2. In the ‘Edit date format’ box I entered: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
  3. I double-checked that in the checker you mentioned, and they are all apostrophes: Unicode lookup
  4. I hit Validate, and am successful (see screenshot)
  5. I hit Update
  6. I hit “Save and Publish”

At this point, within a few seconds, the refresh fails with the following error. Note that I do not have any calculated fields in this dataset, all of the other fields are strings.

Hi @bhamplify,

I’ve re-run the test, but couldn’t reproduce the result, would you be able to open a support case and provide them the details including the Ingestion ID?

Here are the steps to open a support case so that we can help you further: Creating support cases and case management - AWS Support . If your company has an internal IT team, you might not have direct access to AWS Support and will need to raise an internal ticket to your IT team. They’ll open an AWS Support case on your behalf.

Many Thanks,
Andrew

Thank you for your help; I will. I do wonder because what you were able to run is different than what is in the official documentation…anyway, hopefully I can get an answer. Thanks!

I want to completely remove timestamp once I import in CSV. I have tried multiple formats but everytime I get the timestamp. Is there a way to remove it without using calculated fields?