# Calculate Percentages in Dynatrace DQL Queries

**URL:** <https://community.dynatrace.com/t/calculate-percentages-in-dynatrace-dql-queries/45557>\
**Category:** DQL\
**Tags:** dql, dynatrace-managed\
**Created:** [May 13, 2024, 11:57pm UTC](https://community.dynatrace.com/t/calculate-percentages-in-dynatrace-dql-queries/45557 "2024-05-13T23:57:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hfdku](https://avatars.discourse-cdn.com/v4/letter/h/cc9497/32.png) [@hfdku](https://community.dynatrace.com/u/hfdku)\
**Post date:** [May 13, 2024, 11:57pm UTC](https://community.dynatrace.com/t/calculate-percentages-in-dynatrace-dql-queries/45557/1 "2024-05-13T23:57:52Z")

</div>

Given two fields:

genCount = 2

vecEmbeddingCount = 2

I want to calculate the percentage of genCount of the total (ie. The result I’m looking for is 50%).

```markup
fetch logs
...
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount*100

```

![hfdku_0-1715644544283.png](https://global.discourse-cdn.com/dynatrace/original/3X/c/7/c75300b50ad85257a6f7f12d8a60e65bc582006a.png)

What am I missing?

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [May 14, 2024, 5:43am UTC](https://community.dynatrace.com/t/calculate-percentages-in-dynatrace-dql-queries/45557/2 "2024-05-14T05:43:55Z")

</div>

Order of operations matters here, especially these are integer operations:  
2/4\*100 = 0\*100 = 0  
but

100\*2/4 = 200/4 = 50.

Here is DQL example:

```markup
data record(genCount = 2,vecEmbeddingCount = 2)
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount*100
| fieldsAdd cacheHitRateCorrect = 100*genCount / totalCount

```

 ![krzysztof_hoja_0-1715665377589.png](https://global.discourse-cdn.com/dynatrace/original/3X/9/5/9520829566a22cbe65f08ce927d3042a64d7fcc7.png)

Kris

---

<div class="post-metadata">

**Author:** ![hfdku](https://avatars.discourse-cdn.com/v4/letter/h/cc9497/32.png) [@hfdku](https://community.dynatrace.com/u/hfdku)\
**Post date:** [May 14, 2024, 6:39am UTC](https://community.dynatrace.com/t/calculate-percentages-in-dynatrace-dql-queries/45557/3 "2024-05-14T06:39:39Z")

</div>

Where is it specified that these are integers (or integer operations)?  
In fact, integers aren’t even mentioned in the [DQL data types page](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/data-types) - only double and long are listed.

This seems to work:

```markup
fetch logs
| summarize genCount = toDouble(countIf(matchesPhrase(content, "gpt-3.5"))),
            vecEmbeddingCount = toDouble(countIf(matchesPhrase(content, "text-embedding-ada-002")))
| fieldsAdd totalCount = genCount + vecEmbeddingCount
| fieldsAdd cacheHitRate = genCount / totalCount * 100
| fieldsKeep cacheHitRate

```

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [May 14, 2024, 7:51am UTC](https://community.dynatrace.com/t/calculate-percentages-in-dynatrace-dql-queries/45557/4 "2024-05-14T07:51:19Z")

</div>

I did not mean “integer data type”, but meant “integer mathematical concept” ([Integer - Wikipedia](https://en.wikipedia.org/wiki/Integer))

When you convert “long” numbers to “double” numbers (floating point) it will work too, but will be give you result in form of “double”. What type you want to get in the end of course depends on your requirements.

When you get result in form of floating point number, you can always get needed precision using _ **round** _ ([Mathematical functions — Dynatrace Docs](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/mathematical-functions#round)) or needed data type i.e. using _ **toLong** _ ([Conversion and casting functions — Dynatrace Docs](https://docs.dynatrace.com/docs/platform/grail/dynatrace-query-language/functions/conversion-and-casting-functions#toLong))

Re: “order of operations” which often matter when dealing with integers . DQL uses most common approach uses by programming languages (“left to right”), probably that’s why it was never mentioned in documentation. Example in second paragraph here covers similar case like ours:&nbsp;[Order of operations - Wikipedia](https://en.wikipedia.org/wiki/Order_of_operations#Programming_languages)

Kris
