DateTime.IsInPreviousNHours
DateTimeReturns true if the datetime falls within the previous N clock hours.
Syntax
DateTime.IsInPreviousNHours(dateTime as any, hours as number) as logicalParameters
| Name | Type | Required | Description |
|---|---|---|---|
dateTime | any | Yes | A datetime or datetimezone value to test. |
hours | number | Yes | The number of hours back to include. Must be a positive integer. |
Return Value
logical — true if the datetime falls within the N hours before the current hour, false otherwise.
Remarks
DateTime.IsInPreviousNHours returns true if the input datetime or datetimezone value falls within the N complete clock hours preceding the current hour. The current hour itself is not included; use DateTime.IsInCurrentHour to also match the current hour. The function is re-evaluated on each query refresh.
Examples
Example 1: Filter events from the previous 4 hours
Table.SelectRows(
Events,
each DateTime.IsInPreviousNHours([EventTime], 4)
)Result
EventID | EventTime | |
|---|---|---|
| 1 | 30 | 3/8/2026 6:15:00 AM |
| 2 | 35 | 3/8/2026 8:45:00 AM |
| 3 | 40 | 3/8/2026 9:30:00 AM |
Example 2: Flag events from the past 8 hours
Table.AddColumn(
Events,
"InPrevious8Hours", each DateTime.IsInPreviousNHours([EventTime], 8), type logical
)Result
EventID | EventTime | InPrevious8Hours | |
|---|---|---|---|
| 1 | 25 | 3/8/2026 1:00:00 AM | FALSE |
| 2 | 30 | 3/8/2026 6:15:00 AM | TRUE |
| 3 | 42 | 3/8/2026 10:05:00 AM | FALSE |
Compatibility
✓ Power BI Desktop✓ Power BI Service✓ Excel Desktop✓ Excel Online✓ Dataflows✓ Fabric Notebooks