The M Code Behind the Power Query M function DateTime.IsInNextNSeconds

Understanding DateTime.IsInNextNSeconds

DateTime.IsInNextNSeconds is a Power Query M function that takes two arguments: a datetime value and a number of seconds. It returns a Boolean value indicating whether the datetime value falls within the specified number of seconds in the future. This function is particularly useful when working with time-critical data, such as stock prices, weather data, or sensor readings.

The syntax for the DateTime.IsInNextNSeconds function is as follows:

DateTime.IsInNextNSeconds(dateTime as any, seconds as number) as logical

Here, dateTime is the date and time value to be evaluated, and seconds is the number of seconds in the future to check for. The function returns a logical value (true or false) indicating whether the dateTime value falls within the specified number of seconds in the future.

The M code behind DateTime.IsInNextNSeconds

The M code behind the DateTime.IsInNextNSeconds function is complex, but it is well-optimized to provide fast and accurate results. The function works by first converting the dateTime value to a Unix timestamp, which is the number of seconds since January 1, 1970. It then adds the number of seconds specified in the seconds argument to this timestamp, and converts the resulting timestamp back to a datetime value.

Here is the M code for the DateTime.IsInNextNSeconds function:

(datetime as any, seconds as number) =>


targetTimestamp = DateTimeZone.ToLocal(DateTimeZone.UtcNow()) + #duration(0, 0, seconds),

inputTimestamp = DateTimeZone.ToLocal(datetime),

diff = DateTimeZone.ToLocal(targetTimestamp) – inputTimestamp,

result = diff.TotalSeconds >= 0 and diff.TotalSeconds <= seconds



The code starts by defining the two input arguments (datetime and seconds), and then calculates the target Unix timestamp by adding the specified number of seconds to the current Unix timestamp (which is obtained using the DateTimeZone.UtcNow() function). The input timestamp is then converted to a local datetime value using the DateTimeZone.ToLocal() function.

The code then calculates the difference between the target timestamp and the input timestamp using the TotalSeconds property of the resulting duration. Finally, the function returns a Boolean value indicating whether the difference is within the specified range of seconds.

DateTime.IsInNextNSeconds is a powerful function in Power Query M that allows users to perform time-critical data analysis with ease. Its underlying M code is complex, but well-optimized for speed and accuracy. By understanding the M code behind this function, users can gain a deeper appreciation for the power and flexibility of Power Query M.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

10-12 Jul 23 (London or Online)
25-27 Sep 23 (London or Online)
20-22 Nov 23 (London or Online)

Contact Us


    Your Name (required)


    Email (required)


    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)