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

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Creating a segment with an OR condition between two related entities

(3) ShareShare
ReportReport
Posted on by 32
I'm working on some segments and I'm running into some unintuitive segment evaluation behaviour. Can someone help me a) understand how the segment is evaluating and b) suggest a different approach?
 
It seems like if you have 2 different conditions about related tables using an OR connector, it only pulls in contacts that are related to a record from both of those tables, even if they meet one of the conditions.  
 
To illustrate, suppose contacts can be related to 0, 1, or many leads. Each lead must be related to a single contact. Contacts can also be related to 0, 1 or many products, and each product must be related to a single contact. Products have a 'price' and lead's have an 'expected price' attribute. 
Suppose I wanted a segment of contacts where
The contact was created in the last week
AND 
They are either related to a product with price > $100 OR they have a lead with an expected price > $100. 
 
I'd create it like this (some conditions in the picture are doctored to simplify the example, I've recreated it with my company's own tables to confirm). 
I would expect this to pull in contacts that had a lead with revenue > 100, even if they have not been related to a product. However, its only pulling in contacts that are related to both a lead and product, and they both have to meet the price criteria.
 
It behaves like this even if I put the OR conditions in a separate group. 
 
Can anyone explain this behaviour?
 
 
I have the same question (0)
  • Taavo T Profile Picture
    32 on at
    Creating a segment with an OR condition between two related entities
    Thanks Saif, wish I had you as my copilot for some of this segment work.
     
    Do you know if any of this behaviour is actually documented anywhere by MS? Best I can find is Segment query definition - Dynamics 365 Customer Insights | Microsoft Learn which seems out of date, or different from how segment queries are structured in both outbound and realtime marketing. 
  • Saif Ali Sabri Profile Picture
    2,346 Super User 2025 Season 2 on at
    Creating a segment with an OR condition between two related entities

    The behavior you're experiencing in Microsoft Dynamics is due to how the system evaluates segments with conditions across related entities, especially when using OR logic. Let’s break this down and provide a solution.


    Why This Happens

    When you create a segment with conditions across related entities (e.g., Leads and Products) using an OR connector, Dynamics evaluates the conditions in a way that requires the contact to be related to both entities (Leads and Products) to satisfy the query. This is because the system is performing an INNER JOIN-like operation behind the scenes, which only includes records that have relationships in both tables.

    In your case:

    • The segment is only returning contacts that are related to both a Lead and a Product, even though you used an OR condition.

    • This happens because the system is trying to evaluate both conditions simultaneously, and it requires the contact to have relationships in both tables to proceed.


    How to Fix This

    To achieve the desired behavior (contacts that meet either condition), you need to structure your segment differently. Here's a step-by-step approach:

    Option 1: Use Separate Queries and Combine Results

    1. Create Two Separate Segments:

      • Segment 1: Contacts created in the last week AND related to a Product with Price > $100.

      • Segment 2: Contacts created in the last week AND related to a Lead with Expected Price > $100.

    2. Combine the Results:

      • Use a Union operation to combine the results of the two segments. This will give you a list of contacts that meet either condition.

      Unfortunately, Dynamics does not natively support unions in segments, so you may need to export the results of both segments and combine them manually (e.g., in Excel or Power BI).


    Option 2: Use a Custom Workflow or Plugin

    If you need this functionality frequently, consider creating a custom workflow or plugin to handle the logic:

    1. Write a query that retrieves contacts created in the last week.

    2. For each contact, check if they are related to:

      • A Product with Price > $100 OR

      • A Lead with Expected Price > $100.

    3. Add the contact to the segment if they meet either condition.

    This approach requires development expertise but provides a more automated and scalable solution.


    Option 3: Use FetchXML for Advanced Queries

    If you have access to FetchXML, you can write a custom query to achieve the desired logic. FetchXML allows you to define more complex queries, including OR conditions across related entities.

    Example FetchXML:

    xml
    Copy
    <fetch>
      <entity name="contact">
        <filter type="and">
          <condition attribute="createdon" operator="last-x-days" value="7" />
          <filter type="or">
            <condition entityname="product" attribute="price" operator="gt" value="100" />
            <condition entityname="lead" attribute="expectedprice" operator="gt" value="100" />
          </filter>
        </filter>
      </entity>
    </fetch>

    You can execute this FetchXML query using a tool like the XrmToolBox or a custom plugin.


    Why This Works

    • Separate Queries: By splitting the conditions into separate segments, you avoid the issue of Dynamics requiring relationships in both tables.

    • Custom Logic: Using FetchXML or a plugin allows you to define the exact logic you need, bypassing the limitations of the segment builder.


    Summary

    The behavior you're seeing is due to how Dynamics evaluates OR conditions across related entities. To resolve this:

    1. Use separate segments and combine the results manually.

    2. Implement a custom workflow or plugin for more advanced logic.

    3. Use FetchXML to define a custom query.

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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 210

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 77 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans