DateTime.IsInPreviousSecond

D

T

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

Understanding the DateTime.IsInPreviousSecond Function

Before we dive into the M code behind DateTime.IsInPreviousSecond, let’s first explore what this function does. The DateTime.IsInPreviousSecond function takes a single argument, a datetime value, and returns a boolean value indicating whether the datetime value is in the previous second.

This function is particularly useful when working with streaming data, where new data is constantly being added to a dataset. By using DateTime.IsInPreviousSecond, you can quickly determine if the new data point is within the previous second, allowing you to aggregate the data in real-time.

Breaking Down the M Code Behind DateTime.IsInPreviousSecond

To understand how the DateTime.IsInPreviousSecond function works, we need to first look at the M code behind it. The M code for this function is as follows:


let

IsInPreviousSecond = (datetime as datetime) =>

let

PrevSecond = DateTime.LocalNow() – #duration(0, 0, 0, 1),

Result = datetime > PrevSecond and datetime <= DateTime.LocalNow()

in

Result

in

IsInPreviousSecond


Let's break down this code into its individual components:

- The first line defines a function called "IsInPreviousSecond", which takes a single argument, a datetime value.

- The next line defines a variable called "PrevSecond", which is set to the current local date and time minus one second.

- The third line defines a variable called "Result", which checks if the datetime value is greater than the previous second and less than or equal to the current local date and time.

- The final line returns the "Result" variable.

Using DateTime.IsInPreviousSecond in Power Query

Now that we understand how the DateTime.IsInPreviousSecond function works, let's look at how we can use it in Power Query.

To use the DateTime.IsInPreviousSecond function, we first need to create a new column in our dataset that contains our datetime values. We can do this by selecting the "Add Column" tab in the Power Query Editor and choosing "Add Custom Column". In the formula bar, we can then enter the DateTime.IsInPreviousSecond function, passing in the name of the column containing our datetime values:


DateTime.IsInPreviousSecond([DateTimeColumn])


This will create a new column in our dataset that contains boolean values indicating whether each datetime value is in the previous second.

In conclusion, the DateTime.IsInPreviousSecond function is a powerful tool in data analysis that allows you to quickly determine if a given datetime value is in the previous second. By understanding the M code behind this function, you can gain a deeper understanding of how it works and how to use it effectively in your Power Query 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)