Last active
January 30, 2026 22:32
-
-
Save Medohh2120/f8553c149684e39bb499249e39f01017 to your computer and use it in GitHub Desktop.
Unpivot 2 Headers table
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
| /* | |
| Name: UNPIVOT_PLUS | |
| Description: Given a Table or a range with headers, unpivots all columns | |
| V2 UPDATE: Now supports n headers. | |
| Syntax Change: Header names are now passed as a single array. | |
| optionally removing Grid blank entries. | |
| optionally removing Grid errors. | |
| Made By: Medohh2120 | |
| */ | |
| UNPIVOT_PLUS = LAMBDA(Table, [No_of_headers], [Col_names], [value_name], [Remove_blanks], [Remove_errors], | |
| LET( | |
| /* 1. Optional Arguments & Defaults */ | |
| Col_names, IF(ISOMITTED(Col_names), "", Col_names), | |
| value_name, IF(ISOMITTED(value_name), "Value", value_name), | |
| Remove_blanks, IF(ISOMITTED(Remove_blanks), TRUE, Remove_blanks), | |
| Remove_errors, IF(ISOMITTED(Remove_errors), TRUE, Remove_errors), | |
| No_of_headers,IF(ISOMITTED(No_of_headers), 1, No_of_headers), | |
| /* 2. Anchor Column Name */ | |
| primary_col_name, IF(ISBLANK(INDEX(Table, No_of_headers, 1)), "Col_name", INDEX(Table, No_of_headers, 1)), | |
| /* 3. Extract Data Grids */ | |
| Grid, DROP(Table, No_of_headers, 1), | |
| col, DROP(TAKE(Table, , 1), No_of_headers), | |
| hdrs, TAKE(DROP(Table, , 1), No_of_headers), | |
| /* 4. Flatten & Broadcast Indices */ | |
| r, ROW(Grid), | |
| c, COLUMN(Grid), | |
| f_Grid, TOCOL(Grid), | |
| f_col, TOCOL(IF(c, col)), | |
| /* 5. Stack Headers */ | |
| header_stack, DROP( | |
| REDUCE("",SEQUENCE(No_of_headers),LAMBDA(acc, nxt, | |
| LET( | |
| /* grab names from user's list at index nxt */ | |
| user_input, IFERROR(INDEX(TOCOL(Col_names), nxt), ""), | |
| /* If input is empty, use default name */ | |
| final_names, IF(user_input="" , "Col" & nxt, user_input), | |
| /* Stack the names with the repeated header values */ | |
| HSTACK(acc, VSTACK(final_names, TOCOL(IF(r, INDEX(hdrs, nxt, ))))) | |
| ) | |
| ) | |
| ), | |
| , | |
| 1 | |
| ), | |
| /* 6. Combine columns with headers */ | |
| stacked, HSTACK(VSTACK(primary_col_name, f_col), header_stack, VSTACK(value_name, f_Grid)), | |
| /* 7. handle all four states (Blanks, Errors, Both, None) */ | |
| FILTER(stacked, | |
| VSTACK( | |
| 1, // 1 forces header retention | |
| NOT( | |
| (Remove_blanks * ISBLANK(f_Grid)) | |
| + | |
| (Remove_errors * ISERROR(f_Grid)) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Warning: Data range Must be updated according to given number of headers:
2 Headers in action:
1 Header in action (defaults to 1 header):
[NEW] N headers (you can add as much as you want)
Note:
column_namesis realted tonumber_of_headersNOT primary left most column, and It's put in vertical or horizontal array/reference according to your likings as in:{header1;header2}vstack(header1,header2)a3:a5