Using Dynamic Arrays to Replicate ROW_NUMBER function in SQL server

denzo36

Board Regular
Joined
Sep 22, 2019
Messages
218
Office Version
  1. 365
Platform
  1. Windows
Hi folks, this one has been stumping me. Apologies, I cannot download XLB2B as I'm not on my personal pc. Using Office 365.

For those familiar with T-SQL language, you will know that there is a very helpful function called ROW_NUMBER which allows you to partition data based on criteria. The most common use case for me is to identify in our transactional data, the first or last transaction that happened for every policy. My data is a list of policies and policyv ersions. So in my example below, a new 'policy version' is created for each policynumber when a new transaction is done, going up in increments of 1. So the same policynumber occurs, having mutiple policyversion going from 1 upwards. Below is just a snippet of the data

1700742709062.png


In T-SQL, ROW_NUMBER will then create a new column and label the rows based on what I specify in my partition and how I choose to order my data. Below is when I partition by policynumber and order by policyversion descending. I think then use an outer query to only select where RowNumber = 1

1700742808428.png



I'm trying to replicate the same functionality using dynamic arrays. I can use SORTBY to sort my data by policynumber and policyversion descending but I can't figure out how to then using that resultant array to filter to only take the highest policyversion. I think some combo of FILTER based on max policyversion or something along those lines.

Below is the desired result.

1700743024358.png


Any ideas?

Thanks in advance
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
How about
Excel Formula:
=LET(d,SORT(A2:B46,{1,2},{1,-1}),DROP(REDUCE("",UNIQUE(TAKE(d,,1)),LAMBDA(x,y,VSTACK(x,TAKE(FILTER(d,TAKE(d,,1)=y),1)))),1))
 
Upvote 0
Thanks Fluff, it's working as expected, thanks so much
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
I know it's a little late. But just for fun here is another option:

Row number function sql.xlsx
ABCDE
1PolicyNumberPolicyVersionP30036944453
2P30123328155P30038381962
3P301283873511P30123328159
4P30123328158P30127996166
5P30123328159P301283873514
6P301283873514
7P30127996164
8P301283873513
9P30127996166
10P30036944451
11P30036944452
12P30127996165
13P301283873510
14P30038381961
15P301283873512
16P30036944453
17P30123328157
18P30038381962
19P30123328156
20P30127996163
21
Hoja1
Cell Formulas
RangeFormula
D1:E5D1=LET(d, A2:B40, n, CHOOSECOLS(d,1), v, CHOOSECOLS(d,2), fn, FILTER(n,n<>""), fv, FILTER(v,v<>""), u, UNIQUE(fn), r, REDUCE("",u,LAMBDA(x,y,VSTACK(x,HSTACK(y,MAX(FILTER(fv,fn=y)))))), SORT(DROP(r,1),1) )
Dynamic array formulas.
 
Upvote 0
A little less complex.

Complex Formulas.xlsm
ABCDEF
1PolicyNumberPolicyVersionPolicyNumberMinMax
2P30123328155P300369444513
3P301283873511P300383819612
4P30123328158P301233281559
5P30123328159P301279961636
6P301283873514P30128387351014
7P30127996164
8P301283873513
9P30127996166
10P30036944451
11P30036944452
12P30127996165
13P301283873510
14P30038381961
15P301283873512
16P30036944453
17P30123328157
18P30038381962
19P30123328156
20P30127996163
1cc
Cell Formulas
RangeFormula
D2:D6D2=SORT(UNIQUE(rPN))
E2:E6E2=MINIFS(rPV,rPN,D2)
F2:F6F2=MAXIFS(rPV,rPN,D2)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
rPN='1cc'!$A$2:$A$20D2, E2:F6
rPV='1cc'!$B$2:$B$20E2:F6
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,964
Members
449,094
Latest member
Anshu121

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