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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
Nigel_Mayhew1
Helper I
Helper I

Extract a specific number of charcherts if a condition is met

Hi there,

 

I'd be very grateful if someone'd help with this:

 

I've a column called Reference. I need to extract a specific number of charachters (10) if the rows of this column contain a value starting with either an X or a Z.

 

For example, the references that I want to extract could be at the start, the middle or the end:

 

348964516-Z100158161

X400004308-1-65655687

(X100069554_9998

FullPPL_X300094361

 

What's important is that I extract the X or the Z in addition to the 9 charachters that follows (total 10 charachters including the letter).

 

This what the result should look like:

 

Z100158161

X400004308

X100069554

X300094361


Thanks, Nigel

4 ACCEPTED SOLUTIONS
Ahmedx
Super User
Super User

write the code in language m in power query

Screenshot_1.png

View solution in original post

pls try this

[
t1 = Splitter.SplitTextByCharacterTransition({"X","Z"},{"0".."9"})([Column1]),
t2 =  Text.End( t1{0},1)& Text.Start( t1{1},9),
Result= if List.Count(t1)>1 then t2 else null

][Result]

Screenshot_1.png

View solution in original post

Hi Ahmed,

 

Many thanks!

 

When you have a couple of minutes, I'd very grateful if you'd explain how this M code works, so i can adjust it to my needs in the future (perhaps i'll need to extract strings starting with something else other than ZRET):

 

[
a = Splitter.SplitTextByAnyDelimiter({" ","-",")","(","_"})([Reference]),
b = List.Select( a, (x)=> List.Count(
Splitter.SplitTextByCharacterTransition({"/"},{"0".."9"})(x))>1),
c = List.Transform(List.Transform(b, (x)=> Text.SplitAny(x,"/")),(x) => x{0}&"/"&x{1})
][c]

 

Best,

Nigel

View solution in original post

12 REPLIES 12
Ahmedx
Super User
Super User

or this

[a = Text.BetweenDelimiters([Reference],"ZRET"," ",RelativePosition.FromStart),
b = if a <>"" then "ZRET"& a else null][b]

 

Screenshot_3.png

Ahmedx
Super User
Super User

write the code in language m in power query

Screenshot_1.png

Hi Ahmed,

It worked really well. Thank you!

How can i please eleminate the errors deriving from cells containing values such as this?:

ZRET14/0071470

It would be great to deal with this in the same step.

Much appreciated, Nigel

pls try this

[
t1 = Splitter.SplitTextByCharacterTransition({"X","Z"},{"0".."9"})([Column1]),
t2 =  Text.End( t1{0},1)& Text.Start( t1{1},9),
Result= if List.Count(t1)>1 then t2 else null

][Result]

Screenshot_1.png

Hi Ahmed,

 

I now have another case where i need to extraxt a specific reference from a column:

 

ReferenceResult
X201227638null
ZRET19/0014457ZRET19/0014457
ZRET23/0003605 ( X201212911-1-Madone 838954920)ZRET23/0003605
SB_X201463782  ZRET23/0010338ZRET23/0010338
X201436502-1 ZRET22/0004334 - TA-N100728285ZRET22/0004334
Leergut Z201294363-1-Fuerstenfeldbrucknull
( X201212911-1-Madone 838954920 ZRET14/55597 )ZRET14/55597

 

Can you please help?

 

Thanks,

Nigel

pls try this

[
    a = if Text.Contains([Reference], "ZRET") then Splitter.SplitTextByAnyDelimiter({" ","-",")","("})([Reference]) else {null},
b= List.Min( List.Select(a, (x)=> Text.Contains(x, "ZRET")))
][b]

Screenshot_2.png

Hi Ahmed,

 

Thanks for your response!

 

It mostly works well, but I still have some issues with the results.

 

Here are a few examples:

 

ReferenceResult
DURCHG. MIT TA-N100640929_ ZRET23/000781ZRET23/000781
DURCHGEF. MIT TA-N100646558-ZRET19/00144254ZRET19/00144254
Durchg mit TA-N100642382- ZRTE18/0011958null
Durchgef. mit TA-N100640917_ ZRET19/0014457ZRET19/0014457
Durchgef. mit TA-N100646527 - ZRET23/0003958ZRET23/0003958
Durchgeführt mit TA-100632927 TT-10213893 ZRET23/0003605ZRET23/0003605
Dürchgeführ mit TA-N100640935_ ZRET23/0003782ZRET23/0003782
ERFASST ZRET23/0013486 GESCANT ZRET23/0012163/BZ:162ZRET23/0012163/BZ:162
Erfasst Zweck Fakturation SBnull
FAKTURIERT MIT TA-N100632169 ZRET23/0003781ZRET23/0003781
FAKTURIERT MIT TA-N100642382 _ZRET18/0011958_ZRET18/0011958

 

Can you please help?

 

Best,

Nigel

pls try

Screenshot_4.png

Hi Ahmed,

 

Many thanks!

 

When you have a couple of minutes, I'd very grateful if you'd explain how this M code works, so i can adjust it to my needs in the future (perhaps i'll need to extract strings starting with something else other than ZRET):

 

[
a = Splitter.SplitTextByAnyDelimiter({" ","-",")","(","_"})([Reference]),
b = List.Select( a, (x)=> List.Count(
Splitter.SplitTextByCharacterTransition({"/"},{"0".."9"})(x))>1),
c = List.Transform(List.Transform(b, (x)=> Text.SplitAny(x,"/")),(x) => x{0}&"/"&x{1})
][c]

 

Best,

Nigel

// Step 1: Split the text in the [Reference] column by any specified delimiter (space, "-", ")", "(", "_").
a = Splitter.SplitTextByAnyDelimiter({" ", "-", ")", "(", "_"}) ([Reference]),

// Step 2: Filter the elements in list `a`, selecting only items that contain at least one "/" character followed by a sequence of numbers.
// This is done by checking each element in `a` to see if splitting by a "/" and a sequence of digits (0-9) results in multiple parts.
b = List.Select(a, (x) => List.Count(
Splitter.SplitTextByCharacterTransition({"/"}, {"0" .. "9"})(x)) > 1),

// Step 3: Transform each item in `b` by splitting it by the "/" character.
// Then, reconstruct each item by joining the first two parts from the split with "/" in between.
c = List.Transform(
List.Transform(b, (x) => Text.SplitAny(x, "/")),
(x) => x{0} & "/" & x{1}
)

Magic! Thanks very much!

Mykhailo_Fl
New Member

Mykhailo_Fl_0-1709649580053.png

Column here is for visualyzing purpose of the SEARCH output.
Value required is in Column 2, see the DAX for it

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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
Users online (6,258)