split('string',',')[sub(max(length(split('string',',')),2),2)]
An explanation:
An integer number in square brackets after an array will select the corresponding item starting at 0. E.g createArray('a','b','c')[0] = a
In your formula you calculate the index of the second to last item by using lengh() to get the length of the array then subtracting two from it (because length() measures the array starting at 1 but the index in square brackets starts at 0 and you want to get the second to last item, so count back 2 from the total length.
To protect the sub() function from returning a negative number if your array is only 1 item in length (there was no delimiter to split on) use max() to set a "floor" of 2 to the number being subtracted from, so in that instance 2-2 = 0 and you would get item 0 from the array (the first and last item) rather than trying to get item -1 which is non existent and would fail..
------------------------------
Will Page
Technical consultant
Christchurch, NZ
------------------------------
Original Message:
Sent: Aug 17, 2021 01:44 PM
From: Nicholas McMillan
Subject: Selecting a single string from an array of strings in power automate flow
I'm trying to select a single string from an array of strings. First, I am using the split function to split a string. Then, I want to select the string that is second to the last in the array once the original string is split. The firstN() and last() functions together would work except that firstN() is not available in Power Automate. I am trying to set a variable equal to the second to last string in an array. Do you know how I can set a variable equal to the second to the last string in an array?
------------------------------
Nicholas McMillan
------------------------------