Power Query Date.EndOfWeek: Step-by-Step Guide
In this article
Power Query's Date.EndOfWeek function returns the date of the last day of the week for any given date. It takes a date, datetime, or datetimezone value and an optional start-of-week parameter (default is Sunday) and returns the date of the week's final day. For example, Date.EndOfWeek(#date(2025, 1, 15)) returns January 18, 2025, because with Sunday as the first day, the week ends on Saturday. This is useful for grouping data by week, building weekly reports, or aligning dates to week endings.
What You'll Need
- Power Query Editor open in Excel, Power BI Desktop, or another tool that hosts Power Query.
- A table or query with a column containing date or datetime values.
- Basic familiarity with adding custom columns and writing M formulas.
Step-by-Step
- 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. Ensure your date column is typed as Date or DateTime.
- Add a custom column. Go to the Add Column tab and click Custom Column. Give the new column a name, such as "Week End".
- Write the formula. In the formula box, enter
Date.EndOfWeek([YourDateColumn]). Replace[YourDateColumn]with the actual name of your date column. The default start of week is Sunday, so the function returns the Saturday date for each row. - Optionally specify a different start day. If your week starts on Monday, use
Date.EndOfWeek([YourDateColumn], Day.Monday). The second argument accepts any Day constant (Day.Sunday, Day.Monday, etc.). The function returns the last day of the week based on that start day. For example, with Monday as start, the week ends on Sunday. - Click OK. The new column appears with the end-of-week dates. Check a few rows against a calendar to confirm the logic matches your expectation.
- Change the data type if needed. By default, the new column may be typed as Date. If you need it as DateTime, use the Transform tab and change the type.
- Close and load. When satisfied, go to Home > Close & Load (Excel) or Home > Close & Apply (Power BI) to bring the results into your worksheet or data model.
Common Mistakes
- Forgetting the optional start-of-week argument. Many users expect Monday as the first day, but the default is Sunday. Always specify
Day.Mondayif your business week starts on Monday. - Using a text column instead of a date column. Date.EndOfWeek requires a date, datetime, or datetimezone value. If your column is text, change its type first.
- Misunderstanding the return value. The function returns a date, not a datetime. If you need a timestamp, combine it with a time value or use DateTime functions.
- Assuming the function handles nulls. If the input date is null, the function returns null. Handle nulls with if needed.
- Not accounting for locale differences. The Day constants are not affected by locale, but your interpretation of "end of week" might be. Double-check with sample dates.
Tips
- Use
Date.EndOfWeekin combination withDate.StartOfWeekto create a week range for filtering or grouping. - To get the end of the current week dynamically, use
Date.EndOfWeek(DateTime.LocalNow()). - If you need the week number, consider
Date.WeekOfYearwith the same start day for consistency. - For fiscal weeks, you may need a custom calculation; Date.EndOfWeek only handles calendar weeks.
- Test with a few dates that cross month and year boundaries to ensure the function behaves as expected.
Troubleshooting
If the custom column shows errors, check the following:
- Formula syntax: Ensure you have matching parentheses and the column name is correct. The column name must be in square brackets and match exactly, including spaces.
- Data type: Verify the input column is Date, DateTime, or DateTimeZone. Use the Transform tab to change the type if needed.
- Null values: If errors appear only on some rows, those rows may contain nulls or invalid dates. Filter or replace them.
- Function name: M is case-sensitive. Use
Date.EndOfWeekexactly, notdate.endofweek. - Start day constant: If you specify a start day, use the correct constant like
Day.Monday. Using a number (e.g., 1) will cause an error.
If the function works but results seem off, compare a few dates manually. For instance, Date.EndOfWeek(#date(2025,1,1)) returns January 4, 2025 (Saturday) because the week containing January 1 starts on Sunday, December 29, 2024. If you expected January 5, you likely need Monday as the start day.
Frequently Asked Questions
What does Date.EndOfWeek return exactly?
It returns a date value representing the last day of the week for the given date, based on the specified start day (default Sunday).
Can I use Date.EndOfWeek with a datetime column?
Yes, it accepts date, datetime, and datetimezone values and returns a date.
How do I set Monday as the start of the week?
Add the second argument: .
What happens if the date is null?
The function returns null. You can handle nulls with an if statement.
Is Date.EndOfWeek available in all Power Query versions?
It is a standard M function available in Excel 2016 and later, Power BI Desktop, and Power Query Online. Menu labels may vary slightly by version.
Can I get the end of the current week?
Use Date.EndOfWeek(DateTime.LocalNow()) to get the end of the week containing today's date.