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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Aha2022
Regular Visitor

convert text to date

,mn.JPG I want to convert this text column to a date column and add the day name to the date.

 

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @Aha2022 

 

You can first add a custom column to get the date values without "st/nd/rd/th" into a new column.

Text.RemoveRange([Date], Text.PositionOf([Date], " ")-2, 2)

vjingzhang_0-1669012511136.png

 

Then change the new column to Date type. With the new format, dates can be converted into Date type successfully. 

vjingzhang_1-1669012580536.png

 

If you want to change the format of dates, you need to apply the data to Power BI Desktop and change the column format there, just like below image. In Power Query Editor, it doesn't support to change the date format when the column is of Date type. 

vjingzhang_2-1669012970447.png

 

If you don't want to add a custom column to get the new date values, you can transform the original column with the following M code. Then modify the column type to Date and apply it to Power BI Desktop to change the date format. 

= Table.TransformColumns(#"Changed Type", {{"Date", each Text.RemoveRange(_, Text.PositionOf(_, " ")-2, 2)}})

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

 

 

View solution in original post

3 REPLIES 3
RicardoTeixeira
Frequent Visitor

Function for Power query that converts a text with dates (like 11/5/2023 or  4.5.22 ) to dates based on a kwon format (MM.DD.YYYY).

(Creating a function from an existing endpoint is a simple process. Right-click on the endpoint name and choose Create Function.)

Over the new function, in advace editor past the code:

let
Source = (dateColumn as text,DateSchema as text) => let
MyDelimiter = Text.Range(DateSchema,Text.PositionOfAny(DateSchema,{"/","-",".","\","_"}),1),
DateList = Splitter.SplitTextByDelimiter(MyDelimiter, QuoteStyle.None)(dateColumn),
SchemaList = Splitter.SplitTextByDelimiter(MyDelimiter, QuoteStyle.None)(DateSchema),
TableDate = Table.FromColumns({DateList,SchemaList}),
myDay = "0" & Text.From(Table.SelectRows(TableDate,each Text.Upper(Text.Range([Column2],0,1)) = "D" )[Column1]{0}),
myDD = Text.Range(myDay,Text.Length(myDay)-2,2),
MyMonth= "0" & Text.From(Table.SelectRows(TableDate,each Text.Upper(Text.Range([Column2],0,1)) = "M" )[Column1]{0}),
myMM = Text.Range(MyMonth,Text.Length(MyMonth)-2,2),
myYear= "20" & Text.From(Table.SelectRows(TableDate,each Text.Upper(Text.Range([Column2],0,1)) = "Y" )[Column1]{0}),
myYYYY = Text.Range(myYear,Text.Length(myYear)-4,4),
DateByParts = myYYYY & "-" & myMM & "-" & myDD,
DateCleaned = Date.FromText(DateByParts)
in
DateCleaned
in
Source

Then invoke the function in a new column in power query.

v-jingzhang
Community Support
Community Support

Hi @Aha2022 

 

You can first add a custom column to get the date values without "st/nd/rd/th" into a new column.

Text.RemoveRange([Date], Text.PositionOf([Date], " ")-2, 2)

vjingzhang_0-1669012511136.png

 

Then change the new column to Date type. With the new format, dates can be converted into Date type successfully. 

vjingzhang_1-1669012580536.png

 

If you want to change the format of dates, you need to apply the data to Power BI Desktop and change the column format there, just like below image. In Power Query Editor, it doesn't support to change the date format when the column is of Date type. 

vjingzhang_2-1669012970447.png

 

If you don't want to add a custom column to get the new date values, you can transform the original column with the following M code. Then modify the column type to Date and apply it to Power BI Desktop to change the date format. 

= Table.TransformColumns(#"Changed Type", {{"Date", each Text.RemoveRange(_, Text.PositionOf(_, " ")-2, 2)}})

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it.

 

 

Jayee
Responsive Resident
Responsive Resident

Hi @Aha2022 

 

If you can remove unnecessary characters like th, Rd, st and ",' you can conver it to date it is best to do the cleaning and treating date field as date type then you can perform all the date functions and time intelligence in DAX.

 

 

If this post helps, then please consider Accept it as the solution, Appreciate your Kudos!!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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