Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Converting date time in text format to date time format

The source data I'm trying to convert is in text format as "20201201 163045". I'm using to_timestamp and returning a value "12/01/2020 4:30:45 PM". 

This is SQL I'm using:

to_timestamp(txndatetime, 'yyyy-mm-dd HH24:mi:ss') As EventDate

 

How do I get this to return the date time in 24 hour format?

 

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

In your original text column (with 20201201 163045), you can do a Replace Values step and replace the space with a "T".  Then you can add a custom column with this formula to get your result.

 

= DateTime.ToText(DateTime.FromText([DateTimeText]), "MM/dd/yy HH:mm:ss")

 

Or (recommended) just use

= DateTime.FromText([DateTimeText])

And then use a format string after you load the query on that column to display it with that format (in quotes above).  That way it will stay as a DateTime instead of text.

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Microsoft Employee
Microsoft Employee

In your original text column (with 20201201 163045), you can do a Replace Values step and replace the space with a "T".  Then you can add a custom column with this formula to get your result.

 

= DateTime.ToText(DateTime.FromText([DateTimeText]), "MM/dd/yy HH:mm:ss")

 

Or (recommended) just use

= DateTime.FromText([DateTimeText])

And then use a format string after you load the query on that column to display it with that format (in quotes above).  That way it will stay as a DateTime instead of text.

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat , how would you convert text to datetime if the text is written, "01/23/2024 1:23 PM PST?" The above formulae doesn't work in this case and the timezone would be important. Thanks.

Anonymous
Not applicable

Worked perfectly, thank you very much 🙂

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.