web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Replace last comma wit...
Power Automate
Answered

Replace last comma with an & unless there is no last comma

(0) ShareShare
ReportReport
Posted on by 34

Hi all, 

I would really appreciate a bit of help with a compose action to change the last comma to an '&' unless there is no last comma. I have a flow that gets a list of events from a calendar we invite to any business meetings and an automated email goes out to share with our department our business meetings for the next week. The flow looks through each calendar event and pulls out the attendee names and stores that to a string variable. Before it stores that data to the string variable I do the following:

  1. Get the required attendees and options attendees, then do a concat to join them together
  2. I replace the domain name with a comma, I replace the full stop with a space, I remove the account that stores the meetings across our teams, I replace ';' with a ', ' (as some external meetings show like this) - This leaves me with Firstname Lastname, Firstname Lastname, Firstname Lastname, 
    Compose action (CleanAttendees): 
    trim(replace(replace(replace(replace(replace(outputs('JoinAttendees'),'@test.com;'','),'.'' '),'BRMMeetings,',''),',',', '),';',', '))
  3. I then remove the last comma, This leaves me with Firstname Lastname, Firstname Lastname, Firstname Lastname 
    Compose action (removelastcomma): 
    substring(outputs('CleanAttendees'),0,sub(length(outputs('CleanAttendees')),1))
  4. I then remove the last comma and replace with an '&', This leaves me with Firstname Lastname, Firstname Lastname & Firstname Lastname 
    Compose action (FinalNames): 
    concat(substring(outputs('removelastcomma'),0,lastIndexOf(outputs('removelastcomma'),',')), ' & ',slice(outputs('removelastcomma'),add(lastIndexOf(outputs('removelastcomma'),','),1)))

    This has been working perfectly fine for the last 6 weeks, although I hit an issue this week as someone put a placeholder in the diary, invited the calendar to the meeting and as it only had the organizer as the required attendee, there was no comma between two names as only one name exists. Therefore on the FinalNames compose action I got the error below

    Unable to process template language expressions in action 'FinalNames' inputs at line '0' and column '0': 'The template language function 'substring' parameters are out of range: 'start index' and 'length' must be non-negative integers and their sum must be no larger than the length of the string. Please see https://aka.ms/logicexpressions#substring for usage details.'.

    Is there a way to change the FinalNames compose action to deal with the scenarios where there is only one name? Essentially, if there is no comma then ignore. I have told the team only to include the calendar when business users have been added, but I know this mistake will happen again and cause the flow to fail, so would prefer to add the logic to deal with this. 

    Image of portion of flow below:
    BRMUpdateEmail&.PNG
Categories:
  • Verified answer
    Henderson87 Profile Picture
    83 on at

    Looks like you have a great thing going. A quick fix would be to use the control action 'condition'. And preform a condition on the attendees at the start of the flow. use the contains and search for the  "," :

    Henderson87_0-1684769437489.png

    Then you can handle the list separate of the single items.

     

  • Gowmadrid82 Profile Picture
    34 on at

    Thanks Henderson, appreicate it. I would need to add that condition to just before the 'FinalNames' compose action, as that is the only step where there would be no commas. That would definitely work and I would just duplicate the append to string action, but instead use the output of 'RemovedLastComma'. 

    I do try to steer away from conditions, only because I see them used in anger by most of our users and becomes really messy. Although for this scenario it works well as a quick solution. 

    I would still also be interested to know if there is a away to modify the expression of the FinalNames compose action to not try to replace the comma between the last two names with a '&', if no comma exists.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 245 Super User 2026 Season 2

#2
11manish Profile Picture

11manish 233 Super User 2026 Season 2

#3
Valantis Profile Picture

Valantis 136 Super User 2026 Season 2

Last 30 days Overall leaderboard