DateTime.FromText

DateTime

Converts a text representation of a datetime to a datetime value.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

DateTime.FromText(text as nullable text, optional options as any) as nullable datetime

Parameters

NameTypeRequiredDescription
texttextYesA text string representing a datetime, such as "2024-03-15 14:30:00" or "March 15, 2024 2:30 PM".
optionsanyNoA record or culture text string. Supported record fields: Format (custom format string) and Culture (e.g., "en-US").

Return Value

datetimeA datetime value parsed from the given text, or null if the input is null.

Remarks

DateTime.FromText parses a text value into a datetime. Without options, it attempts common ISO 8601 datetime formats such as "2024-03-15T14:30:00" or "2024-03-15 14:30:00". Use the options record to supply a Format pattern or a Culture for locale-specific strings such as "03/15/2024 02:30:00 PM". Format strings follow .NET custom datetime format conventions.

Always specify both Format and Culture when parsing locale-specific strings. Without Culture, parsing behavior depends on the host machine's locale, which can cause different results in Power BI Desktop vs. Power BI Service. A culture of "en-US" is recommended for most M development since it produces predictable results across environments.

If the text is null, the function returns null. If parsing fails, the function raises an error. Use try DateTime.FromText(...) otherwise null to handle mixed or malformed data gracefully without crashing the query.

Examples

Example 1: Parse ISO datetime text strings

#table(
    type table [TextValue = text, Parsed = datetime],
    {
        {"2024-01-15T09:30:00", DateTime.FromText("2024-01-15T09:30:00")},
        {"2024-02-01T16:20:00", DateTime.FromText("2024-02-01T16:20:00")},
        {"2024-03-05T08:00:00", DateTime.FromText("2024-03-05T08:00:00")}
    }
)
Result
TextValue
Parsed
12024-01-15T09:30:001/15/2024 9:30:00 AM
22024-02-01T16:20:002/1/2024 4:20:00 PM
32024-03-05T08:00:003/5/2024 8:00:00 AM

Example 2: Parse a locale-specific datetime string

#table(
    type table [TextValue = text, Parsed = datetime],
    {{"03/15/2024 02:30:00 PM", DateTime.FromText("03/15/2024 02:30:00 PM", [Format="MM/dd/yyyy hh:mm:ss tt", Culture="en-US"])}}
)
Result
TextValue
Parsed
103/15/2024 02:30:00 PM3/15/2024 2:30:00 PM

Example 3: Safe parsing with try...otherwise for mixed data

#table(
    type table [TextValue = text, Parsed = datetime],
    {
        {"2024-01-15T09:30:00", try DateTime.FromText("2024-01-15T09:30:00") otherwise null},
        {"N/A",                 try DateTime.FromText("N/A") otherwise null},
        {"2024-03-12T11:00:00", try DateTime.FromText("2024-03-12T11:00:00") otherwise null}
    }
)
Result
TextValue
Parsed
12024-01-15T09:30:001/15/2024 9:30:00 AM
2N/Anull
32024-03-12T11:00:003/12/2024 11:00:00 AM

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks