Smart Systems for Home

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

Power Query Date.DayOfYear: 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.DayOfYear returns the day number of the year for a given date, from 1 for January 1 to 365 (or 366 in a leap year). In Power Query you call it as Date.DayOfYear(#date(2025,3,15)), which returns 74. You can use it in a custom column, a calculated table, or a query step to sort, filter, or group dates by their position in the year.

What You'll Need

  • A version of Excel, Power BI Desktop, or Power Query Online that includes Power Query (Excel 2016 and later, Power BI Desktop, or the Power Query web experience).
  • A table or query that contains a column of Date or DateTime values, or the ability to create dates with #date().
  • Basic familiarity with the Power Query Editor: adding a custom column and writing M formulas.

Step-by-Step

  1. Open the Power Query Editor. In Excel, select your data and go to Data > Get & Transform Data > From Table/Range. In Power BI Desktop, use Home > Transform data. The exact menu labels can differ slightly by version and platform, but the Editor opens with your query loaded.
  2. Confirm your date column is typed as Date. Select the column header. If the icon shows a calendar, it is already Date. If it shows ABC or a number, use Transform > Data Type > Date. If the values fail to convert, check for text dates in a different locale format before continuing.
  3. Add a custom column. Go to Add Column > Custom Column. In the New column name box, type a name such as DayOfYear.
  4. Write the formula. In the Custom Column formula box, enter a formula that references your date column, for example: Date.DayOfYear([OrderDate]) Replace OrderDate with the exact name of your date column, including any spaces inside square brackets. The function name is case-sensitive in M: Date.DayOfYear, not date.dayofyear.
  5. Click OK. Power Query adds the new column. Each row now shows a whole number between 1 and 366.
  6. Set the data type of the new column. Select the new column and use Transform > Data Type > Whole Number. Without this, the column may stay as Any and behave unpredictably in later steps.
  7. Use the result. You can sort ascending to put dates in calendar order, filter to a range such as 1 to 90 for the first quarter, or group by the column to count events per day of the year. To keep the value, close and load the query with Home > Close & Load.

For a quick test without a table, open a blank query (Data > Get Data > From Other Sources > Blank Query), open the Advanced Editor, and replace the query with Date.DayOfYear(#date(2025,12,31)). The result should be 365. Change the date to 2024-12-31 and it returns 366, because 2024 is a leap year.

Common Mistakes

  • Using a DateTime column without converting. Date.DayOfYear expects a date value. If your column is DateTime, the function still works in many cases, but the safest approach is to change the type to Date first with Transform > Data Type > Date.
  • Mixing up 1-based and 0-based logic. January 1 returns 1, not 0. If you need a zero-based offset, subtract 1 in the formula.
  • Forgetting the square brackets. Column references in M use [ColumnName], not a bare name. A missing bracket produces a syntax error.
  • Assuming a fixed year length. The result depends on the year in each row. A column that mixes years will contain both 365 and 366 values.
  • Leaving the column as Any. The step succeeds but later operations such as sorting or merging may treat the values as text.

Tips

  • Combine with Date.Year to build a unique key per day across multiple years, for example .
  • To get the day of the year for today, use Date.DayOfYear(DateTime.Date(DateTime.LocalNow())) in a query that refreshes.
  • If you need the reverse, the day number back to a date, use Date.AddDays(#date(year,1,1), dayNumber - 1).
  • Name the custom column clearly, such as DayOfYear, so it is obvious in the Applied Steps and in the data model.

Troubleshooting

The formula returns an error. Check that the column name matches exactly, including case and spaces. Use the column picker in the Custom Column dialog to insert the reference instead of typing it.

All values show as Error. The column probably contains text or nulls. Filter out nulls or use try Date.DayOfYear([Column]) otherwise null to handle bad rows.

The result is text, not a number. Change the column type to Whole Number after adding it. If the type option is greyed out, the query may be connected to a source that restricts type changes; apply the change in a later step.

The numbers look wrong for some rows. Check the date format in the source. A date stored as text in day/month order can be read as month/day, shifting the day number. Fix the source format or use a locale-aware conversion step before applying the function.

The function name is not recognised. Ensure you are using a version of Power Query that supports it; it has been part of the standard library for many years. Also check for a typo such as Date.DayofYear or Date.Day_Of_Year.

Frequently Asked Questions

Does Date.DayOfYear work in Excel Power Query and Power BI?

Yes. It is part of the standard M library and works in Excel, Power BI Desktop, Power BI Service dataflows, and Power Query Online.

What does Date.DayOfYear return for a leap day?

February 29 returns 60 in a leap year, because January has 31 days and February 29 is the 60th day. In non-leap years that date does not exist.

Can I use Date.DayOfYear on a column that contains nulls?

It returns an error for null. Wrap it in try ... otherwise null to keep nulls as null and avoid breaking the query.

Is the result always between 1 and 366?

Yes, for any valid date. The maximum is 366 for December 31 in a leap year, and 365 otherwise.

How do I get the day of the year from a text date?

Convert the text to a date first with Transform > Data Type > Date, or use Date.FromText with the correct format, then apply Date.DayOfYear to the converted column.