DateTime.IsInNextNSeconds

Updated on

DateTime.IsInNextNSeconds is a Power Query M function that indicates whether the provided date(time) value occurs during the next 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.IsInNextNSeconds(
   dateTime as any,
   seconds as number,
) as nullable logical

Description

DateTime.IsInNextNSeconds indicates whether the given dateTime argument (which can be of type datetime or datetimezone) occurs during the next 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.IsInNextNSeconds function works.

Checking DateTime Values for the Next N Seconds

Imagine you have a table with DateTime values and you want to add a column that shows true for DateTime values that fall within the next n seconds. You can do this by using the DateTime.IsInNextNSeconds function on your [DateTime] column, where n is the number of seconds you want to check.

DateTime.IsInNextNSeconds( [DateTime], n ) // Output depends on [DateTime] column and n

This expression returns true for any DateTime values in the [DateTime] column that fall within the next n seconds.

You might wonder what types of values can fall within the next few seconds. We’re talking about values that occur in the immediate future, within the specified range of seconds. For example, if you set n to 2 you would use the following code:

DateTime.IsInNextNSeconds( [DateTime], 2 )

If the current time is 08:35:21.301 on July 8, 2024, this function will return true for all rows with DateTime values between 08:35:22 and 08:35:23.9999999. Here’s a screenshot showing this:

Example of how to use the DateTime.IsInNextNSeconds Function in Power Query_M

As the image shows, there are four values that fall within the next 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.IsInNextNSeconds if the current date and time falls within the next few seconds, you can do that using the DateTime.FixedLocalNow function and adding a number of seconds using the #duration function:

// Output: true for the next n seconds
DateTime.IsInNextNSeconds(
  DateTime.FixedLocalNow() + #duration(0, 0, 0, 3), 
  3
)

Other functions related to DateTime.IsInNextNSeconds are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/datetime-isinnextnseconds

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy