# Reuse Custom Metrics in DQL Without Reinitialisation

**URL:** <https://community.dynatrace.com/t/reuse-custom-metrics-in-dql-without-reinitialisation/52636>\
**Category:** DQL\
**Tags:** dql, dynatrace-managed\
**Created:** [July 24, 2025, 5:48pm UTC](https://community.dynatrace.com/t/reuse-custom-metrics-in-dql-without-reinitialisation/52636 "2025-07-24T17:48:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![knallapa](https://avatars.discourse-cdn.com/v4/letter/k/f1d935/32.png) [@knallapa](https://community.dynatrace.com/u/knallapa)\
**Post date:** [July 24, 2025, 5:48pm UTC](https://community.dynatrace.com/t/reuse-custom-metrics-in-dql-without-reinitialisation/52636/1 "2025-07-24T17:48:28Z")

</div>

I’m currently using this query to reuse the same custom metric at different time intervals to populate table data in my dashboard. However, I’d like to simplify the query and reuse a variable multiple times with different conditions in DQL. Is there a better way to structure this?

timeseries METRIC\_A\_approved = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeA” AND  
(responseCode == “0” OR responseCode == “00”) AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }

| join [  
timeseries METRIC\_A\_total = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeA” AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_A\_total }, kind: outer

| join [  
timeseries METRIC\_B\_approved = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeB” AND  
(responseCode == “0” OR responseCode == “00”) AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_B\_approved }, kind: outer

| join [  
timeseries METRIC\_B\_total = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeB” AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_B\_total }, kind: outer

| join [  
timeseries METRIC\_B\_approved\_subgroup = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeB” AND  
(responseCode == “0” OR responseCode == “00”) AND  
(categoryCode in [“1001”, “1002”, “1003”, “1004”, “1005”]) AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_B\_approved\_subgroup }, kind: outer

| join [  
timeseries METRIC\_B\_total\_subgroup = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeB” AND  
(categoryCode in [“1001”, “1002”, “1003”, “1004”, “1005”]) AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_B\_total\_subgroup }, kind: outer

| join [  
timeseries METRIC\_B\_approved\_domestic = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeB” AND  
(responseCode == “0” OR responseCode == “00”) AND  
categoryCode == “2001” AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_B\_approved\_domestic }, kind: outer

| join [  
timeseries METRIC\_B\_total\_domestic = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeB” AND  
categoryCode == “2001” AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_B\_total\_domestic }, kind: outer

| join [  
timeseries METRIC\_B\_approved\_crossborder = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeB” AND  
(responseCode == “0” OR responseCode == “00”) AND  
categoryCode == “2002” AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_B\_approved\_crossborder }, kind: outer

| join [  
timeseries METRIC\_B\_total\_crossborder = sum(  
`custom.app-metrics.transaction.data`,  
scalar: true,  
filter: {  
txType == “TypeB” AND  
categoryCode == “2002” AND  
(isNull($Entity) OR $Entity == “” OR entity == $Entity)  
}  
), by: { entity }  
], on: { entity }, fields: { METRIC\_B\_total\_crossborder }, kind: outer

| fieldsAdd  
Rate\_A = if(coalesce(METRIC\_A\_total, 0) == 0, 0, 100 \* coalesce(METRIC\_A\_approved, 0) / coalesce(METRIC\_A\_total, 0)),  
Rate\_B = if(coalesce(METRIC\_B\_total, 0) == 0, 0, 100 \* coalesce(METRIC\_B\_approved, 0) / coalesce(METRIC\_B\_total, 0)),  
Rate\_B\_subgroup = if(coalesce(METRIC\_B\_total\_subgroup, 0) == 0, 0, 100 \* coalesce(METRIC\_B\_approved\_subgroup, 0) / coalesce(METRIC\_B\_total\_subgroup, 0)),  
Rate\_B\_domestic = if(coalesce(METRIC\_B\_total\_domestic, 0) == 0, 0, 100 \* coalesce(METRIC\_B\_approved\_domestic, 0) / coalesce(METRIC\_B\_total\_domestic, 0)),  
Rate\_B\_crossborder = if(coalesce(METRIC\_B\_total\_crossborder, 0) == 0, 0, 100 \* coalesce(METRIC\_B\_approved\_crossborder, 0) / coalesce(METRIC\_B\_total\_crossborder, 0))

| filter not(isNull(entity) or entity == “”)  
| fieldsAdd  
Entity = entity,  
A.Total = toLong(coalesce(METRIC\_A\_total, 0)),  
A.Rate = concat(toString(round(coalesce(Rate\_A, 0), decimals: 2)), “%”),  
B.Total = toLong(coalesce(METRIC\_B\_total, 0)),  
B.Rate = concat(toString(round(coalesce(Rate\_B, 0), decimals: 2)), “%”),  
B.Total.Sub = toLong(coalesce(METRIC\_B\_total\_subgroup, 0)),  
B.Rate.Sub = concat(toString(round(coalesce(Rate\_B\_subgroup, 0), decimals: 2)), “%”),  
B.Total.Dom = toLong(coalesce(METRIC\_B\_total\_domestic, 0)),  
B.Rate.Dom = concat(toString(round(coalesce(Rate\_B\_domestic, 0), decimals: 2)), “%”),  
B.Total.XB = toLong(coalesce(METRIC\_B\_total\_crossborder, 0)),  
B.Rate.XB = concat(toString(round(coalesce(Rate\_B\_crossborder, 0), decimals: 2)), “%”)

| fields Entity, A.Total, A.Rate, B.Total, B.Rate, B.Total.Dom, B.Rate.Dom, B.Total.XB, B.Rate.XB, B.Total.Sub, B.Rate.Sub  
| sort A.Rate desc

---

<div class="post-metadata">

**Author:** ![Maheedhar\_T](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/maheedhar_t/32/155_2.png) [@Maheedhar\_T](https://community.dynatrace.com/u/Maheedhar_T)\
**Post date:** [July 24, 2025, 7:23pm UTC](https://community.dynatrace.com/t/reuse-custom-metrics-in-dql-without-reinitialisation/52636/2 "2025-07-24T19:23:13Z")

</div>

Hi&nbsp;[@knallapa](https://community.dynatrace.com/user/87812)&nbsp;,  
You can set up the common thing in all queries as variable and reference it in dashboard tiles.  
[Reference: Add a variable to a dashboard — Dynatrace Docs](https://docs.dynatrace.com/docs/analyze-explore-automate/dashboards-and-notebooks/dashboards-new/components/dashboard-component-variable)  
You can add as many variables as you want to. Maybe set up one variable for totalTransactions then use that in calculations. Set up a constant timeframe for that variable and let the other tiles have dashboard timeframe.

Regards,  
[@Maheedhar\_T](https://community.dynatrace.com/user/76275)

---

<div class="post-metadata">

**Author:** ![knallapa](https://avatars.discourse-cdn.com/v4/letter/k/f1d935/32.png) [@knallapa](https://community.dynatrace.com/u/knallapa)\
**Post date:** [July 24, 2025, 7:31pm UTC](https://community.dynatrace.com/t/reuse-custom-metrics-in-dql-without-reinitialisation/52636/3 "2025-07-24T19:31:20Z")

</div>

Sure, I will try in that direction. Thankyou&nbsp;[@Maheedhar\_T](https://community.dynatrace.com/user/76275)

---

<div class="post-metadata">

**Author:** ![knallapa](https://avatars.discourse-cdn.com/v4/letter/k/f1d935/32.png) [@knallapa](https://community.dynatrace.com/u/knallapa)\
**Post date:** [July 31, 2025, 4:22am UTC](https://community.dynatrace.com/t/reuse-custom-metrics-in-dql-without-reinitialisation/52636/4 "2025-07-31T04:22:37Z")

</div>

For Above Query Nothing worked for me: Tried variables, Problems i faced is :

- can’t fetch a metric once and reuse it with conditional logic applying filters.
- can’t assign a timeseries result to a variable inside fetch.
- can’t use let to define reusable filters inside fetch.

Moreover variables have limits. other issue was variables i can’t extend and apply filter conditions to that variable.
