Getting the count of records or averaging is one of the most commonly used Aggregate Functions in programming. FetchXML queries too facilitate aggregation. So here’s how you can utilize the same in a Flow using Common Data Service (Current Environment) connector [Because I’m waiting for it to be renamed to Dataverse Connector yet 😊]
To know about Fetch XML aggregation, here’s the Microsoft Docs link for the same – https://docs.microsoft.com/en-gb/powerapps/developer/data-platform/use-fetchxml-aggregation?WT.mc_id=DX-MVP-5003911
Let’s assume, you want to get the count of records from a Common Data Service (Current Environment) connector. Here’s how you can do the same using existing Aggregate Functions provided by Fetch XML.
Disclaimer: This is of course, not he only way to get get the aggregate functions, you can implement custom logic after you’ve retrieved all the data as well.
- Here’s the Fetch XML I’ll be using to retrieve all the Accounts from the Dataverse environment.
I’m using List Rows action from the connector which is this
And this is the query which I generated from the Advanced Find in D365 CE
- Below are the changed I must make to work with Aggregates in Fetch XML.
in the <fetch>, I’ll set aggregate=”true”
And the columns which I’m using the Aggregate function on, I’ll mention the aggregate=”[Aggregate]” alias=”[AliasName]”
- Now, this query along won’t run and you’ll get the below error –
“An attribute can not be requested when an aggregate operation has been specified and its neither groupby not aggregate. NodeXml: [FirstAttributeInQuery]“
- The reason being, since you are using Aggregate, only the columns on which aggregates are applied must exist. Hence, you’ll need to remove the other attributes which don’t have aggregate applied to them.
- And the workable FetchXML will now look like this
- When you run this, these are the results you’ll get in which you’ll have the aggregate value.
- Observe the same below
Parse JSON to read the aggregate
Now, since you’ve got the aggregated results. You can do an extra step to read the value. There are several ways to contain this, but here’s a quick example of how I did it –
- Declare a variable. It must be outside of a For Each at all times.
- And in the For Each, because I’m selecting the Array inside the value attribute in the Fetch XML results, I can then use the sample data to generate the schema and use it. The loop will anyway run only once.
- And I’ll set the variable below
- And here’s the final result once you run it. Your scenario of usage may vary.
Hope this was useful!
Here’s a YouTube video I made to summarize this example –
Here are some more Power Automate / Flow posts you might want to check –
- Invalid type. Expected Integer but got Number error in Parse JSON – Error at runtime after generating Schema | Power Automate
- Secure Input/Output in Power Automate Run History
- Validate JSON Schema for HTTP Request trigger in a Flow and send Response | Power Automate
- Parsing Outputs of a List Rows action using Parse JSON in a Flow | Common Data Service (CE) connector
- Trigger Conditions not working in a Cloud Flow? Here’s Why | Power Automate Quick Tip
- Read OptionSet Labels from CDS/Dataverse Triggers or Action Steps in a Flow | Power Automate
- Asynchronous HTTP Response from a Flow | Power Automate
- FormatDateTime function in a Flow | Power Automate
- Tag a User in a Microsoft Teams post made using Power Automate
- Converting JSON to XML and XML to JSON in a Flow | Power Automate
- Office 365 Outlook connector in Cloud Flows showing Invalid Connection error | Power Automate
- Create a Team, add Members in Microsoft Teams upon Project and Team Members creation in PSA / Project Operations | Power Automate