# Replace null Values with previous non-Null value in an array

**URL:** <https://community.dynatrace.com/t/replace-null-values-with-previous-non-null-value-in-an-array/51347>\
**Category:** DQL\
**Tags:** dql\
**Created:** [April 23, 2025, 7:08pm UTC](https://community.dynatrace.com/t/replace-null-values-with-previous-non-null-value-in-an-array/51347 "2025-04-23T19:08:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SimGiants92](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@SimGiants92](https://community.dynatrace.com/u/SimGiants92)\
**Post date:** [April 23, 2025, 7:08pm UTC](https://community.dynatrace.com/t/replace-null-values-with-previous-non-null-value-in-an-array/51347/1 "2025-04-23T19:08:59Z")

</div>

I have arrays that contain a lot of null values but i would like to replace them not with a single value but instead with whatever previous value in the array is not null, is this possible in DQL?

---

<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:** [April 23, 2025, 8:53pm UTC](https://community.dynatrace.com/t/replace-null-values-with-previous-non-null-value-in-an-array/51347/2 "2025-04-23T20:53:45Z")

</div>

Unfortunately we do not have such array function yet, but there is a trick which can be used if you know maximum number of consecutive nulls in your array.

If you can have max 2 nulls in row, query could look like this:

```markup
data record(a=array(1.0 ,null, 2.5, 3.0, null, null, 1.1))

| fieldsAdd a = if( isNotNull(a[]), a[], else: arrayMovingMin(a, 2)[] )
| fieldsAdd a = if( isNotNull(a[]), a[], else: arrayMovingMin(a, 2)[] )

```

If more, you need to repeat last command more times.

 ![krzysztof_hoja_0-1745441525970.png](https://global.discourse-cdn.com/dynatrace/original/2X/0/089a28e43c48b0da6c78850f47253f25adcd6d7b.png)

but:

 ![krzysztof_hoja_1-1745441560325.png](https://global.discourse-cdn.com/dynatrace/original/3X/6/6/660b86a943623c0a434c5428e063358d94bf361e.png)

---

<div class="post-metadata">

**Author:** ![SimGiants92](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@SimGiants92](https://community.dynatrace.com/u/SimGiants92)\
**Post date:** [April 24, 2025, 1:30pm UTC](https://community.dynatrace.com/t/replace-null-values-with-previous-non-null-value-in-an-array/51347/3 "2025-04-24T13:30:03Z")

</div>

Thanks for the answer, I can’t know how many consecutive NULL i will have ahead of time but at least i have an idea of a different solution and i know the function does not exist

---

<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:** [April 25, 2025, 6:46pm UTC](https://community.dynatrace.com/t/replace-null-values-with-previous-non-null-value-in-an-array/51347/4 "2025-04-25T18:46:52Z")

</div>

Friend showed me this solution not having this limitation:

```markup
data record(a=array(1.0 ,null, 2.5, 3.0, null, null, null, 1.1))

| fieldsAdd va=record(a=a[], i=iIndex())
| expand va
| fieldsAdd fixedValue = if(isNotNull(va[a]), va[a], else: arrayLast(arrayRemoveNulls( iCollectArray( if(iIndex() < va[i], a[])))))

| summarize a=collectArray(record(va[i],a=fixedValue))
| fieldsAdd a=arraySort(a, direction:"ascending")
| fieldsAdd a=a[][a]

```

 ![krzysztof_hoja_0-1745606603349.png](https://global.discourse-cdn.com/dynatrace/original/3X/3/6/36f49aa5f642e69b34fec3f551ebef0d86a14e66.png)

credits go to&nbsp;[@AustinSabel](https://community.dynatrace.com/user/86520)

[@SimGiants92](https://community.dynatrace.com/user/87981)&nbsp;, can you tell us more about the need behind this operation? In what situation do you need it. I think there may be a value in having dedicated utility function doing it in one step.

---

<div class="post-metadata">

**Author:** ![SimGiants92](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@SimGiants92](https://community.dynatrace.com/u/SimGiants92)\
**Post date:** [April 25, 2025, 6:52pm UTC](https://community.dynatrace.com/t/replace-null-values-with-previous-non-null-value-in-an-array/51347/5 "2025-04-25T18:52:43Z")

</div>

Hello,  
Thanks for the answer!  
In my case it is specifically for anomaly detection, i want to trigger an alert if every datapoint is below a certain threshold for an hour. But if some of the values are NULL within that hour the alert does not trigger. I can just assume NULL values are in breach of the threshold by forcing them to be a specific value, but then it gives a graph going up and down (but it still works)
