Power Query DateTime.FromText Function Guide
In this article
The DateTime.FromText function in Power Query is used to convert a text string into a DateTime value. This is essential when importing data from CSVs, text files, or APIs where dates are stored as strings and need to be transformed into a format that Power BI or Excel can use for time-intelligence calculations, sorting, and filtering.
What You'll Need
- Microsoft Excel (with Power Query) or Power BI Desktop.
- A dataset containing a column of text-based date and time values.
- Basic familiarity with the Power Query Editor interface.
Step-by-Step
- Open the Power Query Editor: In Excel, go to the Data tab and select Get Data > Launch Power Query Editor. In Power BI, click Transform Data on the Home ribbon.
- Identify the Text Column: Locate the column containing the date/time strings you wish to convert. Ensure the data type is currently set to "Text" (indicated by the
ABCicon next to the column header). - Create a Custom Column: Navigate to the Add Column tab on the top ribbon and select Custom Column. This allows you to apply the function without overwriting your original source data, which is a best practice for auditing your transformations.
- Enter the Function: In the Custom Column formula box, type the following basic syntax:
= DateTime.FromText([ColumnName]). Replace[ColumnName]with the actual name of your text column by double-clicking it from the available columns list on the right. - Handle Culture/Regional Settings (Optional): If your text dates are in a format that doesn't match your system's local settings (for example, using US format MM/DD/YYYY while your system is UK DD/MM/YYYY), add a second argument to the function:
= DateTime.FromText([ColumnName], "en-US"). The culture code ensures Power Query interprets the month and day in the correct order. - Verify the Result: Click OK. A new column will appear. You will notice the values now look like dates and times. However, the data type may still be "Any".
- Set the Data Type: Click the icon to the left of the new column header and select DateTime from the dropdown menu. This explicitly tells Power Query to treat the result as a temporal value.
- Clean Up: Right-click the original text column and select Remove if it is no longer needed for your report.
Common Mistakes
One of the most frequent errors is attempting to use DateTime.FromText on a column that already has a Date or DateTime data type. This function is designed specifically for text. If the column is already typed as a date, the function will return an error because it expects a string input. Always check the column icon (ABC) before applying the function.
Another common mistake is ignoring the "Culture" argument. Many users assume Power Query will automatically detect if a date is in US or European format. If your source data is from a global region and your local machine is set to a different region, you will often see "Expression.Error: We couldn't parse the input string" or, worse, the dates will be swapped (e.g., January 5th becomes May 1st), leading to incorrect data analysis without an explicit error message.
Users also frequently forget to set the final data type. While the function converts the value, the column metadata often remains as "Any". This can cause issues when you try to use the column in a Date Hierarchy in Power BI or when performing date subtractions in Excel, as the system may not recognize the column as a true temporal object until the type is explicitly changed to DateTime.
Tips
For maximum efficiency, use the "Using Locale" feature in the UI if you are uncomfortable writing M code. You can right-click a column, select Change Type > Using Locale..., and choose DateTime and the specific region. This effectively performs the same action as DateTime.FromText with a culture argument but through a graphical interface.
If your text strings are inconsistent (some have times, some don't), DateTime.FromText might fail on the rows without time components. In these cases, it is safer to use Date.FromText for the date portion and then combine it with a time value, or use a conditional column to check for the presence of a colon (:) before deciding which function to apply.
When dealing with ISO 8601 formats (e.g., "2023-10-27T10:30:00Z"), DateTime.FromText is highly reliable. If you have very complex custom formats that the function cannot parse naturally, consider using DateTime.From or splitting the column by delimiter first to isolate the year, month, day, hour, and minute, then recombining them using #datetime(year, month, day, hour, minute, second).
Troubleshooting
If you encounter an "Expression.Error" stating that the input string could not be parsed, first check for leading or trailing spaces in your text. Use the Transform > Trim function on the text column before applying DateTime.FromText. Hidden spaces are a primary cause of parsing failures.
If the function returns the wrong date (swapping months and days), check your culture code. If you are using "en-US", it expects Month/Day/Year. If your data is Day/Month/Year, change the culture code to "en-GB" or "fr-FR". If you are not sure which code to use, refer to the standard BCP-47 language tags.
If you see "null" values in the resulting column, check your source data for empty strings or "N/A" values. DateTime.FromText cannot convert non-date text into a date. To fix this, use the "Replace Values" feature to change "N/A" or empty strings to a standard placeholder date or leave them as nulls and filter them out before the conversion step.
If the Power Query editor becomes slow after applying this to millions of rows, try to perform the type conversion as early as possible in your query steps. This allows Power Query to potentially "fold" the operation back to the SQL server (if using a database source), which is significantly faster than processing the conversion in your computer's local memory.
Frequently Asked Questions
Can I use this function for dates only without time?
While DateTime.FromText can handle strings that only contain dates, it will automatically append a time of 00:00:00. If you only need the date, it is more efficient to use the Date.FromText function.
What is the difference between DateTime.From and DateTime.FromText?
DateTime.FromText is specifically designed for strings and allows for the "culture" argument to handle regional formatting. DateTime.From is a more generic conversion function that attempts to convert any type (number, date, etc.) into a DateTime value but offers less control over regional formatting.
Does this function support time zones?
DateTime.FromText creates a datetime value, which does not include a time zone. If your text includes an offset (like +05:00), you should use DateTimeZone.FromText to preserve the time zone information as a datetimezone type.
Why is my date showing as an error after I change the type?
This usually happens because the text format does not match the locale settings of your Power BI/Excel installation. Ensure you are using the culture argument within the function or the "Using Locale" option in the menu to match the source data's origin.
Can I convert a Unix timestamp using this function?
No, Unix timestamps are numbers (seconds since 1970), not text strings in a date format. To convert a Unix timestamp, you must use a mathematical formula to add the seconds to a starting date of 1/1/1970.
Is it case-sensitive?
Yes, the M language used in Power Query is case-sensitive. You must write it exactly as DateTime.FromText. Writing datetime.fromtext or DateTime.Fromtext will result in a syntax error.