cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Regular Visitor

How to get the second last element of an array

I am using a template "Email me a weekly summary of Office 365 service health notices" to get service health notifications. An array with 2  string parameters in it is used. The second one is the Published time that gives me the time service health update was published.  As the number of updates with respect to particular service is not fixed, the length of array is dynamic and the reason i am unable to pick the second last element.

 

Can i get an help asap.

6 REPLIES 6
Highlighted
Dual Super User III
Dual Super User III

Re: How to get the second last element of an array

Try the following formula. It gets the last two items in the array and then returns the first one.

first(skip(variables('ArrayName'),sub(length(variables('ArrayName')),2)))


-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
Highlighted
Community Support
Community Support

Re: How to get the second last element of an array

Hi @Varun1917 ,

 

If you want to get the PublishedTime contained in Message, please try the following Expression:

7.PNG

 

Best Regards,

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Highlighted
Regular Visitor

Re: How to get the second last element of an array

@Pstork1 Thanks for the quick response.

Just to confirm, as you said ""the exporession gets the last two items in the array and then returns the first one "" . It means it will return the second last item right ??

 

I tried but it is popping up as Expression is invalid.

 

Please help


@Pstork1 wrote:

Try the following formula. It gets the last two items in the array and then returns the first one.

first(skip(variables('ArrayName'),sub(length(variables('ArrayName')),2)))

 

Highlighted
Regular Visitor

Re: How to get the second last element of an array

Hi @v-bacao-msft ,

Thanks for the response.

 

However, i am getting the published time. The problem here is as there are multiple updates for a service degradation before it gets restored, everytime the updated Published time goes into an array. The final one is the last element of array and hence i am looking for the second last element of array for comparing with the last element.

 

Please help.


@v-bacao-msft wrote:

Hi @Varun1917 ,

 

If you want to get the PublishedTime contained in Message, please try the following Expression:

7.PNG

 

Best Regards,


 

Highlighted
Community Support
Community Support

Re: How to get the second last element of an array

Hi @Varun1917 ,

 

You could use the expression provided by @Pstork1 :

first(skip(body('Filter_array'),sub(length(body('Filter_array')),2)))

Image reference:

9.PNG

 

Best Regards,

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Highlighted
Dual Super User III
Dual Super User III

Re: How to get the second last element of an array

Yes, it will return the next to last item in the array.  Make sure you change the Arrayname to whatever variable you are storing the array in.  Otherwise the code should work.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Power Platform ISV Studio

Power Platform ISV Studio

ISV Studio is designed to become the go-to Power Platform destination for ISV’s to monitor & manage published applications.

Upcoming Events

Experience what’s next for Power Automate

See the latest Power Automate innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Top Solution Authors
Top Kudoed Authors
Users online (6,870)