3

I have a column with some info displayed like that:

Product Info
I am the 3rd product from 2020
I was created in 1995 and I went public in 2021
I am a not sure if I'm from 2019 2020 2021

I have a formula to extract the year in the above column that is:

=IFERROR(FILTERXML("<k><m>"&SUBSTITUTE([@[Product Name]]," ","</m><m>")&"</m></k>","//m[.=number() and string-length()=4]"),"")

The problem with this formula is that it works fine with the first case, but it gives me a #SPILL! Error on the other two cases. My ideal output would be:

Product Info Year
I am the 3rd product from 2020 2020
I was created in 1995 and I went public in 2021 2021
I am a not sure if I'm from 2019 2020 2021
  • Basically, for the first case, just return the 4 digits. EVERY time that I only have one sequence of 4 digits, I want to return that sequence.
  • For the second case, I want to return ONLY the second year. EVERY time I have 2 sequences of 4 digits, I want to return ONLY the second year.
  • For the third case, I want to return nothing. EVERY time I have more than 2 sequences of 4 digits, I want to return blank.

The last thing I tried to add was position()>5 and that would cut off the 1995 in the second example, but I would continue having the Error on the third example. Also, my list is quite huge, and I am not sure if the position()>5 thing would work for ALL products that fall in the same second example.

I am not very good with XPATH, so any help would be greatly appreciated. Thank you!

srtklein
  • 63
  • 5

3 Answers3

3

Disclaimer: Below solution is written on the assumption that when 'count of years < 3', return the last given year. If 'count >= 3' then only return the last year if years come in pairs of two. Hence the use of 'modulus 2 == 0'.


You can expand the xpath for sure if you so desire. However, I'd rewrite it a little bit. Each predicate, the structure between the opening and closing square brackets, is a filter of a given nodelist. To write multiple of these structures is in fact anding such predicates. To get a better understanding of what most common xpath 1.0 functions can do within FILTERXML(), I'd like to redirect you to this post.

So to write a consecutive pattern of predicates I'd opt for:

  • [.*0=0] - First return a filtered nodelist of all numbers where a node multiplied by zero equals zero;
  • [string-length()=4] - Then return only those that are 4 characters long‡‡;
  • [position() = last() and (position() = 1 or position() mod 2 = 0)] - The 3rd and last predicate is the trickiest for your query. This is done with a first check that position() = last() meaning the node needs to be the last node in the filtered nodelist of step 2 and (position() = 1 or position() mod 2 = 0) means we want to check that this node is also at the 1st index or the modulus 2 of the indexed position equals 0‡‡‡.

enter image description here

Formula in B2:

=IFERROR(FILTERXML("<t><s>"&SUBSTITUTE(A2," ","</s><s>")&"</s></t>","//s[.*0=0][string-length()=4][position() = last() and (position() = 1 or position() mod 2 = 0)]"),"")

Whilst the above would work for Excel 2013 and higher‡‡‡‡, you do talk about spilled behaviour. If you happen to work with the current channel in ms365 you could also try:

=LET(x,TEXTSPLIT(A2," "),y,--FILTER(x,ISNUMBER(-(x&"**0"))*(LEN(x)=4),{1,2,3}),z,COUNT(y),IF(OR(z=1,MOD(z,2)=0),TAKE(y,,-1),""))

If you need to simply return the last year if 'count < 3' then you can use xpath "//s[.*0=0][string-length()=4][position()<3 and position() = last()]" or ms365 formula =LET(x,TEXTSPLIT(A2," "),y,FILTER(x,ISNUMBER(-(x&"**0"))*(LEN(x)=4),""),IF(COUNTA(y)>2,"",TAKE(y,,-1))).

‡‡ Note that you can be more strict about this if you'd wish to validate that a year is between say 1900-2050 or so. One could replace the 1st and 2nd predicate with [.*1>1899][.*1<2051].

‡‡‡ Note that the order or writing your and/or statements in xpath do matter. We need to use explicit parentheses to control the precedence. See this

‡‡‡‡ This is not true for Excel Online or Excel for Mac

