cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
CJRV
Frequent Visitor

Last N items from array

I am trying to get the last N items from an array.

 

For example in the below I want to get the last two items in the list. I know I can use take() to get the first N items however how would I get the last ones?

 

My data does not contain a reference which I can use to reorder the list correctly, unless I am missing some kind of reverse command which I could possibly use (then run a take function on that)

 

Appreciate any ideas! Thanks

 

[
{
...(Other records)
{
"Ref": "100",
"First Name": "Nathan",
},
{
"Ref": "1356",
"First Name": "Anne",
},
{
"Ref": "1345",
"First Name": "Ben",

},
{
"Ref": "75",
"First Name": "Carly",
}
]

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @CJRV ,

 

The expression in the post mentioned by @VictorIvanidze should do the job with a little modification:

skip(variables('varData'), sub(length(variables('varData')), variables('N')))

 

where varData is your JSON array

and N is your N value.

 

Snag_9246115.png

ekarim2020_0-1656094660671.png

 


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

View solution in original post

6 REPLIES 6
VictorIvanidze
Community Champion
Community Champion

https://powerusers.microsoft.com/t5/Building-Flows/How-to-get-the-second-last-element-of-an-array/td...

--------------------------------------------------------------------------------------
Contact me if you are interested in custom Power Automate development.
CJRV
Frequent Visitor

Thanks Victor, I think that would return the 2nd to last item, but not the last N items (by N this might be the last 10 items for a larger array for example)

VictorIvanidze
Community Champion
Community Champion

Just test it yourself.

--------------------------------------------------------------------------------------
Contact me if you are interested in custom Power Automate development.
AbigailRRausch
Frequent Visitor

Hi, @CJRV! I think Victor is exactly right, and you can use the expression in that thread to base yours on. You'll just have to remove the first() part of the expression, which returns only the first element of the resulting array (whereas you want both of the last two elements.) Without the first() part, it should be spot on for you to retrieve the last two elements. (To spell it out, the formula will get the # of elements in the array, then skip all but the last two, which is exactly what you want.)

Hi @CJRV ,

 

The expression in the post mentioned by @VictorIvanidze should do the job with a little modification:

skip(variables('varData'), sub(length(variables('varData')), variables('N')))

 

where varData is your JSON array

and N is your N value.

 

Snag_9246115.png

ekarim2020_0-1656094660671.png

 


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

CJRV
Frequent Visitor

Yes that did work indeed! Thanks everyone for clarifying this 😀

Helpful resources

Announcements
 WHAT’S NEXT AT MICROSOFT IGNITE 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Register for a Free Workshop.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Users online (1,141)