0
votes

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);
2

2 Answers

4
votes

Try the startofmonth() function.

Example:

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

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