# Conditional DQL sort

**URL:** <https://community.dynatrace.com/t/conditional-dql-sort/48500>\
**Category:** DQL\
**Tags:** dql\
**Created:** [October 23, 2024, 1:37pm UTC](https://community.dynatrace.com/t/conditional-dql-sort/48500 "2024-10-23T13:37:55Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![sinisa\_zubic](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/sinisa_zubic/32/32498_2.png) [@sinisa\_zubic](https://community.dynatrace.com/u/sinisa_zubic)\
**Post date:** [October 24, 2024, 7:57am UTC](https://community.dynatrace.com/t/conditional-dql-sort/48500/3 "2024-10-24T07:57:24Z")

</div>

hi&nbsp;[@marina\_pollehn](https://community.dynatrace.com/user/2456)

I see here 2 ways of solving this. If you really want to rely on one variable, then you have to execute two queries combined with an append. Always one of the queries will use a filter condition which is false for every record.

```markup
//desc sorting
fetch logs, from: $dt_timeframe_from, to:$dt_timeframe_to
| filter timestamp >= if($aggregation == "max", astimestamp($dt_timeframe_from), else: now()+1d)
| sort timestamp desc

//asc sorting
| append [
  fetch logs, from: $dt_timeframe_from, to:$dt_timeframe_to
| filter timestamp >= if($aggregation == "min", astimestamp($dt_timeframe_from), else: now()+1d)
| sort timestamp asc
]

```

Or you introduce a new Variable called direction with values “descending” and “ascending”. Then you could use it like following:

```markup
fetch logs
| sort timestamp, direction: $direction

```

Best,  
Sini

---

_[View the full topic](https://community.dynatrace.com/t/conditional-dql-sort/48500)._
