How to write query for fetching count of entity from Dataverse

Priya Jha 896 Reputation points
2025-02-05T12:27:10.1566667+00:00

Hi All,

I have a linked service connecting to Dataverse and i am trying to fetch count of an entity/table via lookup activity.

What query should i write to fetch only table count of an entity?

image

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,215 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Chandra Boorla 7,960 Reputation points Microsoft Vendor
    2025-02-06T02:20:58.6366667+00:00

    Hi @Priya Jha

    Greetings & Welcome to Microsoft Q&A forum! Thanks for posting your query!

    To fetch the count of records from a Dataverse entity using a Lookup activity in Azure Data Factory, you can utilize a FetchXML query with aggregation. Below is the FetchXML query and important considerations.

    FetchXML Query Structure:

    <fetch aggregate='true'>
      <entity name='<<tableName>>'>
        <attribute name='<<primaryKeyAttribute>>' alias='count' aggregate='countcolumn' />
      </entity>
    </fetch>
    

    Explanation:

    <fetch aggregate='true'>: This indicates that the query is for aggregation.

    <entity name='<<tableName>>'>: Replace <<tableName>> with the actual name of your Dataverse entity (table).

    <attribute name='<<primaryKeyAttribute>>' alias='count' aggregate='countcolumn' />:

    • Replace <<primaryKeyAttribute>> with the name of the primary key attribute of your entity. This is crucial for counting.
    • alias='count' gives the resulting count column a name "count".
    • aggregate='countcolumn' specifies that we want to count the values in the specified attribute.

    For additional information, please refer: Aggregate data using FetchXml

    Example:

    For instance, if you want to count the records in the "accounts" entity, where the primary key attribute is "accountid", your FetchXML query would look like this:

    <fetch aggregate='true'>
      <entity name='accounts'>
        <attribute name='accountid' alias='count' aggregate='countcolumn' />
      </entity>
    </fetch>
    

    Output - The Lookup activity will return a single row with a column named "count", which contains the total number of records in the specified entity. You can reference this count in subsequent activities in your pipeline.

    Important Considerations:

    • Primary Key Attribute - Ensure you are using the correct primary key attribute for the entity you are querying.
    • Entity Logical Name - Use the logical name of the entity (e.g., "accounts") rather than the display name.
    • Testing the Query - It’s a good practice to test the query using tools like the XrmToolBox FetchXML Builder to verify it returns the expected count.
    • Performance - Using FetchXML with aggregation is efficient as it retrieves only the count rather than all records.

    By following these steps and considerations, you can effectively retrieve the count of records from a Dataverse entity using a Lookup activity in Azure Data Factory.

    I hope this information helps. Please do let us know if you have any further queries.


    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.