Strange behaviour with TEXTSPLIT which to me seems like a bug. Any other explanations?

Anonymous
2024-02-27T15:34:13+00:00

Hi,

I am seeing strange behaviour with TEXTSPLIT which seems incorrect to me, particularly because the same calculations work when I use TEXTBEFORE and TEXTAFTER instead.

I am not looking to solve the particular examples posted, but rather some confirmation whether you agree that this is incorrect behaviour and a bug which should be reported to Microsoft.

The examples below are minimal example to reproduce the behaviour. I stumbled across this in some business logic which was giving incorrect results (and which was much more involved). I haven't been able to spot any pattern in the behaviour so I don't know how to protect myself against being bitten by this bug again in future (other than of course using TEXTBEFORE and TEXTAFTER instead, but that is more to type and also not always equivalent when you want to split into more than two fields).

Does anyone have any explanations or can point out my misconception before I report this as a bug to Microsoft?

Full Table of Examples

To simplify the formulas, I created the following APPLYCOL utility function. It basically functions like BYCOL but lets you return multi-row arrays instead of just a single row.

The APPLYCOL function is defined as follows:

a=1,b=2
Formula Output Expected Status Comment
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, APPLYCOL(kvs, LAMBDA(kv, TEXTSPLIT(kv,"="))), <br>ARRAYTOTEXT(out,1)) {"a","b"} {"a","b";"1","2"} FAIL Why doesn't this work?
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, APPLYCOL(kvs, LAMBDA(kv,TEXTSPLIT(kv,,"="))), <br>ARRAYTOTEXT(out,1)) {"a","b"} {"a","b";"1","2"} FAIL Or at least this?
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, APPLYCOL(kvs, LAMBDA(kv, TOCOL(TEXTSPLIT(kv,"=")))), <br>ARRAYTOTEXT(out,1)) {"a","b"} {"a","b";"1","2"} FAIL Or this?
=LET( <br>out, TRANSPOSE(TEXTSPLIT($B$1,"=",",")), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS This works but doesn't scale to more rows.
=LET( <br>kvs, {"a=1","b=2"}, <br>out, VSTACK(TEXTBEFORE(kvs,"="),TEXTAFTER(kvs,"=")), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS This works and led me to discover the next one.
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, VSTACK(TEXTBEFORE(kvs,"="),TEXTAFTER(kvs,"=")), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS Bizarrely this works!
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, TEXTSPLIT(kvs,,"="), <br>ARRAYTOTEXT(out,1)) {"a","b"} {"a","b";"1","2"} FAIL But this doesn't.
=LET( <br>kvs, HSTACK(TEXTBEFORE($B$1,","),TEXTAFTER($B$1,",")), <br>out, TEXTSPLIT(kvs,,"="), <br>ARRAYTOTEXT(out,1)) {"a","b"} {"a","b";"1","2"} FAIL Why doesn’t this work even though we're splitting by row delimiter?
=LET( <br>kvs, HSTACK(TEXTBEFORE($B$1,","),TEXTAFTER($B$1,",")), <br>out, APPLYCOL(kvs, LAMBDA(kv, TEXTSPLIT(kv,,"="))), <br>ARRAYTOTEXT(out,1)) {"a","b"} {"a","b";"1","2"} FAIL Why doesn’t this work even though we're splitting by row delimiter?
=LET( <br>kvs, HSTACK(TEXTBEFORE($B$1,","),TEXTAFTER($B$1,",")), <br>out, VSTACK(TEXTBEFORE(kvs,"="),TEXTAFTER(kvs,"=")), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS The fact that this works seems to really point the finger at TEXTSPLIT as the problem.
=LET( <br>kvs, TEXTSPLIT($B$1, ","), <br>keys, MAP(kvs, LAMBDA(kv, INDEX(TEXTSPLIT(kv, "="), 1))), <br>vals, MAP(kvs, LAMBDA(kv, INDEX(TEXTSPLIT(kv, "="), 2))), <br>out, VSTACK(keys,vals), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS Here TEXTSPLIT works again. Why? Because it's inside a MAP?
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, APPLYCOL(kvs, LAMBDA(kv, TOCOL(TEXTSPLIT(kv,"=")))), <br>ARRAYTOTEXT(out,1)) {"a","b"} {"a","b";"1","2"} FAIL Why doesn't this work when the previous line worked?
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, APPLYCOL(kvs, LAMBDA(kv, VSTACK(TEXTBEFORE(kv,"="),TEXTAFTER(kv,"=")))), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS
=LET( <br>out, APPLYCOL({"a=1","b=2"}, LAMBDA(kv, VSTACK(TEXTBEFORE(kv,"="),TEXTAFTER(kv,"=")))), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, APPLYCOL(kvs, LAMBDA(kv,TOCOL( HSTACK(TEXTBEFORE(kv,"="),TEXTAFTER(kv,"="))))), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>out, MAKEARRAY(2, 2, LAMBDA(r,c, INDEX(TEXTSPLIT(INDEX(kvs,c),"="),r))), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS Works as expected.
=LET( <br>kvs, TEXTSPLIT($B$1,","), <br>txts, MAP(kvs, LAMBDA(kv, ARRAYTOTEXT(TEXTSPLIT(kv,,"="), 0))), <br>xmlsplit, LAMBDA(text,delimiter, FILTERXML("<t><s>" & SUBSTITUTE(text, delimiter, "</s><s>") & "</s></t>", "//s")), <br>out, APPLYCOL(txts, LAMBDA(txt, ARRAYTOTEXT(xmlsplit(txt, ", "),0))), <br>ARRAYTOTEXT(out,1)) {"a","b"} {"a","b";"1","2"} FAIL I'm surprised even this doesn't work.
=LET( <br>kvs, HSTACK(TEXTBEFORE($B$1,","),TEXTAFTER($B$1,",")), <br>out, APPLYCOL(kvs, LAMBDA(kv, VSTACK(TEXTBEFORE(kv,"="),TEXTAFTER(kv,"=")))), <br>ARRAYTOTEXT(out,1)) {"a","b";"1","2"} {"a","b";"1","2"} PASS
=LET( <br>kvs, TEXTSPLIT($B$1, ","), <br>out, APPLYCOL(kvs, LAMBDA(kv, COUNTA(TEXTSPLIT(kv, "=")))), <br>ARRAYTOTEXT(out,1)) {1,1} {2,2} FAIL It's weird that with APPLYCOL the COUNTA is only seing a 1-element array.
=LET( <br>kvs, TEXTSPLIT($B$1, ","), <br>out, MAP(kvs, LAMBDA(kv, COUNTA(TEXTSPLIT(kv, "=")))), <br>ARRAYTOTEXT(out,1)) {2,2} {2,2} PASS With MAP, COUNTA sees a 2-element array as expected.

Excel Version

Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Rory Archibald 19,235 Reputation points Volunteer Moderator
2024-02-28T12:11:15+00:00

Keep it simpler. Try:

=TEXTSPLIT({"a=1"},"=")

You will just get "a" as the output because you are asking it technically to output a 2D array even if the number of rows (or columns) is actually 1, and it won't do that because it might be a jagged array. Instead it simply returns the first row/col of results.

Perhaps a decision was made that the overhead of verifying the dimensions of the output wasn't justified, or simply that it would be inconsistent for it to work one way in some circumstances and another way in others.

Whatever the reasoning, the simple answer is not to pass an array as the first argument to TEXTSPLIT. I do not believe that MS would accept that it is a bug.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-02-28T11:57:38+00:00

    Thank you. You raised and interesting point about a possibly difference between "boxed" values (even if they're of shape (1,1) ) and "unboxed" values of type text. Let's leave my APPLYCOL function out of it because where I first encountered it, I didn't have that.

    I still think it's a bug. Have a look at these examples:

    Formula Output Expected Status Comment
    =LET( <br>out,BYCOL(TEXTSPLIT("a=1,b=2",","),LAMBDA(kv,COUNTA(TEXTSPLIT(kv,"=")))), <br>ARRAYTOTEXT(out,1)) {1,1} {2,2} FAIL Why is TEXTSPLIT here producing only one value inside the BYCOL?
    =LET( <br>out,BYCOL(TEXTSPLIT("a=1,b=2",","),LAMBDA(kv,ARRAYTOTEXT(kv,1))), <br>ARRAYTOTEXT(out,1)) {"{""a=1""}","{""b=2""}"} {"{""a=1""}","{""b=2""}"} PASS Let's look inside and see what is being passed to TEXTSPLIT inside the BYCOL LAMBDA.
    =LET( <br>out,BYCOL(TEXTSPLIT("a=1,b=2",","),LAMBDA(kv,COUNTA(TEXTSPLIT(INDEX(kv,1),,"=")))), <br>ARRAYTOTEXT(out,1)) {2,2} {2,2} PASS First unwrapping the kv value with INDEX works.
    =LET( <br>out,BYCOL(TEXTSPLIT("a=1,b=2",","),LAMBDA(kv,ARRAYTOTEXT(INDEX(kv,1),1))), <br>ARRAYTOTEXT(out,1)) {"{""a=1""}","{""b=2""}"} {"{""a=1""}","{""b=2""}"} PASS But why, because it's the same value?
    =LET( <br>out,BYCOL(TEXTSPLIT("a=1,b=2",","),LAMBDA(kv,COUNTA(TEXTSPLIT(kv,,"=")))), <br>ARRAYTOTEXT(out,1)) {1,1} {2,2} FAIL I thought maybe there is some implicit column intersection going on, but if you switch to using = as the row delimiter, it still only produces a one-element array output.
    =LET( <br>out,BYCOL(TEXTSPLIT("a=1,b=2",","),LAMBDA(kv,COUNTA(TEXTSPLIT(INDEX(kv,1),,"=")))), <br>ARRAYTOTEXT(out,1)) {2,2} {2,2} PASS Whereas unwrapping with INDEX first gives us the result we expect.
    =LET( <br>out,BYCOL(TEXTSPLIT("a=1,b=2",","),LAMBDA(kv,ARRAYTOTEXT(TEXTSPLIT(kv,"="),1))), <br>ARRAYTOTEXT(out,1)) {"{""a""}","{""b""}"} {"{""a"",""1""}","{""b"",""2""}"} FAIL Let's look what TEXTSPLIT actually produces inside the BYCOL. We have to turn this into a text representation because BYCOL only allows us to return a single value for each column. <br> <br>It looks like it produces just "a" and "b" respectively which doesn't make sense, because it either sees the whole "a=1" or "b=2" input or not. It can't just see the "a" or "a=" part.
    =LET( <br>out,BYCOL(TEXTSPLIT("a=1,b=2",","),LAMBDA(kv,ARRAYTOTEXT(TEXTSPLIT(INDEX(kv,1),"="),1))), <br>ARRAYTOTEXT(out,1)) {"{""a"",""1""}","{""b"",""2""}"} {"{""a"",""1""}","{""b"",""2""}"} PASS After unwrapping with INDEX it produces {"a","1"} and {"b","2"} as expected.

    Was this answer helpful?

    0 comments No comments
  2. Rory Archibald 19,235 Reputation points Volunteer Moderator
    2024-02-28T10:24:56+00:00

    I don't know that it's a bug per se. You're using CHOOSECOLS, so you are passing an array to TEXTSPLIT(albeit an array with one item) and it can't return an array of arrays which would strictly be the correct result, so it just returns an array of the first items (row or col) so behaves like TEXTBEFORE.

    If you use:

    =LET(kvs,TEXTSPLIT(B1,","),out,APPLYCOL(kvs,LAMBDA(kv,TEXTSPLIT(kv,,"="))),ARRAYTOTEXT(out,1))

    and amend APPLYCOLS to something like:

    =LAMBDA(array,colfunc,REDUCE(colfunc(INDEX(CHOOSECOLS(array, 1),1)),SEQUENCE(1,COLUMNS(array) - 1, 2),LAMBDA(accumulator,col,HSTACK(accumulator, colfunc(INDEX(CHOOSECOLS(array,col),1))))))

    then it should produce the output you want.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2024-02-28T09:27:30+00:00

    Thank you for your response.

    Unfortunately I am not interested in producing the text output or those particular examples. I encountered this problem with TEXTSPLIT deep in some business logic which was producing incorrect results. The examples I gave were the minimal reproducible examples I could come up with.

    In terms of producing the output that I want (an array), the simplest formula I believe is:

    
    =TRANSPOSE(TEXTSPLIT(A1,"=",","))
    
    I only put those ARRAYTOTEXT wrappers in so I could efficiently display a whole array in one cell. With that back in my formula above would be:
    
    =ARRAYTOTEXT(TRANSPOSE(TEXTSPLIT(A1,"=",",")),1)
    
    I will try to copy all of my examples from my workbook to the post as a table since it's not letting me upload the workbook.
    

    Was this answer helpful?

    0 comments No comments
  4. Andreas Killer 143.6K Reputation points Volunteer Moderator
    2024-02-27T16:41:57+00:00

    B1:

    =LET(arr,TEXTSPLIT(A1,"=",","), l, """"&TEXTJOIN(""",""",0,VSTACK(CHOOSECOLS(arr,1)))&"""",r, """"&TEXTJOIN(""",""",0,VSTACK(CHOOSECOLS(arr,2)))&"""","{"&l&";"&r&"}")

    Was this answer helpful?

    0 comments No comments