In the previous post of this series, Fun With KQL – DCount, we saw how to use the
dcount function to get an estimated count of rows for an incoming dataset.
It’s common though to want to filter out certain rows from the count. While you could do the filtering before getting to the
dcount, there’s an alternative function that allows you to do the filtering right within it:
Note if you haven’t read the previous post on
dcount, I’d advise taking a quick read now as we’ll be building on it for this post.
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.
dcountif function is almost identical to
dcount, except it allows for an extra parameter, as you can see in this sample.
Here we are using
in to see if the
EventID column is in the list of values in parenthesis. We could have used any number of comparisons, for example using
== to look for a single value,
!in for not in,
startswith, and many more.
In this result set, only rows whose event IDs were in the list of values are included.
dcountif function returns and estimated count. You can pass in a third parameter with an accuracy level to use, these are the same as in
|Accuracy Value||Error Percentage|
Let’s see an example of it in use.
Here we use a value of
0, which is the least accurate but fastest. As with
dcount we can use values
4 to get the best balance of speed and accuracy for our needs. By default
dcountif will use an accuracy level of
1 if it is omitted.
You can see the Fun With KQL – DCount post for a more extensive discussion on the topic of speed versus accuracy.
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.
In this post we saw how
dcountif can be used to get an estimated distinct count, but also allow you to filter out certain rows from the count, all with a single function.
The demos in this series of blog posts were inspired by my Pluralsight courses Kusto Query Language (KQL) from Scratch and Introduction to the Azure Data Migration Service, 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.