Meow
Meow

Reputation: 19071

Kusto query - how to get beginning datetime of current month

Learning Kusto query and looking for a way to get beginning of current month datetime. As of time I post this it is 2/25/2020 so output should looks like below represents Feb 1, 2020

enter image description here

This is what I have so far and works, but there should be better way of doing this. Can anyone please let me know if this query can be improved? What's the common practice of getting beginning of current month?

Below, get year and month, add leading 0 if needed for month then concatenate the string and assign to variable "d" which then look like "2020-02-01" and pass that string to todatetime()

let year = datetime_part("Year",now());
let month = datetime_part("Month",now());
let m = case(month < 10, strcat("0", month), tostring(month));
let d = strcat(year, "-", m, "-01" );
print todatetime(d);

Upvotes: 2

Views: 6575

Answers (2)

Yoni L.
Yoni L.

Reputation: 25895

there's a startofmonth() function: https://learn.microsoft.com/en-us/azure/kusto/query/startofmonthfunction

Upvotes: 1

an1que
an1que

Reputation: 423

Try the startofmonth() function.

Example:

MyKustoTable 
| project MonthStart = startofmonth(datetime('2020-2-5')) 

Reference: https://learn.microsoft.com/en-us/azure/kusto/query/startofmonthfunction

Upvotes: 6

Related Questions