Are you tired of manually trying to extract a date from text values in Power Query? It can be a real headache, especially when dealing with different date formats and culture codes. But this article is here to show you an effective approach using the Date.FromText function.
With this function, you’ll be able to automatically extract dates from text, extract a date based on a specific format, and even retrieve a date based on a format and culture code.
So what approach can you use to convert a data type from text to date?
Table of contents
Introducing Date.FromText
A very versatile function for turning text into a date is the Date.FromText function. As first argument, you can provide the function with a text value. If your value is an integer, please make sure to transform it to a text value first.
The Date.FromText function automatically tries to recognize the date format a text value has, making it a very powerful tool for data analysis. With this function, you can automatically extract dates from text, extract a date based on a specific format, and even retrieve a date based on a format and culture code.
So what does that work?
Auto Recognize Dates
One thing that’s really cool about the Date.FromText function is that it can automatically try to recognize a date format from a text value.
After providing the function with your text, Power Query tries to convert your text to a date value. The following text values transform successfully to a date value:
= Date.FromText( "20221231" ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "31-12-2022" ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "2022-12-31" ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "2022/12/31" ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "31.12.2022" ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "01,01,2022" ) // Returns #date( 2022, 1 , 1 )
= Date.FromText( "1997/1/1" ) // Returns #date( 1997, 1 , 1 )
= Date.FromText( "1/1/1997" ) // Returns #date( 1997, 1 , 1 )
= Date.FromText( "12 MAR 22" ) // Returns #date( 2022, 3 , 12 )
= Date.FromText( "31..12..2022" ) // Returns #date( 2022, 12, 31 )
When Power Query recognizes the text as a date, it automatically converts the string to a date.
Note
Note that results may differ depending on the culture of your device. A computer working on US system settings, recognizes dates different than a Dutch one. Later this article, we cover how you can work around this using a formatting string or a culture code.
Many times, however, text values are not recognized as dates. For example:
= Date.FromText( "31122022" ) // ddMMyyyy returns an Error
= Date.FromText( "12312022" ) // MMddyyyy returns an Error
= Date.FromText( "20223112" ) // yyyyddMM returns an Error
= Date.FromText( "12-31-2022" ) // MM-dd-yyyy returns an Error
= Date.FromText( "2022-31-12" ) // yyyy-dd-MM returns an Error
= Date.FromText( "31*12*2022" ) // dd*MM*yyyy returns an Error
= Date.FromText( "31.*12.*22" ) // dd.*MM.*yy returns an Error
Manual Date Format
To correctly transform a text string to a date value, you can make use of the optional options parameter in the second argument. You can input a formatting string (within a record) to instruct the function which format the text value has. The examples that returned an error before, work with the below formatting instructions.
= Date.FromText( "31122022", [Format="ddMMyyyy"] ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "12312022", [Format="MMddyyyy"] ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "20223112", [Format="yyyyddMM"] ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "12-31-2022", [Format="MM-dd-yyyy"] ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "2022-31-12", [Format="yyyy-dd-MM"] ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "31*12*2022", [Format="dd*MM*yyyy"] ) // Returns #date( 2022, 12, 31 )
= Date.FromText( "31.*12.*22", [Format="dd.*MM.*yy"] ) // Returns #date( 2022, 12, 31 )
You can build your custom-format strings using any of the following strings:
Format | Description | 31/12/2023 | 01/02/2003 |
---|---|---|---|
%d | Single Digit Day (1-31) | 31 | 1 |
dd | Double Digit Day (01-31) | 31 | 01 |
ddd | Short Weekday Name | Sun | Sat |
dddd | Full Weekday Name | Sunday | Saturday |
%M | Single Digit Month (1-12) | 12 | 2 |
MM | Double Digit Month (01-12) | 12 | 02 |
MMM | Short Month Name | Dec | Feb |
MMMM | Full Month Name | December | February |
%y | Year (0-99) | 23 | 3 |
yy | Year (00-99) | 23 | 03 |
yyy | Year with at least three digits | 2023 | 2003 |
yyyy | Four-Digit Year | 2023 | 2003 |
yyyyy | Five-Digit Year | 02023 | 02003 |
m, M | Day followed by Full Month Name | 31 December | 1 February |
y, Y | Standard Long Date | December 2023 | February 2003 |
d | Standard Short Date | 31/12/2023 | 01/02/2003 |
D | Full Long Date | 31 December 2023 | 01 February 2003 |
%g, gg | The Period of an Era | A.D. | A.D. |
Tip
You can also find these custom format strings for time, datetime, datetimezone and number values as described in this guide on formatting strings.
Provide Culture Code
All examples before work regardless of the language your machine has. Yet, sometimes a notation can be different depending on the culture. For example, when taking into account the month name.
In those cases, you can provide the options parameter with a culture. When omitting this culture, the function by default uses the culture of your machine.
So what are examples that are culture-dependent?
= Date.FromText( "30 Mrt 2022", [Format="dd MMM yyyy"] )
// Returns an error, PQ does not recognize Mrt as March
= Date.FromText( "30 Mrt 2022", [Format="dd MMM yyyy", Culture="nl-NL"] )
// Returns #date( 2022, 3, 30). Mrt = March in Dutch
= Date.FromText( "30 Maart 22", [Format="dd MMMM yy", Culture="nl-NL"] )
// Returns #date( 2022, 3, 30). Maart = March in Dutch
= Date.FromText( "30 juin 2022", [Format="dd MMM yyyy"] )
// Returns an error, PQ does not recognize juin as June
= Date.FromText( "30 juin 2022", [Format="dd MMM yyyy", Culture="fr-FR"] )
// Returns #date( 2022, 6, 30 ), juin = June in French
Conclusion
And there you go! You now have the skills to extract dates from text using Power Query’s Date.FromText function. Not only can you auto-extract dates, but you can also specify formats and even apply culture codes for more targeted results.
Are you interested in doing the reverse? Then check out this guide on formatting dates as text.
Tip
You can also use the techniques you’ve just learned to whip up a calendar in no time. You can use the methods described here to generate a calendar from scratch.
So, over to you: Have you put the Date.FromText function to work yet? Encountered any unique challenges or scenarios not covered here? Let me know in the comments and share your story. Or if you haven’t tried it yet, go ahead—give it a try and let me know how it went.
Happy querying!
Date Table Series
Foundations
Creating a (Dynamic) Date Table
Build a Calendar that supports Multiple Languages
Creating a 445 Calendar (incl 454 and 544)
Advanced Topics
Create ISO Week and Year Column
Return Nth Business Day From Date
Compute Working Days Between Dates
Creating Ordinal Numbers (1st, 2nd, 3rd etc.)
Create Default Slicer Value of Current Month
Hi Rick, Great blog!
I get an error message which drives me crazy.
I read a csv file into PQ, the file contains a header and 1 data record.
CSV data:
ts_log,otFtRoomSetpoint,otFtRoomTemperature (ts_log is a date-time object if type string)
03062024 00:00, 21.5,21.5
PQ:
Adding a custom column
= Table.AddColumn(#”Removed Other Columns”, “Date”, each Date.FromText( [ts_log], [Format=”ddMMyyyy hh:mm”]))
I constantly get this error message:
“Expression.Error: We cannot convert a value of type Record to type Text.
Details:
Value=
Format=ddMMyyyy hh:mm
Type=[Type]”
Adding Culture=”nl-NL” or using Format=”ddMMyyyy” did not solve the problem. Any idea what this error message may cause?
Thanks
Hey Fred. There are two issues here:
1. Your text value, besides containing the datetime value, also contains ” 21.5, 21.5″. The first step would be to make sure your text value only includes the datetime component. One way to do that is by using:
2. The second issue is that you used the Date.FromText function on a datetime value. You can instead use
Alternatively, you could also extract only the date value as text, and then use the Date.FromText function as follows:
Hope that helps!
Thanks Rick for your detailed answer.
But what I do not understand: why does the statement “= Table.AddColumn(#”Removed Other Columns”, “Date”, each Date.FromText( [ts_log], [Format=”ddMMyyyy hh:mm”]))” , especialy “Date.FromText( [ts_log]” , does not yield the text of column [ts_log] and put the result in a new column.
Hi Rick, in my date text column there is values, but if I add the custom column the values return null
Hi, what about English dates like 1st January 2024, 13th March 2021 or 3rd December 2022. Is there a way to convert those?
Hi Jok,
Ordinal Numbers are a little tricky. Power Query does not recognize them as such. One approach to achieve your desired outcome is:
Hi Rick, thank you so much for your sharing.
Hope you could help the below?
I have the date time as below
1/8/2023 3:19:00 AM
1/8/2023 4:22:02 AM
1/8/2023 4:22:03 AM
1/8/2023 9:28:06 AM
….
and want to convert into date.
I’ve tried the below formula but still getting error
thank you.
I also tried:
but still error
Parameter.Error: We couldn’t convert the text value to date using the specified format. The format includes a time component.
Details:
Format=%d/%m/yyyy hh:mm:ss AM/PM
hope you could help
Hi!
I know this can be quite confusing. The Date.FromText function does not support any time components as specified on this page: https://powerquery.how/time-totext/
You could take multiple approaches.
Either you can first turn the text into a datetime format with something like:
Alternatively, you could extract all characters before the first space in your string, and turn that string into a date.
When converting a date value to a text we can also insert arbitrary characters like:
Unfortunately, the reverse does not work due to a bug in Power Query. That bug was reported a couple of weeks ago. Hope that helps!
Hi Rick, thanks for the sharing. It is very useful
Hi Rick,
Hope you can help out here. I had a challenge at a job interview process where I was given a dataset with the date column in year & quarter and the column format as Time. I was unable to transform it into a proper Year-quarter.
Part od the dataset below.
Time
201501
201504
201507
201510
Hi Herve. That’s an interesting challenge.
You could try something like:
Thanks Rick for the quick response.
however the Date Format column only contains errors.
Hey Robert,
That’s strange. For me everything seems to work. Could it be that your computer has a different language that requires a different notation of “yyyyMMdd”.
If I had to guess your last name sounds german. Would for example “jjjjmmtt” work for you?
Hi Rick,
Thanks for this article. Pity you don’t have the samples for download.
With the Date.FromText function I am running into errors as soon as I specify the Format argument, after trying for a long time it looks to me like a bug or something regional settings related.
I have a simple sample file I can send so you see the effect …
Essentially I have date colums with Text like “20220726” and I try to convert it with … each Date.FromText([Date Text], [Format=”yyyyMMdd”]), to a date value, this combination is causing an error, where as … each Date.FromText([Date Text]), returns a valid date. Also other formats (you used) cause errors … as soon as i use the Format argument
Any ideas?
Servus
Robert
Hi Robert,
Can you give this code a try? You can copy-paste it into the advanced editor of a query and it should show you a working example.
Cheers,
Rick