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

Notifications

Announcements

No record found.

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
    2,926 Super User 2025 Season 2 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,390 Super User 2025 Season 2 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,039 Super User 2025 Season 2 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans