Search code examples
kqlkusto-explorer

How to detect if Tabular variable is empty in KQL


I have a dashboard populated with a number of Kusto KQL Queries.

Sometimes, my query below returns zero results (for instance if by miracle, there are no failures in the last 24 hours).

//my dashboard query 
let failureResults = exceptions | where blahblahblah;
failureResults;

When there are no items that match the filters, my dashboard is filled with

'The query returned no Results'.

How could I go about checking if this variable is null and then doing a different op? For instance, if it's null, then I would just issue a print "No Failures for today, awesome!"; instead.

I have tried iff() statements and isempty(failures| distinct Outcome) and the like, but to no avail. For example, here is another one which didn't work:

failures | project column_ifexists(tostring(Outcome),"No failures where reported!")

Solution

  • Just thought on an improved solution based on pack_all() and the bag_unpack plugin

    let p_threshold = ... ;// set value
    let failureResults = datatable(exception_id:int,exception_val:int,exception_text:string)[1,100,"Hello" ,2,200,"World"];
    failureResults
    | where exception_val > p_threshold
    | as t1
    | project result = pack_all()
    | union kind=outer (print msg = 'No Failures for today, awesome!' | where toscalar(t1 | take 1 | count) == 0 | project result = pack_all())
    | evaluate bag_unpack(result)
    

    let p_threshold = 0;

    exception_id exception_text exception_val
    1 Hello 100
    2 World 200

    let p_threshold = 300;

    msg
    No Failures for today, awesome!

    Fiddle