Smart Systems for Home

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

Power Query Date.Year: Step-by-Step How-To

· 6 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

The Date.Year function in Power Query returns the year portion of a date value as a whole number, so a date like 15 March 2024 becomes simply 2024. You use it inside a custom column formula, a calculated column, or a query step, and it works in Excel's Power Query Editor and in Power BI Desktop. Before you start, make sure your column is actually typed as Date (or Date/Time), because Date.Year fails on text values that merely look like dates.

What You'll Need

  • Excel (Power Query is built in from Excel 2016 onward) or Power BI Desktop, or another host that includes Power Query.
  • A table or data source already loaded into the Power Query Editor.
  • A column containing real date values, or a way to convert your text column into dates first.
  • Basic familiarity with the Power Query Editor window: the Queries pane on the left, the data preview in the middle, and the Applied Steps list on the right.

Menu and button labels can differ slightly between Excel versions, Power BI versions and regions, so if a label does not match exactly, look for the closest equivalent.

Step-by-Step

  1. Load your data into the Power Query Editor. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open the editor. Either way, you should end up in the Power Query Editor with your data previewed.
  2. Check the column type. Find the date column in the preview. The icon to the left of the column header shows its type. If it shows ABC (text) or a number icon, click that icon and choose Date (or Date/Time if the column includes times). If the values do not convert cleanly, Power Query will show errors in the column, which you must fix before going further.
  3. Add a custom column. Go to Add Column > Custom Column. Do not use the Transform tab's column tools for this, because Add Column keeps your original date column intact.
  4. Enter the formula. In the Custom Column dialog, name the new column something like Year, then type this formula in the box: Date.Year([OrderDate]) — replacing OrderDate with the exact name of your date column. Column names are case-sensitive in Power Query, and they must be wrapped in square brackets. If the name contains spaces, keep the brackets: Date.Year([Order Date]).
  5. Confirm there are no syntax errors. Power Query shows a message under the formula box if something is wrong. A missing closing parenthesis or a misspelled function name will be flagged here. Fix it before clicking OK.
  6. Click OK. A new step appears in the Applied Steps list on the right, and the preview shows a new column containing four-digit years.
  7. Set the new column's type explicitly. Click the type icon on the new Year column header and choose Whole Number. Power Query often infers this correctly, but setting it yourself prevents surprises when the query refreshes.
  8. Rename and reorder if needed. Double-click the new column header to rename it. To move it next to the original date column, right-click the header and use Move > Left or To Beginning.
  9. Apply and close. In Excel, use Home > Close & Load to send the result to a worksheet. In Power BI, use Home > Close & Apply. The query now refreshes with the year column included.

If you prefer not to write a formula at all, you can also use Add Column > Date > Year > Year. That menu command produces the same result and writes the Date.Year step for you. The custom column route is worth knowing because it lets you combine the function with other logic, such as Date.Year([OrderDate]) - 1 for a prior-year comparison.

Common Mistakes

  • Running Date.Year on a text column. This is the most frequent error. The function needs a date type. Convert the column first with Transform > Data Type > Date, or wrap the value in Date.FromText if the format is unusual.
  • Forgetting the square brackets. Writing Date.Year(OrderDate) instead of Date.Year([OrderDate]) produces an error because Power Query reads the bare name as an unknown identifier.
  • Using the wrong column name. Renaming the source column later breaks the formula. If you rename a column upstream, update the custom column step or re-point it.
  • Expecting Date.Year to work on a null. Null dates return null years, which is correct behaviour but can surprise you in totals. Filter or replace nulls beforehand if that matters.
  • Confusing Date.Year with Date.YearOfWeek or Duration functions. Date.Year returns the calendar year of a single date value. It does not take a list, a table or a duration.
  • Editing the wrong step. If you click an earlier Applied Step and add a column there, later steps may reference columns that no longer exist. Always work from the last step unless you intend to rebuild the query.

Tips

  • You can use Date.Year in a group-by operation: Group By the new Year column to produce yearly totals without any pivot table.
  • To extract other parts, the same family includes Date.Month, Date.Day, Date.QuarterOfYear and Date.DayOfWeek. They follow identical syntax.
  • For a two-digit or text year label, wrap the result: Text.From(Date.Year([OrderDate])).
  • Use the formula bar to inspect the generated M code. Seeing the step written out helps you learn the syntax and spot mistakes quickly.
  • If you need the year from a Date/Time column, Date.Year works directly — no need to split the time off first.
  • Keep a habit of setting data types explicitly on every new column. It makes refreshes more predictable and avoids type-mismatch errors downstream.

Troubleshooting

If the custom column dialog refuses to accept the formula, check three things in order: the column name matches exactly (including spaces and capitalisation), the column is typed as Date or Date/Time, and every opening parenthesis has a closing one. The error message under the formula box usually points at the exact character.

If the column is created but shows Error in every row, the underlying values are probably not valid dates. Select the original date column, check the type icon, and look at the first few rows. Text such as "2024-13-45" or regional formats like day/month/year versus month/day/year can fail conversion. Use Transform > Data Type > Using Locale to specify the correct regional format, then retry.

If the year values look right in the preview but wrong after loading to the worksheet or report, the column type was probably left as Any or Text. Reopen the query, set the type to Whole Number, and refresh.

If a refresh later breaks the query, the source column may have been renamed or removed. Open the Applied Steps, click the custom column step, and inspect the formula in the formula bar. Update the bracketed name to match the current column.

If nothing works, delete the custom column step and rebuild it from scratch. It is usually faster than hunting for a single typo in a long formula.

Frequently Asked Questions

Does Date.Year work in Excel as well as Power BI?

Yes. Both Excel's Power Query Editor and Power BI Desktop use the same M engine, so the function and syntax are identical.

What does Date.Year return for a null date?

It returns null. If you need a placeholder, wrap it: if [OrderDate] = null then 0 else Date.Year([OrderDate]).

Can I get the year from a text column without converting it?

You can, using Date.Year(Date.FromText([Column])), but converting the column type first is cleaner and easier to debug.

Why does my formula say the column cannot be found?

The bracketed name must match the column header exactly, including spaces and case. Check the header in the preview and copy it precisely.

Is there a menu button that does the same thing?

Yes. Add Column > Date > Year > Year adds a year column using the same function, which is handy if you do not want to type a formula.

Can I use Date.Year inside a Group By step?

Yes. Add the year column first, then use Home > Group By and choose the new column as the grouping key.