DateTime.IsInPreviousNHours

DateTime

Returns true if the datetime falls within the previous N clock hours.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

DateTime.IsInPreviousNHours(dateTime as any, hours as number) as logical

Parameters

NameTypeRequiredDescription
dateTimeanyYesA datetime or datetimezone value to test.
hoursnumberYesThe number of hours back to include. Must be a positive integer.

Return Value

logicaltrue 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
1303/8/2026 6:15:00 AM
2353/8/2026 8:45:00 AM
3403/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
1253/8/2026 1:00:00 AMFALSE
2303/8/2026 6:15:00 AMTRUE
3423/8/2026 10:05:00 AMFALSE

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks