Forum Discussion
Convert Data Type from Text to Date and into a Different Date Format
Hi,
New to Power BI and trying my hand with datasets.
Imported a CSV where I found that the date columns in MM/DD/YYYY format have been detected as Text. I would like to have it as DD/MM/YYYY. So right clicked the column, selecting Change Type > Using Locale and then selecting Data Type as Date and Locale as English (UK). Some dates did convert correctly but some gave an error.
Tried a very unelegant way after this - splitting the column into three columns for MM, DD and YYYY and then merging them to give DD/MM/YYY. Pretty sure there is a more sophisticated way to do it.
Thanks.
Coming back to this, I realize the function I proposed is not the right one. Date.ToText assumes your values are already Date type, but yours are text.
Did you say some of your values are in general format vs date? Does that mean you input table looks like the column on the left (dates stored as integers and date formats).
If so, you can use a formula like this to convert each type differently. We may now be beyone the more elegant solution you initially requested, but I will share this anyway in case it helps.
= if Text.Contains([Date], "/") then Date.ToText(Date.FromText([Date]), "dd/MM/yyyy") else Date.ToText(Date.AddDays(#date(1899,12,31), Number.FromText([Date])-1), "dd/MM/yyyy")
The above is an added custom column, and you should be able to convert it to Date in the following step.
If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
19 Replies
- Greg_DecklerCommunity Champion
KunalL - Not sure why some of your values would cause an error. In terms of what you did, it's funny because that is the exact approach that I use when people post their data to the forums that are mm/dd/yyyy and I want it in dd/mm/yyyy. I think I posted an idea to make it a button or option to convert from one to the other.
- KunalLFrequent Visitor
I got an answer by looking up the error message and searching for a post for that here - I saw your response here.
I saw that its the CSV file that has the issue. Some of the cells in the column are formatted General while some are Date. Those with General formatting are the ones that are shown with the Error link saying 'DataFormat.Error: We couldn't parse the input provided as a Date value. Details: 8/26/1961.' While this may be out of the scope of Power BI, I wonder if you could point me in the direction of the resolution?
Thanks.
- mahoneypatMicrosoft Employee
You could also try to add a custom column with Date.ToText([DateColumn], "dd/MM/yyyy"). Upper/lower case makes a difference. And then change it to Date type.
If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
- KunalLFrequent Visitor
I tried what you suggested. I got an error that says "Expression.Error: We cannot convert the value "1/1/1997" to type Date".
The step is shown in PBI as (may be this helps you resolve)
= Table.AddColumn(#"Promoted Headers", "Custom", each Date.ToText([date], "dd/MM/yyyy"))I also tried using mm instead of MM but it didn't work.
- mahoneypatMicrosoft Employee
Coming back to this, I realize the function I proposed is not the right one. Date.ToText assumes your values are already Date type, but yours are text.
Did you say some of your values are in general format vs date? Does that mean you input table looks like the column on the left (dates stored as integers and date formats).
If so, you can use a formula like this to convert each type differently. We may now be beyone the more elegant solution you initially requested, but I will share this anyway in case it helps.
= if Text.Contains([Date], "/") then Date.ToText(Date.FromText([Date]), "dd/MM/yyyy") else Date.ToText(Date.AddDays(#date(1899,12,31), Number.FromText([Date])-1), "dd/MM/yyyy")
The above is an added custom column, and you should be able to convert it to Date in the following step.
If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
- KunalLFrequent Visitor
- amitchandakSuper User
KunalL ,
if you date are in UK format and you want to work with those -https://community.powerbi.com/t5/Desktop/How-to-apply-UK-date-format-dd-mm-yyyy-in-Date-slicer/td-p/...
Check an option in data format (screenshot at last) or under formatting string for that :https://docs.microsoft.com/en-us/power-bi/desktop-custom-format-strings
Else create a date string like (if it is detected as date )
Format([Date],"dd-mm-yy")
Format([Date],"mm-dd-yy")
Format option
- KunalLFrequent Visitor
Thanks, this is another way. But I would like to do it in Query Editor. Because I just saw a video where it was recommended that column additions be done in Query Editor first and then as any Calculated Column - please correct as per your experience.
- Jimmy801Community Champion
HelloKunalL
you can use Table.TransformColumns
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUNzDSNzIwMlCK1YlWMgSyTRFcc31DGC8WAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]), Trans = Table.TransformColumns(Source, {"Date",each Date.FromText(_,"en-US"), type date}) in TransCopy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy - Gatita69New Member
Hola