Best way to get the value of the last (or any) element of an array?

felixstraube

Well-known Member
Joined
Nov 27, 2023
Messages
897
Office Version
  1. 365
Platform
  1. Windows
  2. Web
I was working on a formula and I needed to get the value of the last item of an array to create a sequence from 1 to that value. Something like this:

Book1
AB
1ArrayNo the result i wanted
251
310
415
Sheet1
Cell Formulas
RangeFormula
B2B2=SEQUENCE(CHOOSEROWS(A2:A4, -1))


but instead of getting an array of 15 numbers i got the "1" as you see in the example, which let me scratching my head at first.
It took me some time to realize that CHOOSEROWS actually returns an array of only one item, ({15}), instead of the value itself (15). And feeding that into SEQUENCE didn't work.
Then I used INDEX as a workaround like this:
Book1
ABC
1ArrayNo the result i wantedINDEX workaround
2511
3102
4153
54
65
76
87
98
109
1110
1211
1312
1413
1514
1615
Sheet1
Cell Formulas
RangeFormula
B2B2=SEQUENCE(CHOOSEROWS(A2:A4, -1))
C2:C16C2=SEQUENCE(INDEX(CHOOSEROWS(A2:A4,-1), 1))
Dynamic array formulas.

This worked and I continued with my formula.
Now the question is: What is the best way, or best practice, to get the actual value from one item of an array? Is there a better way than using INDEX?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Sorry, I didn't write it clearly.
The array could be the result of a FILTER function or any other that returns a dynamic array.
 
Upvote 0
You may be missing some parameters for SEQUENCE.. perhaps one of these:

Edit: Nvm about the missing parameters part. I see SEQUENCE will assume starting value of 1, and defaults to 1 column.

Book1
ABCDEFGHIJKLMNOP
1ArrayResult
25123456789101112131415
3101
4152
53
64
75
86
97
108
119
1210
1311
1412
1513
1614
1715
Sheet1
Cell Formulas
RangeFormula
B2:P2B2=SEQUENCE(1,TAKE(A2:A4,-1),1)
B3:B17B3=SEQUENCE(TAKE(A2:A4,-1),1,1)
Dynamic array formulas.
 
Upvote 0
TAKE returns the value, not an array of only one item, and feeding it to SEQUENCE it works fine, i get my sequence.
What if I want the penultimate item?
Which of this options do you think is the best? Is there another, better, cleaner way?

Book1
ABCD
1ArrayNo the result i wantedwith INDEXwith TAKE
25111
31022
41533
52044
655
766
877
988
1099
111010
121111
131212
141313
151414
161515
Sheet1
Cell Formulas
RangeFormula
B2B2=SEQUENCE(CHOOSEROWS(A2:A5, -2))
C2:C16C2=SEQUENCE(INDEX(CHOOSEROWS(A2:A5,-2), 1))
D2:D16D2=SEQUENCE(TAKE(TAKE(A2:A5, -2),1))
Dynamic array formulas.
 
Upvote 0
I'd use INDEX:

Excel Formula:
=SEQUENCE(INDEX(A2:A5,COUNT(A2:A5)-1), 1)
 
Upvote 1
Solution
Agreed with Rory, TAKE only looks at the first or last elements in an array. If the value you seek is in a different position, TAKE won't get you there.
 
Upvote 1

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top