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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
TheInvoker
Frequent Visitor

How to extract a pathname from url text?

I have a table with a column for different urls like these

 

/

/person

/person?name=xyz

/person/123

/person/123?name=xyz

 

How can I extract it so it makes a new column like

 

/

/person

/person

/person

/person

 

So far I have this, and it removes the ? and all after that. But it doesn't handle /123. I don't know how to remove that.

 

= Table.AddColumn(#"Changed Type", "Table", each Text.Range([Document.request], 0, if(Text.PositionOf([Document.request], "?")=-1) then Text.Length([Document.request]) else Text.PositionOf([Document.request], "?")))

 

Does anyone know?

 

Thanks

1 ACCEPTED SOLUTION
TheInvoker
Frequent Visitor

I figured it out. First I find position of ? if exists and remove everything from it onwards.

Then I find last index of "/", and get the substring from that+1 to the end, and check if thats a number. 

If it is, I get the substring from start to the above index-1, else use the above string.

View solution in original post

6 REPLIES 6
TheInvoker
Frequent Visitor

I figured it out. First I find position of ? if exists and remove everything from it onwards.

Then I find last index of "/", and get the substring from that+1 to the end, and check if thats a number. 

If it is, I get the substring from start to the above index-1, else use the above string.

watkinnc
Super User
Super User

Here's an even easier solution:

 

let
Source = Table,
#"Extracted First Characters" = Table.TransformColumns(Source, {{"Data", each Text.Start(_, 7), type text}})
in
#"Extracted First Characters"

 

watkinnc_0-1646417786820.png

 

Keep in mind that this is not dynamic; it'll break down if the text length changes.  This is what I meant by replacing "person" with "person/":

 

let
Source = Table,
#"Replaced Value" = Table.ReplaceValue(Source,"person","person/",Replacer.ReplaceText,{"Data"}),
#"Extracted Text Before Delimiter" = Table.TransformColumns(#"Replaced Value", {{"Data", each Text.BeforeDelimiter(_, "/", 1), type text}})
in
#"Extracted Text Before Delimiter"

 

watkinnc_1-1646417998188.png

 

Forget about the non-digit to digit--that won't work.

 

--Nate

 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

You can modify the non-digit to digit to make it work. Just use a list of split characters like "/" and "?".

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W0leK1QGSBalFxfl5yGz7vMTcVNuKyipkQX1DI2N0PpLCWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Doc = _t]),
    #"Split Column by Character Transition" = Table.SplitColumn(Source, "Doc", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"/", "?"}, c), {"/", "?"}), {"Doc", "Suffix"})
in
    #"Split Column by Character Transition"

 

AlexisOlson_0-1646424303597.png

watkinnc
Super User
Super User

You could also replace values and replace "person" with "person/" and then split by the second "/".

 

--Nate


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!
TheInvoker
Frequent Visitor

Can you show an example? I don't understand.

watkinnc
Super User
Super User

You can use the Split by Character Transition under the Split menu.

 

--Nate 


I’m usually answering from my phone, which means the results are visualized only in my mind. You’ll need to use my answer to know that it works—but it will work!!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors