DateTime.FromText

DateTime.FromText

D

The M Code Behind the Power Query M function DateTime.FromText

What is the M language?

M is the language used in Power Query to write data transformation scripts. It is a functional programming language that is designed to be easy to learn and use. M is used to perform various data transformation tasks, such as filtering, sorting, and grouping. In addition, M has a wide range of built-in functions that make data transformation tasks easier.

Understanding DateTime.FromText

DateTime.FromText is a function used to convert text to a datetime value. The function syntax is:


DateTime.FromText(text as text, optional culture as nullable text)


The function takes two arguments:

- text: The text to be converted to a datetime value.

- culture: An optional argument that specifies the culture to be used for the conversion.

By default, the function uses the culture of the current system. However, you can also specify a custom culture to be used for the conversion.

The M code behind DateTime.FromText

To understand how DateTime.FromText works, let's examine the M code behind the function. The M code for DateTime.FromText is:


let

Source = DateTimeZone.FromText(text, culture),

Result = DateTime.LocalNow() + Duration.From(Source – DateTimeZone.LocalNow())

in

Result


The M code consists of two parts:

1. The first part uses the DateTimeZone.FromText function to convert the text value to a datetime value. The function takes two arguments: the text to be converted and the culture to be used for the conversion. The function returns a datetime value in the UTC time zone.

2. The second part uses the DateTime.LocalNow and Duration.From functions to convert the UTC datetime value to the local datetime value. The function adds the duration between the UTC and local time zones to the UTC datetime value to get the local datetime value.

Examples of using DateTime.FromText

Here are some examples of using DateTime.FromText:

Example 1


DateTime.FromText(“1/1/2021”)


In this example, the function converts the text value "1/1/2021" to a datetime value using the default culture of the current system.

Example 2


DateTime.FromText(“01/01/2021”, “en-US”)


In this example, the function converts the text value "01/01/2021" to a datetime value using the "en-US" culture.

Example 3


DateTime.FromText(“20210101”, “en-GB”)


In this example, the function converts the text value "20210101" to a datetime value using the "en-GB" culture.

DateTime.FromText is a powerful function in Power Query that allows you to convert text to a datetime value. By understanding the M code behind the function, you can gain a deeper understanding of how it works and how to use it effectively.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

Subject

Your Name (required)

Company/Organisation

Email (required)

Telephone

Training Course(s)

Your Message

Upload Example Document(s) (Zip multiple files)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: