Azure Data Explorer - Perform Calculation On Multiple Values From Single Kusto Input

Let’s consider a scenario, wherein the requirement is to find out the percentage of a particular type of values from a single input set.

Below can be considered as an example of input sample data and we need to find out what percentage of dev releases and what percentage of prod releases are present in the input data.

let demoData = datatable(Environment: string, Feature:string)  
[  
"dev", "Feature1",  
"test", "Feature1",  
"prod", "Feature1",  
"Dev", "Feature2",  
"test", "Feature2",  
"dev", "Feature3",  
"test", "Feature3",  
"prod", "Feature3" 
];  


Approach

In order to achieve the solution, one has to go through various steps as mentioned below,

Step 1

Get the total number of records from the set.

let totalRecords = demoData
| count 
| project TotalRecords = Count;  


Step 2

Get only those records which are of type ‘dev’

let devRecords = demoData
| where Environment =~ "dev" 
| count 
| project TotalDevRecords = Count;  


Step 3

Get only those records which are of type ‘prod’

let prodRecords = demoData
| where Environment =~ "prod" 
| count
| project TotalProdRecords=Count;  


So far we have got all the individual parts. The next task is to combine all the above mentioned 3 steps and generate a single result set and here comes the challenge.

Challenge

As input set is holding only two columns, there is no common field in all the above mentioned three queries, and as there is no commonality it is significantly difficult to bring such a result set together to form a single result set.

Addressing the challenge

Can’t we go ahead and introduce some new column just for the sake of projection? Well, let’s see how that changes our above 3 steps now,

Updated Step 1

let totalRecords = demoData  
| count |extend CommonCol="Dummy" 
| project CommonCol, TotalRecords = Count;  


Updated Step 2

let devRecords = demoData  
| where Environment =~ "dev" 
| count | extend CommonCol="Dummy" 
| project CommonCol, TotalDevRecords = Count;  


Updated Step 3

let prodRecords = demoData  
| where Environment =~ "prod" 
| count|extend CommonCol="Dummy" 
| project CommonCol, TotalProdRecords = Count;  


Now comes the final step, wherein we need to bring all the above result set together to calculate the percentage.

Step 4

Combining the individual results to get a single result.

totalRecords  
| join (devRecords | join prodRecords on CommonCol) on CommonCol  
| extend DevRecords = (TotalDevRecords * 100)/TotalRecords  
| extend ProdRecords = (TotalProdRecords * 100)/TotalRecords  
| project DevRecords, ProdRecords;  


Complete query

let totalRecords = demoData  
| count 
|extend CommonCol="Dummy" 
| project CommonCol, TotalRecords = Count;  
let devRecords = demoData  
| where Environment =~ "dev" 
| count 
| extend CommonCol="Dummy" 
| project CommonCol, TotalDevRecords = Count;  
let prodRecords = demoData  
| where Environment =~ "prod" 
| count|extend CommonCol="Dummy" 
| project CommonCol, TotalProdRecords = Count;  
totalRecords  
| join (devRecords   
| join prodRecords on CommonCol) on CommonCol  
| extend DevRecords = (TotalDevRecords * 100)/TotalRecords  
| extend ProdRecords = (TotalProdRecords * 100)/TotalRecords  
| project DevRecords, ProdRecords;  


Result

On execution of the above steps, you will get the desired output as shown below,




Source: C# Corner

0 comments