DateTime.IsInPreviousNSeconds is a Power Query M function that indicates whether the provided date(time) value occurs during the previous number of seconds specified. The function returns true if the value occurs within the specified seconds, otherwise false.
Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365
Syntax
DateTime.IsInPreviousNSeconds(
dateTime as any,
seconds as number,
) as nullable logical
Description
DateTime.IsInPreviousNSeconds indicates whether the given dateTime
argument (which can be of type datetime or datetimezone) occurs during the previous number of seconds, as determined by the current date and time on the system. This function will return false for a value within the current second.
Examples
Let’s explore two examples to understand how the DateTime.IsInPreviousNSeconds function works.
Checking DateTime Values for the Previous N Seconds
Suppose you have a table with DateTime values and you want to add a column that marks true
for DateTime values that fall within the previous n seconds. You can achieve this by using the DateTime.IsInPreviousNSeconds function on your [DateTime]
column, where n is the number of seconds you want to check.
// Output depends on [DateTime] column and n
DateTime.IsInPreviousNSeconds( [DateTime], n )
This expression returns true
for any DateTime values in the [DateTime]
column that fall within the previous n seconds.
You might wonder what types of values can fall within the previous few seconds. We’re talking about values that occurred in the immediate past, within the specified range of seconds and possibly milliseconds. For example, if you set n to 2 you would use the following code:
DateTime.IsInPreviousNSeconds( [DateTime], 2 )
If the current time is 22:17:40.757 on July 7, 2024, this function will return true
for all rows with DateTime values between 22:17:38 and 22:17:39.9999999. Here’s a screenshot showing this:
As the image shows, there are several values that fall within the previous n seconds. Although the default DateTime value does not display fractional seconds, you can view the detailed values in the [DateTime Text]
column, which uses a format string that includes fractional seconds.
Using the Current Date and Time
If you want to check with DateTime.IsInPreviousNSeconds if the current date and time falls within the previous few seconds, you can do that using the DateTime.FixedLocalNow function and subtracting a number of seconds using the #duration function:
// Output: true
DateTime.IsInPreviousNSeconds(
DateTime.FixedLocalNow() - #duration(0, 0, 0, 3),
3
)
Related functions
Other functions related to DateTime.IsInPreviousNSeconds are:
- DateTime.IsInPreviousHour
- DateTime.IsInPreviousMinute
- DateTime.IsInPreviousNHours
- DateTime.IsInPreviousNMinutes
- DateTime.IsInPreviousSecond
2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy