Date.AddWeeks

D

T

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

The `Date.AddWeeks` function is a part of the M language, which is used in Power Query. The M language is a functional programming language that is used to build custom functions and expressions in Power Query.

In this article, we will explore the M code behind the `Date.AddWeeks` function and how it can be used to manipulate dates in Power Query.

Syntax of the Date.AddWeeks Function

The `Date.AddWeeks` function takes two arguments: the date to which you want to add or subtract weeks, and the number of weeks to add or subtract. The syntax of the `Date.AddWeeks` function is as follows:


Date.AddWeeks(date as any, weeks as number) as any


The first argument, `date`, can be any valid date format, including a date, datetime, or datetimezone. The second argument, `weeks`, is the number of weeks to add or subtract from the `date` argument. The function returns the resulting date after adding or subtracting the specified number of weeks.

Examples of the Date.AddWeeks Function

Let us consider some examples to understand how the `Date.AddWeeks` function works.

Example 1:

Suppose we have a date, 01/01/2022, and we want to add 2 weeks to it. The M code to achieve this is:


Date.AddWeeks(#date(2022,1,1), 2)


The resulting date will be 15/01/2022.

Example 2:

Suppose we have a datetime, 01/01/2022 12:00:00 AM, and we want to subtract 3 weeks from it. The M code to achieve this is:


Date.AddWeeks(#datetime(2022,1,1,0,0,0), -3)


The resulting datetime will be 11/12/2021 12:00:00 AM.

Understanding the M Code Behind the Date.AddWeeks Function

The M code behind the `Date.AddWeeks` function is:


(date as any, weeks as number) =>

date + #duration(0, 0, weeks 7, 0)


The function takes two arguments, `date` and `weeks`, and returns the resulting date after adding or subtracting the specified number of weeks.

The `#duration` function is used to add or subtract the specified number of weeks to the given date. The first argument of the `#duration` function is the number of days, the second argument is the number of hours, the third argument is the number of minutes, and the fourth argument is the number of seconds. In the case of the `Date.AddWeeks` function, we only need to add or subtract the number of weeks, which is why we set the third argument to `weeks 7`.

The `Date.AddWeeks` function is a powerful tool that can be used to manipulate dates in Power Query. In this article, we explored the M code behind the function and how it can be used to add or subtract a specified number of weeks to a given date. By understanding the M code behind the function, you can customize and create your own functions in Power Query to manipulate dates and transform your data as required.

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)