Introduction
The take_any
function is a random row generator. Based on the parameters passed it, it will select a random row from the dataset being piped into it. It also has a variant, take_anyif
, we’ll see both in this post.
Note that take_any
was originally called any
and was renamed. While any
still works, it has been deprecated and you should now use take_any
.
Any and all of the samples in this post will be run inside the LogAnalytics demo site found at https://aka.ms/LADemo. This demo site has been provided by Microsoft and can be used to learn the Kusto Query Language at no cost to you.
If you’ve not read my introductory post in this series, I’d advise you to do so now. It describes the user interface in detail. You’ll find it at https://arcanecode.com/2022/04/11/fun-with-kql-the-kusto-query-language/.
Note that my output may not look exactly like yours when you run the sample queries for several reasons. First, Microsoft only keeps a few days of demo data, which are constantly updated, so the dates and sample data won’t match the screen shots.
Second, I’ll be using the column tool (discussed in the introductory post) to limit the output to just the columns needed to demonstrate the query. Finally, Microsoft may make changes to both the user interface and the data structures between the time I write this and when you read it.
Take_Any Basics
Like other functions we’ve covered so far, we will need to use summarize
in order to execute take_any
. In this example, we will pass an *
(asterisk) into the take_any
parameter. This will cause all columns for a random row to be returned.
Each time you execute this you should get a different row back from the piped in dataset. Note there are more columns that appear off screen to the right.
Take_Any For A Column
With take_any
, you can also pass in a specific column name instead of using the *
.
As you can see, it returns a random value from the column passed as the parameter.
Take_Any With Multiple Columns
take_any
can also work with multiple columns. Just pass each column you want as a parameter, and it will return the values from a random row that contains the columns you requested.
Here, we passed three columns from the Perf table into take_any
. It returned a random row with the three columns. We could have used more columns or less, according to our needs.
Returning Random Multiple Rows Based On A Column with Take_Any
You can return multiple rows from take_any
. To do so, you can add by
then the column name after the take_any
as you can see in this example.
Here we passed an *
to get all columns, then we follow with by CounterName
. KQL will get a list of unique CounterNames then return a random row for each one.
Take_AnyIf
The take_anyif
variant of take_any
operates like other if variants we covered recently, maxif
, minif
, and sumif
.
We pass in the name of a column in the first parameter, then the second parameter is a condition. In this case, the row that will be picked randomly must have a CounterName of % Free Space.
In the results, you can see it grabbed a random computer name from the Computer column where the CounterName for that row had a value of % Free Space.
take_anyif
does have a few limitations compared to take_any
. First, you cannot pass in an *
and get all columns. Second, you can only enter a single column. It does not support passing in multiple columns.
See Also
The following operators, functions, and/or plugins were used or mentioned in this article’s demos. You can learn more about them in some of my previous posts, linked below.
Conclusion
In this post we learned how to use take_any
to grab a random row. We saw how to return all columns, a single column, or multiple columns. In addition we saw how to use take_anyif
to grab a random value conditionally.
The demos in this series of blog posts were inspired by my Pluralsight courses on the Kusto Query Language, part of their Kusto Learning Path.
The first course in the series is Kusto Query Language: Getting Started. The second course is Kusto Query Language: Beginning Operators. The third course, to be published soon, is Kusto Query Language: Basic Scalar Operators
I have two previous Kusto courses on Pluralsight as well. Kusto Query Language (KQL) from Scratch and Introduction to the Azure Data Migration Service, and these are two of the many courses I have on Pluralsight. All of my courses are linked on my About Me page.
If you don’t have a Pluralsight subscription, just go to my list of courses on Pluralsight . At the top is a Try For Free button you can use to get a free 10 day subscription to Pluralsight, with which you can watch my courses, or any other course on the site.