web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Query order by complex expression

(4) ShareShare
ReportReport
Posted on by 133
Hello fellow Dynamics users,
 
I hope you can help me building a query in Dynamics with a complex order by clause.
 
The query that I want to build (simplified version):
 
select * from SomeTable order by     case         when Field1 = Value1 then x        when Field2 = Value2 then y        else z    end
Now I haven't found a way of doing this with the QueryBuildDataSource class. The addOrderByCalculationField method sounds like a good start, but so far I haven't been able to find any documentation on how to use that. The function takes a Microsoft.Dynamics.AX.Analytics.CalculationModel.NumericExpression parameter, but that is an abstract class. And I haven't found any list of possible inheriting classes online.
 
Anyone who can point me in the right direction?
I have the same question (0)
  • Suggested answer
    Anthony Blake Profile Picture
    3,092 Super User 2026 Season 1 on at
    In your scenario you could have 3 queries, one covering each order by:
     
    Query 1: use Field1=Value1as criteria
    Query 2: use Field2=Value2 as criteria 
    Query 3: use Field1 != Value1 && Field2 != Value2 as criteria
     
    There could be repeated results as Field1 and Field2 could have overlapping results, but that's also an issue with your original query/problem.
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,801 Super User 2026 Season 1 on at
    Hi @,
     
    I guess you can create a view with the result of case.
     
    than you can order by your new field based the result.
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • Waed Ayyad Profile Picture
    9,089 Super User 2026 Season 1 on at
    Hi,
     
    Can you write what you want to achieve using SQL statement? Do you want to have order by have cases?
     
    Thanks,
    Waed Ayyad
  • FV@E Profile Picture
    133 on at
    Thank you all for your suggestions. What I'm trying to achieve is to create a slightly more complex ordering than simply ordering by 1 field. For example: I want my query to have 2 outer joins with other tables. Records where the first outer join matches should have the highest priority, records where the second outer join matches should have the second priority etc.
     
    I can build my query easily in SQL, that's not the problem. My problem is how to convert this syntax to x++;
     
    @Anthony: Yes I agree I could solve it by using multiple queries. However, due to performance I would prefer to do it in 1 query. Especially since I already know it can be done in SQL. I'm just not sure if I can generate the correct SQL from x++.
     
    @Mohammed Thank you for your suggestion, but I don't think I can capture this logic in a view
     
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 658

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 468 Super User 2026 Season 1

#3
Syed Haris Shah Profile Picture

Syed Haris Shah 333 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans