13 Oct 2025
	
		
		04:25 PM
	
	
	
	
	
	
	
	
	
	
	
	
	
	
 - last edited on 
    
	
		
		
		15 Oct 2025
	
		
		01:21 PM
	
	
	
	
	
	
	
	
	
	
	
	
	
	
 by 
				
		
		
			MaciejNeumann
		
		
		
		
		
		
		
		
	
			
		
Here is my code related to area trying to get the percentage of 5xx. I seem to have all the data here but the simple math is not working out...Any help?
| summarize { dur_msAvg=avg(duration), dur_msMax=max(duration), dur_msMin=min(duration), dur_medMin=median(duration),
failedRequests300 = countIf(http.response.status_code >= 300 and http.response.status_code < 400),
failedRequests400 = countIf(http.response.status_code >= 400 and http.response.status_code <500),
failedRequests500 = countIf(http.response.status_code >= 500),
successfulRequests = countIf(http.response.status_code == 200),
allRequests = count(),
rate5xx = countIf( toDouble((failedRequests500 / allRequests) * 100) <> 0) },
by:{service.entity.name, endpoint.name, appName}
output:
Solved! Go to Solution.
13 Oct 2025 05:23 PM
Perhaps (since it involves percentage) you could calculate the percentage directly, like this?
rate5xx = toDouble(failedRequests500) / toDouble(allRequests) * 100
Remarks:
- toDouble() ensures you're doing floating-point division.
- I don't think you need to wrap the percentage calculation in countIf — that’s only for counting events that match a condition.
13 Oct 2025 05:40 PM
Thanks for the option when I just try straight math whether I use toDouble or not I get
13 Oct 2025 07:47 PM
Here is the fix...I had to END the summarize, then include the field...subtle but I missed it..
|summarize by:{service.entity.name, endpoint.name, appName},
{ dur_msAvg=avg(duration), dur_msMax=max(duration), dur_msMin=min(duration), dur_medMin=median(duration),
failedRequests300 = countIf(http.response.status_code >= 300 and http.response.status_code < 400),
failedRequests400 = countIf(http.response.status_code >= 400 and http.response.status_code <500),
failedRequests500 = countIf(http.response.status_code >= 500),
successfulRequests = countIf(http.response.status_code == 200),
allRequests = count()}
|fieldsAdd rate5xx = if(allRequests > 0, toDouble(failedRequests500) / toDouble(allRequests) * 100)
|fieldsAdd rate4xx = if(allRequests > 0, toDouble(failedRequests400) / toDouble(allRequests) * 100)
13 Oct 2025 08:01 PM - edited 13 Oct 2025 08:03 PM
The issue is that you're trying to reference fields created within the same summarize command and that isn't possible. You will have to do something like this:
rate5xx = toDouble(countIf(http.response.status_code >= 500) / count() * 100)EDIT: I see that you found a solution.