Use Variables in Kusto


In kusto, there are basically two types of data, one is tabular, another is scalar. By using let operator, you can predefine some parameters before a tabular querying. now, how should you do if you want to reassign the parameters in a the middle of a query? just like Variable do in a general programming language.

Yes, we can easily achieve it by using toscalar() function. for example:

let max_date_tabular = sample_table
| summarize
    max_date = max(Timestamp);
let max_date = toscalar(max_date_tabular);
| where 
    date_time > datetime_add('month',-1,max_date)
| take 10

in this script, first, I get the max date value from sample_table, but now, the date is still in tabular format, we can't use it in a where statement. Next, I use toscalar() function to convert the tabular data to scalar value. and I can now use it in my next query.