Duration.TotalDays

D

T

The M Code Behind the Power Query M function Duration.TotalDays

Behind this function lies the M code, a programming language used by Power Query to transform and manipulate data. In this article, we will explore the M code behind the Duration.TotalDays function and how it works.

Understanding the Duration.TotalDays Function

The Duration.TotalDays function is used to convert a duration value into the number of days. It takes one argument, which is the duration value to be converted.

For example, if we have a duration value of 2 days and 6 hours, we can use the Duration.TotalDays function to convert it into 2.25 days. The M code behind this function performs the necessary calculation to convert the duration value into the equivalent number of days.

The M Code Behind the Duration.TotalDays Function

The M code behind the Duration.TotalDays function is a simple formula that takes the duration value and divides it by the total number of seconds in a day. This calculation gives us the number of days in decimal form.

The M code for the Duration.TotalDays function looks like this:


(duration as duration) =>

duration / #duration(1,0,0,0)


Let's break down this code to understand what it does.

The first line defines the function and its input, which is a duration value. The second line performs the calculation to convert the duration value into the number of days.

The #duration(1,0,0,0) is a duration value that represents one day. It has a format of #duration(days, hours, minutes, seconds). In this case, we have set the days value to 1 and the other values to 0 to represent one full day.

The M code divides the duration value by the one-day duration value to get the number of days. This calculation is performed for each row in the data set, allowing us to convert multiple duration values into their equivalent number of days.

Using the Duration.TotalDays Function

To use the Duration.TotalDays function in Power Query, we need to have a duration value to convert. This value can be obtained from a column containing duration values or from a custom formula.

To create a custom formula using the Duration.TotalDays function, we can follow these steps:

1. Select a cell where the formula will be used.

2. Click on the "Add Column" tab in the Power Query editor.

3. Choose "Custom Column" from the drop-down menu.

4. Enter a name for the new column.

5. Enter the formula using the Duration.TotalDays function, and select the column containing the duration value to be converted.

For example, if we have a column named "Duration" containing duration values, we can create a new column named "Days" with the equivalent number of days using the following formula:


= Duration.TotalDays([Duration])


This formula will apply the Duration.TotalDays function to each value in the "Duration" column, converting it into the equivalent number of days and storing it in the new "Days" column.

The Duration.TotalDays function in Power Query is a useful tool for converting duration values into the equivalent number of days. Behind this function lies the M code, which performs the necessary calculation to convert the duration value.

By understanding the M code behind this function, we can gain a deeper understanding of how Power Query works and how we can use it to manipulate and clean our data.

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)