DateTime.From

DateTime.From

D

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

In this article, we will dive into the M code behind the DateTime.From function and explain how it works. We will explore the syntax, parameters, and examples of this function to give you a better understanding of its capabilities.

Syntax of the DateTime.From Function

The syntax of the DateTime.From function is straightforward and follows the format:


DateTime.From(dateTimeValue as any) as nullable datetime


The function takes a single parameter, which is the dateTimeValue, and returns a nullable datetime value. The dateTimeValue parameter can be any value that can be converted into a datetime value, including text, numbers, and other datetime values.

Parameters of the DateTime.From Function

The dateTimeValue parameter of the DateTime.From function is the only parameter required for this function. However, it is essential to understand the format of the dateTimeValue parameter to ensure that the function works correctly.

The dateTimeValue parameter must be in one of the following formats:

- A datetime value, such as #2022-01-01 00:00:00#

- A text representation of a date and time in a recognizable format, such as "01/01/2022 12:00:00 AM"

- A number representing the number of days since December 30, 1899, and the time as a decimal fraction of a day

The DateTime.From function will automatically recognize the format of the dateTimeValue parameter and convert it into a datetime value.

Examples of the DateTime.From Function

Let's take a look at some examples to understand how the DateTime.From function works. Suppose we have a table that contains a column of dates in various formats:

| Date |

|-------------------|

| 2022-01-01 00:00:00 |

| 01/02/2022 12:00:00 PM |

| 44797.5 |

We can use the DateTime.From function to convert these dates into a datetime value by creating a new column and using the DateTime.From function:


= Table.AddColumn(Source, “DateTime”, each DateTime.From([Date]))


This code will create a new column called "DateTime" and convert the values in the "Date" column into a datetime value using the DateTime.From function.

Suppose we want to extract the month and year from the datetime value and create new columns for each value. We can use the DateTime.Month and DateTime.Year functions with the DateTime.From function to achieve this:


= Table.AddColumn(Source, “DateTime”, each DateTime.From([Date])),

= Table.AddColumn(#”Added DateTime”, “Month”, each DateTime.Month([DateTime])),

= Table.AddColumn(#”Added Month”, “Year”, each DateTime.Year([DateTime]))


This code will create two new columns called "Month" and "Year" and extract the month and year from the datetime value using the DateTime.Month and DateTime.Year functions.

The DateTime.From function is a powerful tool in Power Query that allows users to convert any datetime value into a datetime value that can be manipulated and analyzed more efficiently. Understanding the syntax, parameters, and examples of this function is essential for data analysts who deal with time-based data. By mastering the DateTime.From function, you can unlock the full potential of Power Query and improve your data analysis skills.

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: