Date.AddDays

D

T

The M Code Behind the Power Query M function Date.AddDays

Let’s take a closer look at the M code behind this function and how it works.

The Syntax of Date.AddDays

The syntax for Date.AddDays is as follows:


Date.AddDays(startDate as date, days as number) as date


`startDate` is the starting date to which you want to add days.

`days` is the number of days you want to add to the starting date.

The function returns a new date that is `days` days after the `startDate`.

An Example of Date.AddDays

Suppose you have a table named `SalesData` that contains a column named `OrderDate` with dates in the format of `yyyy-mm-dd`. You want to add 90 days to each date in the `OrderDate` column and create a new column named `NewOrderDate`.

Here's the M code to achieve this:


let

Source = SalesData,

#”Added Custom” = Table.AddColumn(Source, “NewOrderDate”, each Date.AddDays([OrderDate], 90)),

#”Changed Type” = Table.TransformColumnTypes(#”Added Custom”,{{“NewOrderDate”, type date}})

in

#”Changed Type”


The above code adds a custom column named `NewOrderDate` to the `SalesData` table. The `each` keyword is used to apply the Date.AddDays function to each value in the `OrderDate` column. The resulting table has a new column named `NewOrderDate` with dates that are 90 days after the original `OrderDate` values.

Further Examples of Date.AddDays

Here are some further examples of using Date.AddDays:

1. Adding 7 days to a given date:


Date.AddDays(#date(2022,1,1), 7)


This returns the date `2022-01-08`.

2. Adding -30 days to a given date:


Date.AddDays(#date(2022,1,1), -30)


This returns the date `2021-12-02`.

The Date.AddDays function in Power Query M is a useful tool for performing date calculations. By understanding the M code syntax behind this function, you can leverage its power to manipulate dates in your 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)