Smart Systems for Home

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

Excel Power Query: Subtract Days from a Date

· 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

To subtract days from a date in Excel Power Query, add a custom column with the formula Date.AddDays([YourDateColumn], -N), where N is the number of days to subtract. For example, Date.AddDays([OrderDate], -7) returns the date one week earlier. This works in both Excel and Power BI because Power Query uses the same M language. You can also use the Date.AddDays function directly in the formula bar or the Add Column tab without writing code. The result is a proper date value that you can format, filter, or use in further calculations. No add-ins are required—Power Query is built into Excel 2016 and later (including Microsoft 365) under the Data tab.

What You'll Need

  • Excel 2016 or later (Power Query is included; in Excel 2010/2013 it was a separate add-in).
  • A table or range loaded into Power Query (Data > From Table/Range).
  • A column containing date values (not text that looks like a date).
  • Basic familiarity with the Power Query Editor window.

Step-by-Step

  1. Load your data into Power Query. Select your table, then go to Data > From Table/Range. If prompted, confirm the range and click OK. The Power Query Editor opens.
  2. Verify the date column type. In the Editor, find your date column. The column header should show a calendar icon (type Date). If it shows ABC (text) or a number, click the icon and choose Date. This step is critical—Date.AddDays only works on true date values.
  3. Add a custom column. Go to the Add Column tab and click Custom Column. In the dialog, give the new column a name (e.g., "Date Minus 7"). In the formula box, enter Date.AddDays([YourDateColumn], -7) replacing [YourDateColumn] with your actual column name (case-sensitive, in square brackets). Click OK.
  4. Check the result. A new column appears with dates shifted back by 7 days. If you see errors, the original column likely contains nulls or non-date values; see Troubleshooting.
  5. Set the new column's data type. Click the icon in the new column header and choose Date. Power Query may auto-detect, but setting it explicitly avoids issues downstream.
  6. Rename the column if needed. Double-click the header to rename it to something meaningful like "Adjusted Date".
  7. Load the result. Go to Home > Close & Load (or Close & Load To… to choose a location). The new column appears in your worksheet or data model.

Alternative without a custom column: You can also use the Date.AddDays function in the formula bar after selecting an existing step, but the custom column method is the most straightforward and auditable.

Common Mistakes

  • Using a positive number instead of negative.
  • Forgetting the minus sign. The function name is Date.AddDays; it does not have a separate Subtract function. The minus sign is the only way to subtract.
  • Referencing a column name incorrectly. Column names must be in square brackets and match exactly, including spaces. If your column is "Order Date", use [Order Date], not [OrderDate].
  • Leaving the column as text. If the original column is text, Date.AddDays returns an error. Convert to Date first.
  • Expecting the original column to change. Custom columns add a new column; the original remains unchanged. That is usually desirable, but be aware.
  • Using a hard-coded number when you need a dynamic offset. If the number of days to subtract comes from another column, use .

Tips

  • You can subtract days from today's date using Date.AddDays(DateTime.LocalNow(), -30) but note that DateTime.LocalNow() returns a datetime; wrap with Date.From to get a pure date: Date.AddDays(Date.From(DateTime.LocalNow()), -30).
  • To subtract months or years, use Date.AddMonths or Date.AddYears with a negative number.
  • If you need to subtract working days (excluding weekends), Power Query does not have a built-in function; you would need a custom function or a date table. For simple calendar days, Date.AddDays is correct.
  • Use the Advanced Editor to review the generated M code; it helps you learn and spot errors.
  • Name your steps descriptively in the Applied Steps pane so you can trace changes.

Troubleshooting

  • Error: "We cannot apply operator - to types Date and Number." This usually means the column is not typed as Date. Change the type to Date and refresh.
  • Error: "The name 'Date.AddDays' wasn't recognized." Check spelling and capitalization. M is case-sensitive: Date.AddDays, not date.adddays.
  • All results are errors. The source column likely contains nulls or text. Filter out nulls or replace errors before applying the custom column, or use to handle errors gracefully.
  • The new column shows numbers instead of dates. Set the data type to Date explicitly; Power Query may have inferred a number.
  • Formula bar shows a different formula than expected. Ensure you are editing the correct step; click the step in Applied Steps that corresponds to your custom column.
  • Changes don't appear in the worksheet. Click Refresh All on the Data tab; Power Query output does not auto-refresh unless you set the query properties.

Frequently Asked Questions

Can I subtract days from a date in Power Query without writing a formula?

Not directly via a button, but you can use the Date.AddDays function through the Custom Column dialog, which is a guided interface. There is no dedicated "subtract days" button in the ribbon.

What is the difference between Date.AddDays and DateTime.AddDays?

Date.AddDays works on date values (no time component) and returns a date. DateTime.AddDays works on datetime values and returns a datetime. Use the one that matches your column type.

Can I subtract days from a date column that contains nulls?

Yes, but Date.AddDays returns null if the input is null, which is usually fine. If you want a default, wrap with try ... otherwise.

How do I subtract a variable number of days stored in another column?

Use . Ensure DaysColumn is a number type.

Does subtracting days account for leap years and month lengths?

Yes, Date.AddDays performs calendar arithmetic correctly, so subtracting 1 from March 1 gives February 28 or 29 depending on the year.

Can I use this in Power BI as well?

Yes, Power Query is identical in Power BI Desktop; the same steps and formulas apply.