DateTime.IsInPreviousNSeconds

D

T

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

Understanding the DateTime.IsInPreviousNSeconds Function

The DateTime.IsInPreviousNSeconds function allows users to filter data based on whether or not a date and time value falls within a specified number of seconds before the current date and time. For example, if we wanted to filter a list of orders to only show those that were placed within the last 10 minutes, we could use the DateTime.IsInPreviousNSeconds function.

The function takes two arguments: the first is the column containing the date and time values, and the second is the number of seconds to look back. Here is the syntax for the function:


DateTime.IsInPreviousNSeconds(dateTimeColumn as any, seconds as number) as any


Let's take a look at an example. Suppose we have a table of sales data with a column named "Order Date" that contains date and time values. We want to create a new table that only includes orders that were placed within the last 5 minutes. We can use the following M code:


let

Source = Sales_Data,

FilteredRows = Table.SelectRows(Source, each DateTime.IsInPreviousNSeconds([Order Date], 300))

in

FilteredRows


In this code, we first define the source table as "Sales_Data". We then create a new table called "FilteredRows" by using the Table.SelectRows function to filter the data based on the DateTime.IsInPreviousNSeconds function. We pass in the "Order Date" column as the first argument and 300 (which is 5 minutes in seconds) as the second argument.

Breaking Down the M Code

Now let's take a closer look at the M code behind the DateTime.IsInPreviousNSeconds function. Here is the code for the function:


(DateTime.From(DateTime.LocalNow()) – dateTimeColumn) <= #duration(0,0,0,seconds)


This code subtracts the date and time value in the specified column from the current date and time (using the DateTime.From and DateTime.LocalNow functions) and checks if the result is less than or equal to a duration value that represents the number of seconds specified in the second argument.

Let's break down the code piece by piece:

- DateTime.From(DateTime.LocalNow()): This function returns the current date and time as a datetime value.

- - dateTimeColumn: This subtracts the date and time value in the specified column from the current date and time.

- #duration(0,0,0,seconds): This creates a duration value that represents the number of seconds specified in the second argument of the function.

The result of this calculation is a boolean value that indicates whether or not the date and time value falls within the specified number of seconds before the current date and time.

The DateTime.IsInPreviousNSeconds function is a powerful tool in Power Query that allows users to filter data based on a specified time frame. The M code behind the function is relatively simple, but it can be difficult to understand if you are not familiar with Power Query and M code. By breaking down the code step by step, we hope that you now have a better understanding of how this function works and how you can use it in your own projects.

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)