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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
UncleLewis
Helper V
Helper V

Get Date As Text

Hi all,

 

How can I get Date from DateTime field as Text in format "YYYYMMDD"?

I tried Date.ToText([mydate],[Format = "YYYYMMDD"])

Buget getting error:

 

Parameter Error: We couldn't convert the text value to dateusing the specified format. The format includes a time component. Details: Format = yyyymmdd

 

Thanks

-w

 

1 ACCEPTED SOLUTION
watkinnc
Super User
Super User

Try this:

 

Date.ToText(Date.From([mydate]),[Format = "YYYYMMDD"])

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

View solution in original post

10 REPLIES 10
UncleLewis
Helper V
Helper V

Thanks Nate,

That is giving me an error:

Expression.Error:We cannot convert the value 0 to type Text.
Details:
Value = 0
Type = [Type]

Where mytime = 2/22/2022 9:10:31 AM

Thanks,
-w

My bad!

 

Text.Select(Date.ToText(Date.From([mydate]),[Format = "YYYYMMDD"]), {"0".."9"})

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

Thanks Nate,

That works for YYYYMMDD, I'm trying to get to YYYYMMDD_HHMMSS

I change the format string to YYYYMMDD_HHMMSS
But that is returning a format error

Could a solution be to split the date, time into 2 different fields, convert each field to text, then concatenate each field to the id so the final PK is in the format id_YYYYMMDD_HHMMSS?

 

Thanks,

-w

If it's a datetime field, just use DateTime.ToText, and remove the Date.From. 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
watkinnc
Super User
Super User

OK, in that case I would wrap the whole formula with Text.Select, like:

 

= Text.Select(Date.ToText(Date.From([mydate]),[Format = "YYYYMMDD"]), {0..9})

 

This will return just the "numbers".  You may also wich to consider using Date.ToRecord and Time.ToRecord for these.  These break down each date into a field for year, month and day, and hour, minute, second, respectively.

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
UncleLewis
Helper V
Helper V

Thanks Nate,

 

I have a datetime field

Im trying to convert the date to test in the format YYYYMMDD and the time to text in the format HHMMSS or something similar

My end game it to concatenate to an id to create a unique key since the id can have several rows as a workflow executes.

Thanks,

-w

UncleLewis
Helper V
Helper V

The same pattern does not appear to work for time - why is that?

Here is what I tried:

=Time.ToText(Time.From([mytime]),[Format = "HHMMSS"])
Error: Parameter.Error: We couldn't convert the the text value to time using the specified format. The format includes a date component.

Thanks,

-w

For Time.FromText, it's , [Format = "hh:mm:ss"])

 

Power Query thinks "MM" is months.

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
UncleLewis
Helper V
Helper V

Perfect - Thanks Nate!

watkinnc
Super User
Super User

Try this:

 

Date.ToText(Date.From([mydate]),[Format = "YYYYMMDD"])

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors