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

Notifications

Announcements

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:
I have the same question (0)
  • 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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 525 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 324 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard