# Grail code help

**URL:** <https://community.dynatrace.com/t/grail-code-help/53707>\
**Category:** DQL\
**Tags:** dql, dynatrace-managed\
**Created:** [October 13, 2025, 3:25pm UTC](https://community.dynatrace.com/t/grail-code-help/53707 "2025-10-13T15:25:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![derija](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@derija](https://community.dynatrace.com/u/derija)\
**Post date:** [October 13, 2025, 3:25pm UTC](https://community.dynatrace.com/t/grail-code-help/53707/1 "2025-10-13T15:25:35Z")

</div>

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:

![derija_0-1760368990140.png](https://global.discourse-cdn.com/dynatrace/original/3X/a/2/a290b5bba42b0fb9fa921f28e3de3f2f6cf97b92.png)

 ![2025-10-13_08-19-48.jpg](https://global.discourse-cdn.com/dynatrace/original/2X/e/ea5372a7dc5a11d348aa81d0458a2c4073d8d847.jpeg)

---

<div class="post-metadata">

**Author:** ![fstekelenburg](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/fstekelenburg/32/22029_2.png) [@fstekelenburg](https://community.dynatrace.com/u/fstekelenburg)\
**Post date:** [October 13, 2025, 4:23pm UTC](https://community.dynatrace.com/t/grail-code-help/53707/2 "2025-10-13T16:23:12Z")

</div>

Perhaps (since it involves percentage) you could&nbsp;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.

---

<div class="post-metadata">

**Author:** ![derija](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@derija](https://community.dynatrace.com/u/derija)\
**Post date:** [October 13, 2025, 4:40pm UTC](https://community.dynatrace.com/t/grail-code-help/53707/3 "2025-10-13T16:40:23Z")

</div>

Thanks for the option when I just try straight math whether I use toDouble or not I get

![derija_0-1760373595830.png](https://global.discourse-cdn.com/dynatrace/original/2X/a/a7d6ee859ac01d9d872ff1fa68c413723426c148.png)

---

<div class="post-metadata">

**Author:** ![derija](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@derija](https://community.dynatrace.com/u/derija)\
**Post date:** [October 13, 2025, 6:47pm UTC](https://community.dynatrace.com/t/grail-code-help/53707/4 "2025-10-13T18:47:38Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![ZackE](https://avatars.discourse-cdn.com/v4/letter/z/9fc29f/32.png) [@ZackE](https://community.dynatrace.com/u/ZackE)\
**Post date:** [October 13, 2025, 7:01pm UTC](https://community.dynatrace.com/t/grail-code-help/53707/5 "2025-10-13T19:01:23Z")

</div>

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:

```markup
rate5xx = toDouble(countIf(http.response.status_code >= 500) / count() * 100)

```

EDIT: I see that you found a solution.
