endofweek
This page explains how to use the endofweek function in APL.
Use the endofweek function in APL to calculate the end of the week for a given datetime value. The function returns a datetime set to the last moment of Saturday (23:59:59.9999999), with an optional offset to shift forward or backward by a specified number of weeks.
You can use endofweek to create weekly time boundaries for aggregation, reporting, and trend analysis. This is especially useful when you need to bucket events into weekly intervals or define weekly reporting windows.
Use it when you want to:
- Define end-of-week boundaries for weekly reports and dashboards.
- Aggregate events to weekly intervals for trend analysis.
- Build weekly summaries across log, trace, or security datasets.
Usage
Syntax
endofweek(datetime [, offset])Parameters
| Name | Type | Description |
|---|---|---|
| datetime | datetime | The input datetime value. |
| offset | long | Optional: The number of weeks to offset from the input date. Default is 0. |
Returns
A datetime representing the end of the week (Saturday 23:59:59.9999999) for the given date, shifted by the offset if specified.
Use case examples
Summarize total requests per week to track weekly traffic volume.
Query
['sample-http-logs']
| extend week_end = endofweek(_time)
| summarize total_requests = count() by week_end
| sort by week_end ascOutput
| week_end | total_requests |
|---|---|
| 2024-11-16T23:59:59.9999999Z | 4521 |
| 2024-11-23T23:59:59.9999999Z | 4837 |
| 2024-11-30T23:59:59.9999999Z | 4392 |
This query groups HTTP log events by end-of-week boundaries and counts the total requests in each week.
Track weekly average span duration for each service to monitor performance trends over time.
Query
['otel-demo-traces']
| extend week_end = endofweek(_time)
| summarize avg_duration = avg(duration) by week_end, ['service.name']
| sort by week_end ascOutput
| week_end | service.name | avg_duration |
|---|---|---|
| 2024-11-16T23:59:59.9999999Z | frontend | 00:00:01.2430000 |
| 2024-11-23T23:59:59.9999999Z | frontend | 00:00:01.1870000 |
| 2024-11-30T23:59:59.9999999Z | frontend | 00:00:01.3010000 |
This query shows how average span duration changes week by week for each service, helping you spot performance regressions.
Monitor weekly error trends to detect sustained increases in server failures.
Query
['sample-http-logs']
| where toint(status) >= 500
| extend week_end = endofweek(_time)
| summarize error_count = count() by week_end
| sort by week_end ascOutput
| week_end | error_count |
|---|---|
| 2024-11-16T23:59:59.9999999Z | 52 |
| 2024-11-23T23:59:59.9999999Z | 78 |
| 2024-11-30T23:59:59.9999999Z | 41 |
This query counts server errors by week to help you identify weeks with elevated failure rates.
List of related functions
- startofweek: Returns the start of the week for a datetime, useful for defining the beginning of weekly intervals.
- endofday: Returns the end of the day for a datetime.
- endofmonth: Returns the end of the month for a datetime.
- endofyear: Returns the end of the year for a datetime.
- week_of_year: Returns the ISO 8601 week number, useful for week-based grouping.