How to do while / for loop in azure data explorer / kusto?

Sunil Surana 1 Reputation point Microsoft Employee
2023-01-09T11:19:54.2+00:00

I want to run for/while loop to retrieve records using Kusto query. For eg i want to query some rows and depending on corresponding values of those rows i want to query more rows and keep doing it till certain condition satisfy. And while doing this i want to keep appending result of each query to some temp variable/view? Its like building a graph report from one single table.

So how can we run loops in Kusto. I looked into stored function but could not find any loop syntax.

Azure Data Explorer
Azure Data Explorer
An Azure data analytics service for real-time analysis on large volumes of data streaming from sources including applications, websites, and internet of things devices.
525 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sander van de Velde | MVP 32,556 Reputation points MVP
    2023-01-13T23:37:59.8933333+00:00

    Hello Sunil,

    Azure Data Explorer is a database, therefor the Kusto language is thinking in datasets.

    If you want to make a decision on the outcome of a certain decision, you could try to join that with another query by capturing the first set in a 'variable'.

    Check out the LET statement.

    This is an example that is without meaning but it shows the possibilities:

    let compromisedProxies = Votes
    | where vote == “YaddaYadda”
    | where outliers == 1
    | distinct via_ip;
    Votes
    | where not(via_ip in (compromisedProxies) and vote == “BlaBla”)
    | summarize Count=count() by vote
    

    Here, the two queries are combined with this 'in' but a 'join' or 'union' could work too.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.