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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

Adding an OR range in a query based on 2 different table fields

(1) ShareShare
ReportReport
Posted on by 8
Hello,
 
I have to add a certain range in a query object like the following:
 
((Table1.Field1 == 0 AND Table2.Field2 == valueX) OR (Table1.Field1 == 1 OR Table2.Field2 == valueY))
 
Does anyone know how this can be achieved?
I have the same question (0)
  • Layan Jwei Profile Picture
    8,282 Super User 2026 Season 2 on at
    Hi Jason,

    is this an AOT query or x++ query? and where are you using this query?

    Thanks,
    Layan Jweihan
  • Jason0810M Profile Picture
    8 on at
    Hello Layan,
    It is an x++ query.
    I have created a custom view to get the fields from the two tables in one entity.
    Technically, I have now a view XXXItemView with fields IsCalculated, DateFrom and DateTo (and ItemdRecId to do the join).
    I have added the custom view XXXItemView to my query by code.
    I need to filter records as follows :
     
    date calculatedDate = some calculated date as per a custom method;

    ((XXXItemView.IsCalculated == 0 && XXXItemView.DateFrom <= utcNow() && XXXItemView.DateTo >= utcNow()) || (XXXItemView.IsCalculated == 1 && XXXItemView.DateFrom <= calculatedDate && XXXItemView.DateTo >= calculatedDate)
  • Suggested answer
    Layan Jwei Profile Picture
    8,282 Super User 2026 Season 2 on at
    Hi Jason,

    How is the calculatedDate filled? can you show us the logic in the custom method? to be able to tell if we can do the range using a computed column or not.
    And how did you add your view to a query by code can you show us please? 

    but if let's say we can't. and if I understood you correctly, then maybe try sth like this:
     
    QueryBuildDataSource queryBuildDataSource = query.dataSourceTable((tablenum(XXXItemView))); 
    queryBuildDataSource.name('XXXItemView');
    queryBuildDataSource.addRange(fieldNum(XXXItemView, IsCalulated)).value
                ( 
                    strFmt('(((%1.%2 == %3)&&(%1.%4 <= %5)&&(%1.%6 >= %5))||((%1.%2 == %7) && (%1.%4 <= %8) && (%1.%6 >= %8)))',
                    queryBuildDataSource.name(),
                    fieldStr(XXXItemView, IsCalulated),
                    0,
                    fieldStr(XXXItemView,DateFrom),
                    utcNow(),
                    fieldStr(XXXItemView,DateTo),
                    1,
                    calculatedDate)
                );
    
     

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

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

#1
SajeedMullaji Profile Picture

SajeedMullaji 698

#2
Martin Dráb Profile Picture

Martin Dráb 327 Most Valuable Professional

#3
CU10121822-0 Profile Picture

CU10121822-0 320

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans