Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
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.
Solved! Go to Solution.
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
To learn more about Power BI, follow me on Twitter or subscribe on YouTube.
Hola
Hello@KunalL
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
Trans
Copy 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
@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
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.
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
To learn more about Power BI, follow me on Twitter or subscribe on YouTube.
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.
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
To learn more about Power BI, follow me on Twitter or subscribe on YouTube.
Hello @mahoneypat
Thank you for your solution.
Can you please share more detials about how to convert text like row no. 2 and 4 into date?
The integer version of dates are relative to that starting date, so for those rows (those that don't have "/"), the expression adds that many days to it (-1).
Pat
To learn more about Power BI, follow me on Twitter or subscribe on YouTube.
Thanks, the solution you suggested did not work, I got the same error message. However, I may have found a solution. Not sure how, but the solution suggested by @myndworkz on page 2 here worked for me.
I changed the Locale setting in Options from English (United Kingdom) to English (United States). Also tested it with the table where I had converted the date using my unelegant method as described in the issue description (call it table A) and on the table coming in from the original CSV with formatting issue (table B). When I made this change and refreshed, table A fell apart while table B displayed dates corrected in DD/MM/YYYY. When I changed it back to English (United Kingdom), a refresh broke table B but table A now displayed dates in DD/MM/YYYY.
Would you know why?
I don't change the default locale so haven't seen that behavior. Not sure why that worked, but hopefully you are able to move forward now.
Regards,
Pat
To learn more about Power BI, follow me on Twitter or subscribe on YouTube.
@KunalL ,
You need to change you regional settings on pc.
If you check bottom-right corner you will see that currently date is displayed in "us" format.
Update regional settings like this and it will work:
Hi! @KunalL
I had the same problem, try Date.FromText() it behaves difrently from TransformColumnType
Consider marking this as a solution if it solved your problem.
Thanks, Idid try it but this did not work. I had seen this in one of the posts earlier but I think this is more suitable for scenarios where the column has the entire date-time value in text and this needs to be converted to pure date format?
@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.
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.
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
72 | |
67 | |
42 | |
28 | |
21 |