JvdV
  • 70,606
  • 8
  • 39
  • 70
  • 2
    Nice work with `FILTERXML`! Your second offering is not so rigorous, however. Testing for numericalness without explicitly testing each character within a string is risky: if `A2` contained "*I am the 3rd product from 2020 4Mar*", for example, your formula would return 44989, at least for those versions of Excel which recognise "Mar" as a three-letter month. – Jos Woolley Jan 23 '23 at 19:32
  • 3
    Replacing `ISNUMBER(-x)` with, for example, `ISNUMBER(-(x&"**0"))`, would be one way to remedy this. – Jos Woolley Jan 23 '23 at 19:36
  • 1
    Interesting comment @JosWoolley, amened! – JvdV Jan 23 '23 at 19:42
2

Just add a simple clause to determine the number of returns, for example using ROWS (since by default FILTERXML returns a vertical array):

=LET(
    ζ, FILTERXML(
        "<k><m>" &
            SUBSTITUTE(
                [@[Product Name]],
                " ",
                "</m><m>"
            ) & "</m></k>",
        "//m[.=number() and string-length()=4]"
    ),
    ξ, ROWS(ζ),
    IF(ξ > 2, "", INDEX(ζ, ξ))
)

Edit: I might prefer to avoid FILTERXML here:

=LET(
    ζ, TEXTSPLIT([@[Product Name]], " "),
    ξ, -(ζ & "**0"),
    IF(COUNT(ξ) > 2, "", IFERROR(-LOOKUP(1, FILTER(ξ, LEN(ζ) = 4)), ""))
)
Jos Woolley
  • 8,564
  • 2
  • 4
  • 9
  • Great approach, I just have a quick question, wouldn't it be enough just: `=LET(ζ,1*TEXTSPLIT(A2," "),IF(COUNT(ζ)>1,"",TAKE(ζ,,-1)))` Thanks – David Leal Jan 23 '23 at 22:53
  • 1
    @DavidLeal Completely forgot about `TAKE`, thanks! Though see my first comment to JvdV's answer, i.e. you need to account for possibilities such as "*I am the 3rd product from 2020 4Mar*", hence my inclusion of `"**0"`. And I presume you meant `IF(COUNT(ζ)>2`? **Edit: more importantly, how does that set-up account for four-digit numbers only?** – Jos Woolley Jan 24 '23 at 05:48
  • My excel version converts `4Mar` to a date (number), If I enter in `A1` the value `4Mar`, then `=ISNUMBER(-(A1&"**0"))` returns `TRUE`, but `=ISNUMBER(-(A1&"*0"))` returns `FALSE`. My understanding you want to prevent `4Mar` to be considered as number. The OP formulated the condition in a way it makes harder to test, to me the condition would be to count trailing years (number), so it can be expressed as follow: `=LET(ζ,1*TAKE(TEXTSPLIT(A2," "),,-2),IF(COUNT(ζ)>1,"",TAKE(ζ,,-1)))`I had that in mind, that is why I used `COUNT(ζ)>1`I forgot to add another `TAKE` – David Leal Jan 24 '23 at 14:01
  • 1
    But of course if you first allow Excel to reinterpret the string "4Mar" as a date by entering it into a cell then `=ISNUMBER(-(A1&"**0"))` will naturally return `TRUE`! But that's not the point - such conversion does not take place *in-formula*. Try entering that value with a leading apostrophe into cell `A1` instead. – Jos Woolley Jan 24 '23 at 14:05
0

You can try the following using TEXTAFTER function. Assuming you have years at the end delimited by space. If that is not the case, the formula can be adapted to have additional checks (it is a number and four-digit, but strictly speaking a year can have less or more than 4 digits). Let me know if the previous assumption doesn't apply so I can try to adapt it. The following is an array version, so you can use the entire table column in case you are using excel tables:

=LET(in,A2:A4,last,TEXTAFTER(in," ",-1),
 IF(ISNUMBER(1*TEXTAFTER(SUBSTITUTE(in," "&last,"")," ",-1)),"",last))

For the case of more than one year, it removes the last year found, and if the second search is a number, then it returns empty, otherwise returns the previous year found.

excel output

David Leal
  • 6,373
  • 4
  • 29
  • 56