Last active
June 15, 2023 09:39
-
-
Save ncalm/2e90336cf14d72a240e5ee0410247be5 to your computer and use it in GitHub Desktop.
This Excel Lambda gist provides a lambda for building random combinations of dimension members
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Creates an empty array of r rows and c columns | |
| Author: Owen Price | |
| Date: 2022-09-14 | |
| */ | |
| EMPTYARRAY = LAMBDA(r, [c], EXPAND({""}, r, IF(ISOMITTED(c), 1, c), "")); | |
| /* | |
| CROSSJOIN creates the cross-product of two arrays | |
| Slightly modified from similar work by Erik Oehm | |
| Author: Owen Price | |
| Date: 2022-09-14 | |
| */ | |
| CROSSJOIN = LAMBDA(array1, array2, [output], | |
| LET( | |
| _out, IF(ISOMITTED(output), EMPTYARRAY(1, COLUMNS(array1) + COLUMNS(array2)), output), | |
| _row, TAKE(array1, 1), | |
| _fn, LAMBDA(row, array, HSTACK(CHOOSEROWS(row, SEQUENCE(ROWS(array), , , 0)), array)), | |
| _join, VSTACK(_out, _fn(_row, array2)), | |
| _output, IF(ROWS(array1) = 1, DROP(_join, 1), CROSSJOIN(DROP(array1, 1), array2, _join)), | |
| _output | |
| ) | |
| ); | |
| /* | |
| RANDOMDB creates a random array of combinations of items from each column in member_array | |
| The resulting array has 'COLUMNS(member_array)' columns and 'rows' rows. | |
| Author: Owen Price | |
| Date: 2022-12-13 | |
| */ | |
| RANDOMDB = LAMBDA(member_array, rows, | |
| LET( | |
| t, member_array, | |
| c, COLUMNS(t), | |
| IF( | |
| c = 1, | |
| "Not enough columns", | |
| LET( | |
| seq, SEQUENCE(c - 2, , 3), | |
| nonblank, LAMBDA(c, FILTER(INDEX(t, , c), INDEX(t, , c) <> "")), | |
| first, CROSSJOIN(nonblank(1), nonblank(2)), | |
| all, IF(c = 2, first, REDUCE(first, seq, LAMBDA(a, b, CROSSJOIN(a, nonblank(b))))), | |
| rand, CHOOSEROWS(all, RANDARRAY(rows, 1, 1, ROWS(all))), | |
| rand | |
| ) | |
| ) | |
| ) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment