r/excel • • Jul 20 '25

unsolved Sort Multiple Column Data into their own columns

This one has eluded me for forever, so I'm hoping someone can help.

I regularly have to work with a table that adds labels to each row somewhat of at random.

Example:

Line A: Time, Message, Label A, Label B, Label C
Line B: Time, message, Label C, Label A, Label B
Line C: Time, message, Label C, Label G, Label X

I need to dump this all into Tableau, so what I need is for all the label As to be on one column, all the label Bs in a second, All the Label C's in their own.

To add to the confusion, not all rows have all the same labels.

So in an ideal state it would look like:

Time, Message, Label A, Label B, Label C
Time, message, Label A, Label B, Label C
Time, message, BLANK, BLANK, Label C, BLANK, Label G

and so on.

7 Upvotes

9 comments sorted by

•

u/AutoModerator Jul 20 '25

/u/Big-Corner6755 - Your post was submitted successfully.

Failing to follow these steps may result in your post being removed without warning.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/learnhtk 25 Jul 20 '25

Can you use Power Query? You may be able to solve this using only Power Query.

1

u/Big-Corner6755 Jul 21 '25

I know power query exists but I don't know enough about how to use it!

3

u/GregHullender 195 Jul 20 '25

Try this:

=LET(input,A9:E11,
  labels, DROP(input,,2),
  u_labels, UNIQUE(TOCOL(labels,3)),
  th_new, BYROW(labels, LAMBDA(row,
    LAMBDA(TRANSPOSE(IF(XMATCH(u_labels,row),u_labels)))
  )),
  new, DROP(REDUCE(0,th_new,LAMBDA(stack,th, VSTACK(stack,th()))),1),
  IFNA(HSTACK(TAKE(input,,2),new),"")
)

Change the input area if needed. I like trimrefs like A:.E because they let you add data to the end and have it be automatically added to the result, but they only work is there's nothing else in those columns.

Anyway, this dumps the first two columns of the input (the time and message) keeping the rest in a labels array. From that array, it finds all the unique label values Then it tries to find all of the unique labels in each row of the labels array. (Not the other way around; this way it generates the same format for each row.) Finally, we stack this new label array next to the first two columns of input data, and we strip out all of the #NA errors generated by the missing labels.

Presto!

3

u/PaulieThePolarBear 1929 Jul 20 '25

With Excel 2024, Excel 365, or Excel online

=LET(
a, A2:L11, 
b, 2, 
c, DROP(a, , b), 
d, SORT(UNIQUE(TOCOL(c, 3))), 
e,HSTACK(TAKE(a, , b),  MAKEARRAY(ROWS(a), ROWS(d), LAMBDA(rn,cn, IF(ISNUMBER(XMATCH(INDEX(d, cn), CHOOSEROWS(c, rn))), INDEX(d, cn), "")))), 
e
)

Update A2:L11 in variable a to be the range covering all of your data.

Variable b is the number of "constant" columns. Based upon your description and sample data, 2 is the correct value here. If your real data has more "constant" columns, adjust this value.

2

u/Broseidon132 3 Jul 20 '25

Is Label A always called Label A, or is there like a format that label A usually looks like that is different than the others? Basically I’m asking if Label A is always going to be called Label A because there’s different functions based on different data

2

u/RackofLambda 12 Jul 20 '25 edited Jul 20 '25

Another option, using PIVOTBY:

=LET(
   a, A2:E4,
   v, DROP(a,,2),
   t, v<>"",
   k, TOCOL(IFS(t,v),2),
   i, TOCOL(IFS(t,SEQUENCE(ROWS(a))),2),
   p, DROP(PIVOTBY(i,k,k,SINGLE,0,0,,0),1),
   HSTACK(INDEX(a,TAKE(p,,1),{1,2}),DROP(p,,1))
)

2

u/MayukhBhattacharya 1327 Jul 20 '25

Try using the following formula as well:

=LET(
     _a, A.:.E,
     _b, TOCOL(DROP(_a,,2)),
     _c, SORT(UNIQUE(_b)),
     _d, XLOOKUP(_b,_c,SEQUENCE(ROWS(_c))),
     _e, QUOTIENT(SEQUENCE(ROWS(_b))-1,COLUMNS(_a)-2)+1,
     _f, DROP(PIVOTBY(_e,_d,_b,SINGLE,,0,,0),1,1),
     HSTACK(TAKE(_a,,2),_f))

1

u/Decronym Jul 20 '25 edited Jul 21 '25

Acronyms, initialisms, abbreviations, contractions, and other phrases which expand to something larger, that I've seen in this thread:

Fewer LettersMore Letters
BYROWOffice 365+: Applies a LAMBDA to each row and returns an array of the results. For example, if the original array is 3 columns by 2 rows, the returned array is 1 column by 2 rows.
CHOOSEROWSOffice 365+: Returns the specified rows from an array
COLUMNSReturns the number of columns in a reference
DROPOffice 365+: Excludes a specified number of rows or columns from the start or end of an array
HSTACKOffice 365+: Appends arrays horizontally and in sequence to return a larger array
IFSpecifies a logical test to perform
IFNAExcel 2013+: Returns the value you specify if the expression resolves to #N/A, otherwise returns the result of the expression
IFS2019+: Checks whether one or more conditions are met and returns a value that corresponds to the first TRUE condition.
INDEXUses an index to choose a value from a reference or array
ISNUMBERReturns TRUE if the value is a number
LAMBDAOffice 365+: Use a LAMBDA function to create custom, reusable functions and call them by a friendly name.
LETOffice 365+: Assigns names to calculation results to allow storing intermediate calculations, values, or defining names inside a formula
MAKEARRAYOffice 365+: Returns a calculated array of a specified row and column size, by applying a LAMBDA
NAReturns the error value #N/A
PIVOTBYHelps a user group, aggregate, sort, and filter data based on the row and column fields that you specify
QUOTIENTReturns the integer portion of a division
REDUCEOffice 365+: Reduces an array to an accumulated value by applying a LAMBDA to each value and returning the total value in the accumulator.
ROWSReturns the number of rows in a reference
SEQUENCEOffice 365+: Generates a list of sequential numbers in an array, such as 1, 2, 3, 4
SORTOffice 365+: Sorts the contents of a range or array
TAKEOffice 365+: Returns a specified number of contiguous rows or columns from the start or end of an array
TOCOLOffice 365+: Returns the array in a single column
TRANSPOSEReturns the transpose of an array
UNIQUEOffice 365+: Returns a list of unique values in a list or range
VSTACKOffice 365+: Appends arrays vertically and in sequence to return a larger array
XLOOKUPOffice 365+: Searches a range or an array, and returns an item corresponding to the first match it finds. If a match doesn't exist, then XLOOKUP can return the closest (approximate) match.
XMATCHOffice 365+: Returns the relative position of an item in an array or range of cells.

Decronym is now also available on Lemmy! Requests for support and new installations should be directed to the Contact address below.


Beep-boop, I am a helper bot. Please do not verify me as a solution.
27 acronyms in this thread; the most compressed thread commented on today has acronyms.
[Thread #44359 for this sub, first seen 20th Jul 2025, 02:31] [FAQ] [Full list] ^[Contact] [Source code]