DateTime.IsInCurrentSecond

D

T

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

What is DateTime.IsInCurrentSecond?

DateTime.IsInCurrentSecond is a Power Query M function that checks whether a given datetime value falls within the current second. The function returns a boolean value of true if the datetime value falls within the current second, and false otherwise.

How to use DateTime.IsInCurrentSecond

The DateTime.IsInCurrentSecond function takes a single argument, which is a datetime value to be checked. Here’s the syntax:


DateTime.IsInCurrentSecond(datetime as datetime) as logical


Let's take a look at an example. Suppose we have a table containing datetime values in the column "Date". We can use the DateTime.IsInCurrentSecond function to filter the table to only show records where the datetime value falls within the current second. Here's the M code:


let

Source = Table.FromRows({{“2022-01-01T12:34:56.123”}, {“2022-01-01T12:34:57.456”}, {“2022-01-01T12:34:58.789”}}, {“Date”}),

#”Changed Type” = Table.TransformColumnTypes(Source,{{“Date”, type datetime}}),

#”Filtered Rows” = Table.SelectRows(#”Changed Type”, each DateTime.IsInCurrentSecond([Date]))

in

#”Filtered Rows”


In this example, the Source step creates a table with three datetime values. The Changed Type step converts the "Date" column to the datetime data type. Finally, the Filtered Rows step uses the DateTime.IsInCurrentSecond function to filter the table to only show records where the datetime value falls within the current second.

Understanding the M code behind DateTime.IsInCurrentSecond

Now that we know how to use DateTime.IsInCurrentSecond, let's take a closer look at the M code behind the function. Here's the M code for the function:


(DateTime) =>

let

CurrentTime = Time.From(DateTime.LocalNow()),

CurrentSecondStart = Time.FromText(“00:00:00”),

NextSecondStart = Time.FromText(“00:00:01”),

TimeInCurrentSecond = Time.From(DateTime) – CurrentSecondStart

in

CurrentTime >= CurrentSecondStart and CurrentTime < NextSecondStart and TimeInCurrentSecond < #duration(0,0,1,0)


The function takes a datetime value as its input and returns a boolean value as its output. The M code first extracts the current time using the Time.From and DateTime.LocalNow functions.


CurrentTime = Time.From(DateTime.LocalNow())


Next, the M code defines the start time of the current second and the start time of the next second using the Time.FromText function.


CurrentSecondStart = Time.FromText(“00:00:00”),

NextSecondStart = Time.FromText(“00:00:01”)


The M code then calculates the time elapsed since the start of the current second using the Time.From and #duration functions.


TimeInCurrentSecond = Time.From(DateTime) – CurrentSecondStart

TimeInCurrentSecond < #duration(0,0,1,0)


Finally, the M code checks whether the current time falls within the current second and whether the elapsed time is less than one second.


CurrentTime >= CurrentSecondStart and CurrentTime < NextSecondStart and TimeInCurrentSecond < #duration(0,0,1,0)


In this article, we explored the DateTime.IsInCurrentSecond function in Power Query M and its M code. We learned how to use the function to filter a table to only show records where the datetime value falls within the current second. We also gained a deeper understanding of the M code behind the function. By understanding the M code, we can better leverage the full power of Power Query M in our data transformation and manipulation tasks.

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)