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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

FetchXML aggregation for two option fields

(0) ShareShare
ReportReport
Posted on by 10

Good Evening,

I'm trying to aggregate a two option field with the following test FetchXML:

   
      
      

But get the following error:

Aggregate MAX or MIN is not supported for attribute of type bit.

Essentially I need to find if any contact has 'field1' set to 'Yes'.  Is it possible to aggregate a two option field?

I have the same question (0)
  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at
    RE: FetchXML aggregation for two option fields

    Hi,

    Thank you for your query.

    As you can see by the error it is not possible Aggregate on bit (2 optionset) fields.

    However you can use DISTINCT operator.

    Here is the example FetchXML:

      
        
      
    

    Please note, distinct="true" added in the FechXML and it should return 2 records (at MAX).

    An alternated approach to your scenario is:

    • Retrieve Records where field1 is true.
    • Set Top to 1, so we get first record where field1 is true.
    • If the result count is greater than 0, you have your answer.

    For instance here is the example FetchXML:

       
          
          
            
        
          
    

    If there are any results returned from above query, we will get to know if there are any records where field1 is set to true.

    Here is example C# code for your reference:

    string qry = @"
                       
                          
                          
                            
                        
                          
                    ";
    
                FetchExpression fetchExpression = new FetchExpression(qry);
    
                var results = svc.RetrieveMultiple(fetchExpression);
                
                if (results.Entities.Count==1)
                {
                    // There are records where field1=true
                    return true;
                }

    Let me know if you need further clarification.

    Best,

    Wahaj

    (if this helps, please mark it as verified)

  • Tom13212 Profile Picture
    10 on at
    RE: FetchXML aggregation for two option fields

    Hi Wahaj,

    Thanks for your response, what would be the best way to structure a query that was aggregating across multiple fields rather than just one?

    Essentially I'm trying to create a query to determine whether any related contact has a field set to true for a number of fields, in SQL the equivalent query would be:

    SELECT id, MAX(field1), MAX(field2), MAX(field3), MAX(field4)
    FROM contacts
    GROUP BY id
    WHERE ...

  • cloflyMao Profile Picture
    25,210 on at
    RE: FetchXML aggregation for two option fields

    Hi Tom,

    SQL is now supported(and MAX function) to query CDS data(now it is renamed to Dataverse), try the new preview feature and check whether it could work for you:

    https://docs.microsoft.com/en-us/powerapps/developer/data-platform/dataverse-sql-query

    https://powerobjects.com/dynamics-365/use-sql-to-query-data-from-cds-and-dynamics-365-ce/

    Regards,

    Clofly

  • Suggested answer
    Wahaj Rashid Profile Picture
    11,321 on at
    RE: FetchXML aggregation for two option fields

    Hi,

    I believe you have to do multiple queries for each field and then implement your logic.

    For example:

    • Query field1 where value = 1 and store the result in a variable (havingField1 = true).
    • Query field2 where value = 1 and store the result in a variable (havingField2 = true).
    • Query field3 where value = 1 and store the result in a variable (havingField3 = true).
    • The implement your logic like if (havingField1 && havingField2 and havingField3).

    Otherwise, please look into Clofy's approach.

  • Wahaj Rashid Profile Picture
    11,321 on at
    RE: FetchXML aggregation for two option fields

    To make it easier for other readers, If any of the answers helped, please mark it as verified.

    (Select 'Yes' under 'Did this answer your question?')

  • JV_FLA Profile Picture
    22 on at
    RE: FetchXML aggregation for two option fields

    Wahaj, in your "alternated approach" why do you even need aggregation at all in the fetchXML for this? Is that really needed or just forgot to delete that out?

    <fetch distinct='false' mapping='logical' aggregate='true' top='1'>

      <entity name="contact">

         <attribute name="field1"/>

         <filter type="and">

           <condition attribute="field1" operator="eq" value="1" />

       </filter>

      </entity>  

    </fetch>

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Sahan Hasitha Profile Picture

Sahan Hasitha 284

#2
Sohaib Cheema Profile Picture

Sohaib Cheema 104 User Group Leader

#3
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 77

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans