<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Using if condition in summarize in DQL</title>
    <link>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/293697#M3083</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;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.&lt;BR /&gt;You can try this:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;convert the dimension to a number,&lt;/LI&gt;&lt;LI&gt;build a single bucket label field,&lt;/LI&gt;&lt;LI&gt;summarize by that bucket.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;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.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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 &amp;gt;= 1 and d &amp;lt;= 3, "1–3",
          else: if(d &amp;gt;= 4 and d &amp;lt;= 18, "4–18",
            else: if(d &amp;gt; 18 and d &amp;lt;= 32, "19–32",
              else: if(d &amp;gt;= 100, "100+",
                else: "33–99"
              )
            )
          )
        )
      )
    )

| summarize sum = sum(v), by: { bucket }
| sort bucket asc&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="t_pawlak_0-1769516717752.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/31756iA87B08E0657D3F6F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="t_pawlak_0-1769516717752.png" alt="t_pawlak_0-1769516717752.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;For real usage, replace the metric and Dimension with the actual metric key and dimension name from your dataset.&lt;BR /&gt;&lt;BR /&gt;And your second question, w&lt;SPAN&gt;hy does asLong(Dimension) return null.&lt;BR /&gt;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.&lt;BR /&gt;Since your dimension values are strings ("0", "1", …), asLong(Dimension) can evaluate to null.&lt;BR /&gt;Use toLong(Dimension) instead — it performs the actual conversion and allows numeric range comparisons (&amp;gt;=, &amp;lt;=) for bucketing.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="t_pawlak_1-1769516850979.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/31757iA3D1F38710C55DFC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="t_pawlak_1-1769516850979.png" alt="t_pawlak_1-1769516850979.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Jan 2026 12:27:59 GMT</pubDate>
    <dc:creator>t_pawlak</dc:creator>
    <dc:date>2026-01-27T12:27:59Z</dc:date>
    <item>
      <title>Using if condition in summarize</title>
      <link>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/293621#M3077</link>
      <description>&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;I’ve been asked to aggregate metric dimensions for a dashboard.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;0&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;1–3&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;4–18&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;18–32&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;…&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;100+&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;I have a few questions regarding the best way to approach this.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;1.&lt;/SPAN&gt;&lt;SPAN&gt; Complexity of my current solution&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;I 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.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Solution 1:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;timeseries { value.A = sum(metric.count.numbers, scalar: true) }, by: { Dimension }&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;| summarize sum = sum(value.A), by: {&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; Dimension == "0",&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; (Dimension == "1" OR Dimension == "2" OR Dimension == "3")&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; // etc ..&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Output:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shaima_1-1769432473478.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/31737iEEE8E76942F595C8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shaima_1-1769432473478.png" alt="shaima_1-1769432473478.png" /&gt;&lt;/span&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;2.&lt;/SPAN&gt;&lt;SPAN&gt; Issue with an alternative approach using asLong()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Why does asLong(Dimension) return null in this case? Is there a different or recommended way to implement this kind of numeric range grouping?&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Solution 2:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;timeseries { value.A = sum(metric.count.numbers, scalar: true) }, by: { Dimension }&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;| summarize sum = sum(value.A), by: {&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; asLong(Dimension) == 0,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; (asLong(Dimension) &amp;gt;= 1 AND asLong(Dimension) &amp;lt;= 3),&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; (asLong(Dimension) &amp;gt;= 4)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; // etc..&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Output:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="shaima_2-1769432637996.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/31738iB9D891F8169A6EA9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="shaima_2-1769432637996.png" alt="shaima_2-1769432637996.png" /&gt;&lt;/span&gt;&lt;/DIV&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;SPAN&gt;Thanks in advance for any guidance.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 26 Jan 2026 13:10:15 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/293621#M3077</guid>
      <dc:creator>shaima</dc:creator>
      <dc:date>2026-01-26T13:10:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using if condition in summarize</title>
      <link>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/293697#M3083</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;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.&lt;BR /&gt;You can try this:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;convert the dimension to a number,&lt;/LI&gt;&lt;LI&gt;build a single bucket label field,&lt;/LI&gt;&lt;LI&gt;summarize by that bucket.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;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.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;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 &amp;gt;= 1 and d &amp;lt;= 3, "1–3",
          else: if(d &amp;gt;= 4 and d &amp;lt;= 18, "4–18",
            else: if(d &amp;gt; 18 and d &amp;lt;= 32, "19–32",
              else: if(d &amp;gt;= 100, "100+",
                else: "33–99"
              )
            )
          )
        )
      )
    )

| summarize sum = sum(v), by: { bucket }
| sort bucket asc&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="t_pawlak_0-1769516717752.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/31756iA87B08E0657D3F6F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="t_pawlak_0-1769516717752.png" alt="t_pawlak_0-1769516717752.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;For real usage, replace the metric and Dimension with the actual metric key and dimension name from your dataset.&lt;BR /&gt;&lt;BR /&gt;And your second question, w&lt;SPAN&gt;hy does asLong(Dimension) return null.&lt;BR /&gt;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.&lt;BR /&gt;Since your dimension values are strings ("0", "1", …), asLong(Dimension) can evaluate to null.&lt;BR /&gt;Use toLong(Dimension) instead — it performs the actual conversion and allows numeric range comparisons (&amp;gt;=, &amp;lt;=) for bucketing.&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="t_pawlak_1-1769516850979.png" style="width: 400px;"&gt;&lt;img src="https://community.dynatrace.com/t5/image/serverpage/image-id/31757iA3D1F38710C55DFC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="t_pawlak_1-1769516850979.png" alt="t_pawlak_1-1769516850979.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jan 2026 12:27:59 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/293697#M3083</guid>
      <dc:creator>t_pawlak</dc:creator>
      <dc:date>2026-01-27T12:27:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using if condition in summarize</title>
      <link>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/293713#M3084</link>
      <description>&lt;P&gt;I usually use &lt;EM&gt;&lt;STRONG&gt;coalesce()&lt;/STRONG&gt;&lt;/EM&gt; funtion to construct these (switch/case)/(case/when) statements. Less brackets to type, no need for &lt;EM&gt;&lt;STRONG&gt;else:&lt;/STRONG&gt;&lt;/EM&gt; and easier to maintain:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| fieldsAdd bucket = coalesce(
  if(d == 0, "0"),
  if(d &amp;gt;= 1 and d &amp;lt;= 3, "1–3"),
  if(d &amp;gt;= 4 and d &amp;lt;= 18, "4–18"),
  if(d &amp;gt; 18 and d &amp;lt;= 32, "19–32"),
  if(d &amp;gt; 33 and d &amp;lt;= 99, "33–99"),
  if(d &amp;gt;= 100, "100+"),
  "NONUMERIC"
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In case set of possible vales are not numbers which can be organized in ranges, instead of OR operator &lt;STRONG&gt;&lt;EM&gt;in()&lt;/EM&gt;&lt;/STRONG&gt; function will provide much shorter syntax:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;  if(in(Dimension, {"dog","cat","frog"}), "pets"),&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 27 Jan 2026 16:09:58 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/293713#M3084</guid>
      <dc:creator>krzysztof_hoja</dc:creator>
      <dc:date>2026-01-27T16:09:58Z</dc:date>
    </item>
    <item>
      <title>Re: Using if condition in summarize</title>
      <link>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/295707#M3200</link>
      <description>&lt;P&gt;I believe the main issue was the use of&amp;nbsp;&lt;SPAN&gt;asLong() instead of toLong(), and&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;ended up implementing the second solution thanks to your clarification.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2026 16:08:14 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/Using-if-condition-in-summarize/m-p/295707#M3200</guid>
      <dc:creator>shaima</dc:creator>
      <dc:date>2026-03-05T16:08:14Z</dc:date>
    </item>
  </channel>
</rss>

