# Using if condition in summarize

**URL:** <https://community.dynatrace.com/t/using-if-condition-in-summarize/54749>\
**Category:** DQL\
**Tags:** dql, dynatrace-managed, dashboards\
**Created:** [January 26, 2026, 1:10pm UTC](https://community.dynatrace.com/t/using-if-condition-in-summarize/54749 "2026-01-26T13:10:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaima](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@shaima](https://community.dynatrace.com/u/shaima)\
**Post date:** [January 26, 2026, 1:10pm UTC](https://community.dynatrace.com/t/using-if-condition-in-summarize/54749/1 "2026-01-26T13:10:15Z")

</div>

Hello,  
I’ve been asked to aggregate metric dimensions for a dashboard.The metric has over 100 dimensions. Each dimension represents a numeric value ranging from 0 to 150. The customer provided predefined groups, and I need to aggregate the dimensions according to those ranges, for example:  
01–34–1818–32…100+  
I have a few questions regarding the best way to approach this.  
1. Complexity of my current solutionI managed to create a working solution, but it’s overly complex. I’m wondering if there’s a simpler or more elegant way to achieve this.  
Solution 1:  
timeseries { value.A = sum(metric.count.numbers, scalar: true) }, by: { Dimension }| summarize sum = sum(value.A), by: {&nbsp; Dimension == “0”,&nbsp; (Dimension == “1” OR Dimension == “2” OR Dimension == “3”)&nbsp; // etc ..}Output:

 ![shaima_1-1769432473478.png](https://global.discourse-cdn.com/dynatrace/original/2X/b/b1e7e91dd64189ab51afe547cc0fa390cfd57a38.png)

2. Issue with an alternative approach using asLong()I also tried a second approach, which feels more logical to me, but it doesn’t work as expected. Specifically, using the asLong() function returns null.Why does asLong(Dimension) return null in this case? Is there a different or recommended way to implement this kind of numeric range grouping?  
Solution 2:  
timeseries { value.A = sum(metric.count.numbers, scalar: true) }, by: { Dimension }| summarize sum = sum(value.A), by: {&nbsp; asLong(Dimension) == 0,&nbsp; (asLong(Dimension) \>= 1 AND asLong(Dimension) \<= 3),&nbsp; (asLong(Dimension) \>= 4)&nbsp; // etc..}  
Output:  
 ![shaima_2-1769432637996.png](https://global.discourse-cdn.com/dynatrace/original/3X/0/5/053a5ae1a5ef3b6cae99a517ebf9504f2c604c87.png)

Thanks in advance for any guidance.

---

<div class="post-metadata">

**Author:** ![t\_pawlak](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/t_pawlak/32/14991_2.png) [@t\_pawlak](https://community.dynatrace.com/u/t_pawlak)\
**Post date:** [January 27, 2026, 12:27pm UTC](https://community.dynatrace.com/t/using-if-condition-in-summarize/54749/2 "2026-01-27T12:27:59Z")

</div>

Hi,

You’re right — Solution 1 works, but it becomes hard to maintain because you’re grouping by many boolean expressions (true/false), which doesn’t scale when you have 100+ dimension values.  
You can try this:

1. convert the dimension to a number,
2. build a single bucket label field,
3. summarize by that bucket.

I couldn’t properly validate this against your specific metric (metric.count.numbers) in your environment, so I used a built-in CPU metric as an example.

```markup
timeseries {
  v = avg(dt.host.cpu.usage, scalar: true)
}, by: { Dimension }

| fieldsAdd d = toLong(Dimension)

| fieldsAdd bucket =
    if(isNull(d), "NON_NUMERIC",
      else: if(d == 0, "0",
        else: if(d >= 1 and d <= 3, "1–3",
          else: if(d >= 4 and d <= 18, "4–18",
            else: if(d > 18 and d <= 32, "19–32",
              else: if(d >= 100, "100+",
                else: "33–99"
              )
            )
          )
        )
      )
    )

| summarize sum = sum(v), by: { bucket }
| sort bucket asc

```

 ![t_pawlak_0-1769516717752.png](https://global.discourse-cdn.com/dynatrace/original/2X/c/c63c5bbae9a319ae9287e257a693ce3b42ce3627.png)

For real usage, replace the metric and Dimension with the actual metric key and dimension name from your dataset.

And your second question, why does asLong(Dimension) return null.  
Because asLong() typically does not perform a string-to-long conversion. It behaves more like a cast/interpretation and returns a value only if the input is already compatible with a numeric type.  
Since your dimension values are strings (“0”, “1”, …), asLong(Dimension) can evaluate to null.  
Use toLong(Dimension) instead — it performs the actual conversion and allows numeric range comparisons (\>=, \<=) for bucketing.

 ![t_pawlak_1-1769516850979.png](https://global.discourse-cdn.com/dynatrace/original/2X/e/edcab2a3c5a668e382321606aacfbb475986a35e.png)

---

<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:** [January 27, 2026, 4:08pm UTC](https://community.dynatrace.com/t/using-if-condition-in-summarize/54749/3 "2026-01-27T16:08:46Z")

</div>

I usually use _**coalesce()**_ funtion to construct these (switch/case)/(case/when) statements. Less brackets to type, no need for _ **else:** _ and easier to maintain:

```markup
| fieldsAdd bucket = coalesce(
  if(d == 0, "0"),
  if(d >= 1 and d <= 3, "1–3"),
  if(d >= 4 and d <= 18, "4–18"),
  if(d > 18 and d <= 32, "19–32"),
  if(d > 33 and d <= 99, "33–99"),
  if(d >= 100, "100+"),
  "NONUMERIC"
)

```

In case set of possible vales are not numbers which can be organized in ranges, instead of OR operator _**in()**_ function will provide much shorter syntax:

```markup
if(in(Dimension, {"dog","cat","frog"}), "pets"),

```

---

<div class="post-metadata">

**Author:** ![shaima](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@shaima](https://community.dynatrace.com/u/shaima)\
**Post date:** [March 5, 2026, 4:08pm UTC](https://community.dynatrace.com/t/using-if-condition-in-summarize/54749/4 "2026-03-05T16:08:14Z")

</div>

I believe the main issue was the use of&nbsp;asLong() instead of toLong(), and&nbsp;ended up implementing the second solution thanks to your clarification.

Thank you 🙂
