Forum Discussion
Recursive Lambda: Unpacking LET function
TTodorov Iterative methods, such as REDUCE with VSTACK or HSTACK, typically suffer from performance issues when the total number of iterations is in the thousands. As a general rule, it's best to avoid these methods whenever possible. Alternative methods may seem longer, but usually perform better overall.
If I've understood all of your instructions correctly, the following formula should produce the desired results, with no noticeable "lag" in the calculation time (based on the current size of the sample table); plus, it will continue to work even when adding new blank rows to the bottom of the table:
=LET(
table; SampleTable; delim; ", ";
lookup_table; SORT(ObjetsPositions; SEQUENCE(; COLUMNS(ObjetsPositions)));
lookup1; CHOOSECOLS(lookup_table; 1);
return1; CHOOSECOLS(lookup_table; 2);
lookup2; UNIQUE(return1);
return2; MAP(lookup2; LAMBDA(v;
TEXTJOIN(delim;; FILTER(lookup1; return1 = v)))
);
array1; delim & CHOOSECOLS(table; 1) & delim;
col_count1; (LEN(array1) - LEN(SUBSTITUTE(array1; delim; ))) / LEN(delim) - 1;
col_nums1; SEQUENCE(; MAX(col_count1));
row_nums1; TOCOL(IFS(col_count1 >= col_nums1; SEQUENCE(ROWS(array1))); 2);
unpivot1; TOCOL(TEXTBEFORE(TEXTAFTER(array1; delim; col_nums1); delim); 2);
change2; MAP(unpivot1; INDEX(table; row_nums1; 2); LAMBDA(a;b;
IF(b = "For all"; XLOOKUP(--a; lookup2; return2; 0); b))
);
array2; delim & change2 & delim;
col_count2; (LEN(array2) - LEN(SUBSTITUTE(array2; delim; ))) / LEN(delim) - 1;
col_nums2; SEQUENCE(; MAX(col_count2));
row_nums2; TOCOL(IFS(col_count2 >= col_nums2; SEQUENCE(ROWS(array2))); 2);
tbl_rowId; INDEX(row_nums1; row_nums2);
unpivot2; TOCOL(TEXTBEFORE(TEXTAFTER(array2; delim; col_nums2); delim); 2);
change1; MAP(INDEX(unpivot1; row_nums2); unpivot2; INDEX(table; tbl_rowId; 3); LAMBDA(a;b;c;
IF(a = "99"; LET(x; XLOOKUP(--b; lookup1; return1; 0;; -1); TEXT(IF(c = "PERS-0000"; MIN(x; 4); x); "@")); a))
);
results; HSTACK(
change1;
unpivot2;
CHOOSEROWS(CHOOSECOLS(table; 3; 9); tbl_rowId)
);
VSTACK(
CHOOSECOLS(SampleTable[#Headers]; 1; 2; 3; 9);
UNIQUE(results)
)
)
Note: the tbl_rowId variable allows you to pull the corresponding data from any column in the source table using either the INDEX or CHOOSEROWS function.
Please see attached...
djclementsMany thanks, formula works perfect, seems to be fastest one.
Would be possible to "remove duplicates"?
I mean, now formula duplicates positions by objects. If I create sorted table by positions and objects, I see duplicates:
In case of duplicates, I want to keep just line with priority = 0
- TTodorovMay 08, 2024Copper Contributor
djclements
Formula works just PERFECT! 🙂
Many thanks!
- djclementsMay 03, 2024Silver Contributor
TTodorov Try the updated workbook. The only changes I made to the formula were the "results" (sorted by Object, Position and Priority, then filtered to remove duplicate "keys").