Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Date.AddYears: Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Date.AddYears adds a whole number of years to a date, datetime, or datetimezone value in Power Query. The syntax is Date.AddYears(date as nullable date, numberOfYears as number) as nullable date. It returns the same date shifted by the given number of years, keeping the month and day. If the original date is February 29 and the target year is not a leap year, the result becomes February 28. Use it in a custom column, a filter step, or a calculated table when you need to project dates forward or backward by whole years.

What You'll Need

  • Power BI Desktop, Excel with Power Query (Get & Transform), or another host that includes Power Query.
  • A table with a column that is already typed as Date, DateTime, or DateTimeZone. If the column is Text, convert it first with Date.FromText or by changing the data type.
  • Basic familiarity with the Power Query Editor: the ribbon, the Applied Steps pane, and the formula bar.

Step-by-Step

  1. Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the Power Query Editor.
  2. Confirm the date column's type. Select the column header, check the icon on the left of the header, and use Home > Data Type to set it to Date if needed. A Text column will make Date.AddYears return an error.
  3. Add a custom column. Go to Add Column > Custom Column. Give it a name such as RenewalDate.
  4. Enter the formula in the Custom Column dialog. For a column named StartDate and a fixed two-year shift, type Date.AddYears([StartDate], 2). Replace 2 with any positive or negative whole number. Use a column reference such as [TermYears] if the number of years varies per row.
  5. Click OK. Power Query adds the new column and an Applied Step. Check the new column's type; set it to Date using Home > Data Type if it shows as Any.
  6. Handle nulls if your source column allows them. Date.AddYears returns null when the input is null, which is usually the behaviour you want. If you need a fallback, wrap the expression: if [StartDate] = null then null else Date.AddYears([StartDate], 2).
  7. Use it in a filter or conditional step if you do not need a new column. For example, to keep rows expiring within the next year, add a filter with Date.AddYears(DateTime.LocalNow(), 1) as the upper bound.
  8. Apply the changes with Home > Close & Load (Excel) or Home > Close & Apply (Power BI).

Menu labels can differ slightly by version, host application, and region. The same function is available in the M formula language directly if you prefer editing the query in the Advanced Editor.

Common Mistakes

  • Passing a text column. Date.AddYears expects a date value. If the column is Text, you get an error like We cannot convert the value to type Date. Convert the column first.
  • Using a decimal for the year count. The second argument should be a whole number. A value like 1.5 is not meaningful for calendar years; round it first with Number.Round.
  • Forgetting February 29. Adding a year to a leap-day date in a non-leap year returns February 28. If your business rule requires March 1, handle that case explicitly.
  • Expecting month or day changes. Date.AddYears only changes the year. To shift by months or days, use Date.AddMonths or Date.AddDays.
  • Assuming time zone conversion. For DateTimeZone values, the function shifts the calendar date but does not convert between zones. Use DateTimeZone functions if zone conversion is needed.

Tips

  • Use negative numbers to go back in time, for example Date.AddYears([StartDate], -1) for the same date last year.
  • Combine with Duration or Date.From to build age or tenure calculations: Date.AddYears([BirthDate], 18) gives the eighteenth birthday.
  • Keep the original column and add a new one rather than overwriting. It makes auditing and troubleshooting easier.
  • For a dynamic reference date, use DateTime.LocalNow() or DateTime.FixedLocalNow(). The fixed version keeps the value stable during a single refresh.
  • Name custom columns clearly, such as ExpiryDate_Plus2Y, so the intent is obvious in reports.

Troubleshooting

If the new column shows errors, click the error cell to see the message. The most common cause is a wrong data type in the source column. Change the type to Date and refresh the step.

If the formula editor rejects your expression, check brackets and column names. Column names with spaces must be written in square brackets exactly as they appear, for example [Start Date]. Function names are case-sensitive in M: use Date.AddYears, not date.addyears.

If the result is off by a day around February 29, that is expected behaviour. Decide whether February 28 or March 1 is correct for your rule and adjust with an if expression.

If the column appears as Any or Text after adding it, set the data type explicitly. Power Query sometimes cannot infer the type from a custom expression.

If refreshing is slow on large tables, avoid calling DateTime.LocalNow() inside a row-level custom column when possible. Compute the reference date once in a separate step and reference that value.

Frequently Asked Questions

What is the difference between Date.AddYears and Date.AddMonths?

Date.AddYears adds whole calendar years and keeps the month and day where possible. Date.AddMonths adds a number of months, which can change the year and month. Use AddMonths for 12-month shifts only if you specifically want month-based behaviour.

Can I add years to a DateTime column?

Yes. Date.AddYears works with date, datetime, and datetimezone values. The time portion is preserved for datetime values.

Why does February 29 become February 28?

Because the target year has no February 29. Power Query returns the last valid day of February. If you need March 1 instead, add a conditional expression.

Does Date.AddYears work with a column of year counts?

Yes. Replace the fixed number with a column reference, such as Date.AddYears([StartDate], [TermYears]). Ensure the year column is a whole number type.

Can I use Date.AddYears in a filter step?

Yes. You can filter rows where a date is less than or equal to Date.AddYears(DateTime.LocalNow(), 1), for example to keep records expiring within a year.

What happens if the date column contains nulls?

The function returns null for null input. If you need a default date, wrap the expression in an if statement.