Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
lipster26
Frequent Visitor

Parse year from text

Hello,

 

Looking for the best way to parse the year from cells with text. The years at the beginning of the text could be done with splitting into columns, but how about the cells where the year is in the middle of the text? Appreciate the help. thanks.

 

TextResult
2021 NY EXT PMT2021
2017 CEG PA REFUND2017
CPG-LA2019-20 FRANCHISE TAX PMT RECODING2019
CPG-MS 2020 FRANCHISE TAX PMT RECODING2020
CPG-NC 2020 FRANCHISE TAX PMT RECODING2020
2017 PA INCOME TAX NOTICE PAYMENT2017
2018 TN REFUND2018
1 ACCEPTED SOLUTION
rohit_singh
Solution Sage
Solution Sage

Hi @lipster26 

Crude but effective one-liner

rohit_singh_0-1652995523726.png

= Table.AddColumn(#"Changed Type", "Year", each Number.From(Text.Range([Text],Text.PositionOf([Text], "20",Occurrence.First),4)))

 

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

View solution in original post

4 REPLIES 4
rohit_singh
Solution Sage
Solution Sage

Hi @lipster26 

Crude but effective one-liner

rohit_singh_0-1652995523726.png

= Table.AddColumn(#"Changed Type", "Year", each Number.From(Text.Range([Text],Text.PositionOf([Text], "20",Occurrence.First),4)))

 

Kind regards,

Rohit


Please mark this answer as the solution if it resolves your issue.
Appreciate your kudos! 😊

Nice. This is one of those problems that can be done many ways but the more robust you need the calculation to be, the more complex the solution has to be. Sometimes quick and dirty is good enough.

AlexisOlson
Super User
Super User

One possible approach is to split the text on any non-digit into a list and then take the first element of the list that has four digits.

 

Example query you can paste in to the Advanced Editor of a new Blank Query and examine the steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY5BCoMwEEWv8nFdQbNpuwzjmAaaSdApKOL9r+FoIWu3b94f3rY1rnM9ZAUvipK02R8n658gDigeE48/GS5MJbRfb8d36zqMkxf6xJmhfjmnplIeooQqpxn2/pYqdEu9yqwqCuX0dyRrJDa4Jpba/4JKbd8P", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Text = _t]),
    NonDigits = Text.Remove(Text.Combine({" ".."~"}), {"0".."9"}),
    #"Added Custom" = Table.AddColumn(Source, "Result", each
        List.First(List.Select(Text.SplitAny([Text], NonDigits), each Text.Length(_) = 4)),
        type text)
in
    #"Added Custom"

 

How this works:

NonDigits is the string of characters from " " to "~" with the digit characters removed:

 !"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

 Splitting the string "May 12th 2021 NY" on these characters gives a list:

Text.SplitAny("May 12th 2021 NY", NonDigits)
    = {null, null, null, null, 12, null, null, 2021, null, null, null}

Filtering (List.Select) for the elements of this list that are four characters long returns a list with a single element.

{2021}

 List.First gives the first element of this list or returns a null if the list is empty.

Anonymous
Not applicable

I would add a space as a prefix for each value, and then use Text.BetweenDelimiters, using " 2" and " " as delimiters. Just don't forget to go back and delete the leading " " in a later step!

 

--Nate

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors