DateTime.IsInNextNSeconds

DateTime.IsInNextNSeconds

D

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) =>

let

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

in

result


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

Contact Us

Subject

Your Name (required)

Company/Organisation

Email (required)

Telephone

Training Course(s)

Your Message

Upload Example Document(s) (Zip multiple files)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: