Skip to main content

Notifications

Community site session details

Community site session details

Session Id : nZxlTIRUGHIdHbMeoeUsJ3
Power Automate - Building Flows
Unanswered

Build an xpath expression with sum()

Like (0) ShareShare
ReportReport
Posted on 9 May 2024 11:35:28 by 12

Hi there,

I am trying to build a flow with XPath in the 'Select'. When I try to add the sum() to the XPath below, I get 'an invalid expression' error.

 

This statement works!

xpath(outputs('Compose_XML'), concat('(//values[EmployeeNumber=',item(),']/Email)[1]/text()')))

 

This statement with the sum() doesn't work!

xpath(outputs('Compose_XML'), concat('(sum//values[EmployeeNumber=',item(),']/Email)[1]/text()')))

 

I appreciate any help!

 

Thanks in advance!

  • Expiscornovus Profile Picture
    31,643 Most Valuable Professional on 13 May 2024 at 08:37:52
    Re: Build an xpath expression with sum()

    Hi @chandlers201,

     

    Yes, I converted what is in the Compose (which will be text as output) to xml first, before using it in the Select action From field.

     

    I see you have a different xml schema now compared to what you shared earlier.

     

    Try and update the xpath expressions to use the names of your xml schema, below is an updated example.

     

    From field of the Select action

    xpath(xml(outputs('Compose_XML')), '//root/values')

     

    Account value in the Map field of the Select action

    join(xpath(item(), '//finops_accountid[1]/text()'), '')

     

    Amount value in the Map field of the Select action

    xpath(xml(outputs('Compose_XML')), concat('sum(//root/values[finops_accountid=', '''', join(xpath(item(), '//finops_accountid[1]/text()'), ''), '''', ']/finops_amount)'))

     

  • chandlers201 Profile Picture
    12 on 11 May 2024 at 17:31:21
    Re: Build an xpath expression with sum()

    Screenshot 2024-05-11 at 18.27.25.png

    xpath(xml(outputs('Compose_XML')),'sum(//values/finops_amount)')

    Screenshot 2024-05-11 at 18.30.17.png

  • chandlers201 Profile Picture
    12 on 11 May 2024 at 17:26:28
    Re: Build an xpath expression with sum()

    Thanks for this. I really appreciate it.

    join(xpath(item(), '//accountid[1]/text()'), '')

    I tried running this statement but I am getting an error. Does it need to be converted to XML?

     

    Screenshot 2024-05-11 at 18.15.29.png
    Screenshot 2024-05-11 at 18.24.36.png
  • Expiscornovus Profile Picture
    31,643 Most Valuable Professional on 10 May 2024 at 09:32:09
    Re: Build an xpath expression with sum()

    Hi @chandlers201,

     

    Can you try the below?

     

    From

    xpath(xml(outputs('Compose_XML')), '//root/values')

     

    Account

    join(xpath(item(), '//accountid[1]/text()'), '')

     

    Amount

    xpath(xml(outputs('Compose_XML')), concat('sum(//root/values[accountid=', '''', join(xpath(item(), '//accountid[1]/text()'), ''), '''', ']/amount)'))

     

    sumbydistinctaccount.png

     

    My test run

     

    sumamount_test.png

     

    You will also need to use a union after the select to remove the duplicates.

    union(body('Select'),body('Select'))

     

  • chandlers201 Profile Picture
    12 on 09 May 2024 at 16:16:41
    Re: Build an xpath expression with sum()

    Thanks for that. I am looking for the total amount per unique account.

     

     

  • Expiscornovus Profile Picture
    31,643 Most Valuable Professional on 09 May 2024 at 16:06:17
    Re: Build an xpath expression with sum()

    Hi @chandlers201,

     

    Thanks for sharing.

     

    Just to double check. Do you need the total amount of all accounts together? Or the total amount per unique account?

     

    In case of the first scenario you could just use the below in a Compose action.

     

    xpath(xml(outputs('Compose_XML')), 'sum(/root/values/amount)')

     

    sumamount_total.png

     

    Test run

     

    totaltest.png

  • chandlers201 Profile Picture
    12 on 09 May 2024 at 15:57:52
    Re: Build an xpath expression with sum()

    Hi there

    chandlers201_0-1715270217062.png

    using the sum() to add up the amount.

     

     

  • Expiscornovus Profile Picture
    31,643 Most Valuable Professional on 09 May 2024 at 13:31:16
    Re: Build an xpath expression with sum()

    Hi @chandlers201,

     

    Can you share the xml which is used in the Compose XML? Interested to see the schema of your xml. Obfuscate data if needed.

     

    And what are you trying to sum?

  • chandlers201 Profile Picture
    12 on 09 May 2024 at 13:07:12
    Re: Build an xpath expression with sum()

    Thanks for your response. I am using the 'Select' to access the list of accounts, (e.g. ACT001, ACT002, etc). In the XPath expression, I have used concat around the item() to return me each account. I am not sure how to build the XPath with the sum()? Do I need to use concat?

    chandlers201_0-1715259834769.png

     

  • Expiscornovus Profile Picture
    31,643 Most Valuable Professional on 09 May 2024 at 12:04:19
    Re: Build an xpath expression with sum()

    Hi @chandlers201,

     

    Afaik the sum function needs to have parentheses as well, which are missing in your example:

    https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256160(v=vs.100)

     

    Below is an example (based on Example 7 of the Microsoft docs, https://learn.microsoft.com/en-gb/azure/logic-apps/workflow-definition-language-functions-reference#xpath)

     

    xpath(xml(outputs('Compose_XML')), 'sum(/produce/item/count)')

     

    sumfunction_example.png

     

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,668 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,004 Most Valuable Professional

Leaderboard
Loading started