Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

FetchXml Distinct Aggregate not working

(1) ShareShare
ReportReport
Posted on by

Hi,

I'm trying to get the number of Service Activities with Distinct Clients, however the FetchXml gives the same number back whether distinct or not. We already have sample data with the same clients on different Service Activities.

<fetch version="1.0" output-format="xml-platform" mapping="logical" aggregate="true">
  <entity name="serviceappointment">
    <attribute name="new_client" alias="Count" aggregate="count" distinct="true" />
  </entity>
</fetch>
<fetch version="1.0" output-format="xml-platform" mapping="logical" aggregate="true">
  <entity name="serviceappointment">
    <attribute name="new_client" alias="Count" aggregate="count" />
  </entity>
</fetch>

They both return the same number. Am I missing something?

*This post is locked for comments

  • Verified answer
    Community Member Profile Picture
    on at
    RE: FetchXml Distinct Aggregate not working

    Ok so it works. You need to specifically add aggregate='true' at the header and aggregate='countcolumn'.

    You can even add a groupby as below if needed.

    <fetch version="1.0" output-format="xml-platform" mapping="logical" aggregate="true" distinct="true">
      <entity name="serviceappointment">
        <attribute name="new_group" alias="Group" groupby="true" />
        <attribute name="new_client" alias="Count" aggregate="countcolumn" distinct="true" />
      </entity>
    </fetch>

    Thanks everybody

  • Community Member Profile Picture
    on at
    RE: FetchXml Distinct Aggregate not working

    Unfortunately, I tried that and it doesn't work. They always return the same count of 60 whether it's distinct or not. And I do have multiple entities with same contacts.

    I've tried it on different entities and fields but it doesn't work. I tried it in code and using XRMToolbox's FetchXML Tester and Advanced FetchXml Tester. Is this a Dynamics 365 issue?

  • Suggested answer
    Preeti Sharma Profile Picture
    2,678 on at
    RE: FetchXml Distinct Aggregate not working

    Hi Jarren,
    Try to add distinct on first row of fetch as below:

    <fetch version="1.0" output-format="xml-platform" mapping="logical" aggregate="true"
    distinct="true">
    <entity name="serviceappointment">
    <attribute name="new_client" alias="Count" aggregate="count" />
    </entity>
    </fetch>

    Hope this helps :)
  • Community Member Profile Picture
    on at
    RE: FetchXml Distinct Aggregate not working

    Hi Rawish,

    I originally based my Fetch XML on this, problem was that it there was no difference to them.

  • Community Member Profile Picture
    on at
    RE: FetchXml Distinct Aggregate not working

    Hi Francesco,

    This doesn't seem to work. Just to confirm, if I have 3 Service Activities, 2 with Client A and 1 with Client B the New_Client_Count will be 2 right?

    Right now it's still showing 3 for this query and even if I attach distinct="true" on it.

  • Suggested answer
    Rawish Kumar Profile Picture
    13,758 on at
    RE: FetchXml Distinct Aggregate not working

    Hi Jarren,

    Addition to francesco's comment, 

    This should probably answer your question :

    4314.fetchxml.PNG

    mark my anwer as verified if helpful.

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: FetchXml Distinct Aggregate not working

    Hi Jarren,

    try the following:

    <fetch distinct='true' mapping='logical' aggregate='true'>
    <entity name='serviceappointment'>
    <attribute name='new_client' alias='new_client_count' aggregate='count' />
    </entity>
    </fetch>

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans