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

Try your skills in the Power BI Dataviz World Championship! Round one ends June 26. Join now

Reply
non23
Helper I
Helper I

Remove text after any number

How to get text in between Z and the last number? Here's a sample of the output that we want to achieve.

We were able to get the text after 'Z' but we can't seem to drop the letter after the number.

Untitled.png

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @non23 ,

Thank you for reaching out to the Microsoft Fabric Community.

 

I tried the following M Query on my end, and it worked successfully:

 

let

    textValue = [Data], 

    zPos = Text.PositionOf(textValue, "Z", Occurrence.First), 

    numbers = {"0".."9"},

    lastNumPos = List.Max(List.Transform(numbers, each Text.PositionOf(textValue, _, Occurrence.Last))), 

    start = if zPos = -1 then 0 else zPos + 1, 

    end = if lastNumPos = -1 then Text.Length(textValue) else lastNumPos + 1, 

    extractedText = Text.Middle(textValue, start, end - start) 

in

    extractedText

 

Since it worked for me, I am attaching a PBIX file to help you understand the setup better. If you need any further assistance, feel free to reach out.

 

If this post helps, then please give us Kudos and consider Accept it as a solution to help the other members find it more quickly.

 

Thank you. 

View solution in original post

8 REPLIES 8
wdx223_Daniel
Community Champion
Community Champion

wdx223_Daniel_0-1742268406438.png

 

Anonymous
Not applicable

Hi @non23 ,

Thank you for reaching out to the Microsoft Fabric Community.

 

I tried the following M Query on my end, and it worked successfully:

 

let

    textValue = [Data], 

    zPos = Text.PositionOf(textValue, "Z", Occurrence.First), 

    numbers = {"0".."9"},

    lastNumPos = List.Max(List.Transform(numbers, each Text.PositionOf(textValue, _, Occurrence.Last))), 

    start = if zPos = -1 then 0 else zPos + 1, 

    end = if lastNumPos = -1 then Text.Length(textValue) else lastNumPos + 1, 

    extractedText = Text.Middle(textValue, start, end - start) 

in

    extractedText

 

Since it worked for me, I am attaching a PBIX file to help you understand the setup better. If you need any further assistance, feel free to reach out.

 

If this post helps, then please give us Kudos and consider Accept it as a solution to help the other members find it more quickly.

 

Thank you. 

Anonymous
Not applicable

Hi @non23 ,

I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.


Thank you.

Anonymous
Not applicable

Hi @non23 ,

 

I wanted to follow up on our previous suggestions regarding the issue. We would love to hear back from you to ensure we can assist you further.

If my response has addressed your query, please accept it as a solution and give a ‘Kudos’ so other members can easily find it. Please let us know if there’s anything else we can do to help.

 

Thank you.

Anonymous
Not applicable

Hi @non23 ,

 

May I ask if you have resolved this issue? If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.

 

Thank you.

Omid_Motamedise
Super User
Super User

You can use the following code into the advanced editor

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcnb0sTSwcFOK1YlWikLmWBgYQASdghydvQ2MlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Splitter.SplitTextByCharacterTransition({"0".."9"},{"a".."z","A".."Z"})([Data]){0})
in
    #"Added Custom"

If my answer helped solve your issue, please consider marking it as the accepted solution.
MarkLaf
Super User
Super User

The function that does most of the work for you in this scenario would be Text.PositionOfAny

 

Here is some M for handling all your logic in one AddColumn step. I included some logic for handling cases when there is no Z and/or no numbers

 

 

let
    Source = PreviousTransforms,

    //Adds column that provides text between first Z (exclusive) and last number (inclusive)
    AddBetweenZandNum = 
    Table.AddColumn(
        Source, 
        "BetweenZandNum", 
        each let 
            posOfLastNum = Text.PositionOfAny( [Data], {"0".."9"}, Occurrence.Last ), 
            posOfZ =  Text.PositionOf( [Data], "Z", Occurrence.First ), 
            start = List.Max( { 0, posOfZ } ), 
            end = if posOfLastNum = -1 then Text.Length( [Data] ) else posOfLastNum
        in 
            Text.Middle( [Data], start, 1 + end - start ), 
        type text
    )
in
    AddBetweenZandNum

 

 

 Output

MarkLaf_0-1741685799893.png

 

Akash_Varuna
Super User
Super User

Hi @non23 I suppose there are input withot Z also .... To achive this create a calculated column with the formula given

Text.Select(
    Text.BeforeDelimiter(Text.Combine({[Data], "Z"}), "Z"),
    {"A".."Z", "0".."9"}
)

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.