Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created May 4, 2022 21:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ncalm/f9845c28594c0ee117ee198f02f3e13d to your computer and use it in GitHub Desktop.
Save ncalm/f9845c28594c0ee117ee198f02f3e13d to your computer and use it in GitHub Desktop.
This Excel lambda function compares two lists and returns a three column array showing the differences
/*
LIST.COMPARE
Compares the items in two lists and shows where an item exists in one list and not the other
Inputs:
Required:
- list1 - a one-dimensional range or array, either vertical or horizontal
- list2 - a one-dimensional range or array, either vertical or horizontal
Returns:
An array with ROWS(UNIQUE(VSTACK(list1,list2))) rows and 3 columns, with header {"list_item","position_in_list_1","position_in_list_2"}
list_item contains the value from either of the lists
position_in_list_1 indicates the ordinal position in which list_item can be found in list1, or #N/A! if list_item does not exist in list2
position_in_list_2 indicates the ordinal position in which list_item can be found in list2, or #N/A! if list_item does not exist in list2
*/
LIST.COMPARE =LAMBDA(list1,list2,
LET(
_f,LAMBDA(x,LET(fx,FILTER(x,x<>""),IF(ROWS(x)=1,TRANSPOSE(fx),fx))),
_fu,LAMBDA(k,x,XMATCH(k,x)),
_x1,_f(list1),
_x2,_f(list2),
_rx1,ROWS(_x1),
_rx2,ROWS(_x2),
_h,{"list_item","position_in_list_1","position_in_list_2"},
_x3,LET(
_arr,MAKEARRAY(
_rx1+_rx2,
2,
LAMBDA(r,c,
IF(
r<=_rx1,
CHOOSE(c,INDEX(_x1,r,1)&"",r+0),
CHOOSE(c,INDEX(_x2,r-_rx1,1)&"",r-_rx1)
)
)
),
UNIQUE(SORTBY(INDEX(_arr,,1),INDEX(_arr,,2)))
),
_rx3,ROWS(_x3),
_out,MAKEARRAY(
_rx3+1,
3,
LAMBDA(r,c,
IF(
r=1,INDEX(_h,1,c),
LET(
k,INDEX(_x3,r-1,1),
CHOOSE(c,k,_fu(k,_x1),_fu(k,_x2))
)
)
)
),
_out
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment