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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
Anonymous
Not applicable

How to find the length of text strings between 2 keywords?

Hello Evryone,

I have a requirement to find  number of characters between 2 text string. 

Am able to fetch the fix number of characters after the first text string(keyword) but how to extract total number of characters excluding space between 2 keywords here "Given" and "When"?

Note: These 2 keywords can be in one line or 2 different lines.

Ex 1.

Given    When 

Ex 2.

Given I am an Administrator

When I view Requirement Table

Then It should include IDD rules

 

Below is not the correct DAX. Just trying to fetch 50 characters out of it.

 
givenText length =
var BeginText = SEARCH("given",Requirements[Acceptance Criteria],1,BLANK())
var EndText = SEARCH("when",Requirements[Acceptance Criteria],1,BLANK())
RETURN
IF(ISBLANK(BeginText),BLANK(), MID(Requirements[Acceptance Criteria], BeginText,50))
 
TIA
1 ACCEPTED SOLUTION
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

M query may be a better choice.

Try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JYyxCoAwDAV/5dHZn3ASVxEcSoekBgzYiLXW31fqrXec927QKoYRlECGfk1qepVM5cgudN4tW9NV5cEk561ZkljBTLxLK/4DEYGZ0foYI9YPF8IL", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Inserted Text Between Delimiter" = Table.AddColumn(#"Changed Type", "Text Between Delimiter", each (Text.BetweenDelimiters([Column1],"Given","When")), type text),
    #"Replaced Value" = Table.ReplaceValue(#"Inserted Text Between Delimiter"," ","",Replacer.ReplaceText,{"Text Between Delimiter"}),
    #"Added Conditional Column" = Table.AddColumn(#"Replaced Value", "Custom", each if [Text Between Delimiter] = "" then null else Text.Length([Text Between Delimiter]))
    
in
    #"Added Conditional Column"

First use Text.BetweenDelimiters function to split a column by delimiters.

Then replace the space.

Finally, the Text.Length function is used to get the character length.

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
V-lianl-msft
Community Support
Community Support

Hi @Anonymous ,

 

M query may be a better choice.

Try this:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JYyxCoAwDAV/5dHZn3ASVxEcSoekBgzYiLXW31fqrXec927QKoYRlECGfk1qepVM5cgudN4tW9NV5cEk561ZkljBTLxLK/4DEYGZ0foYI9YPF8IL", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
    #"Inserted Text Between Delimiter" = Table.AddColumn(#"Changed Type", "Text Between Delimiter", each (Text.BetweenDelimiters([Column1],"Given","When")), type text),
    #"Replaced Value" = Table.ReplaceValue(#"Inserted Text Between Delimiter"," ","",Replacer.ReplaceText,{"Text Between Delimiter"}),
    #"Added Conditional Column" = Table.AddColumn(#"Replaced Value", "Custom", each if [Text Between Delimiter] = "" then null else Text.Length([Text Between Delimiter]))
    
in
    #"Added Conditional Column"

First use Text.BetweenDelimiters function to split a column by delimiters.

Then replace the space.

Finally, the Text.Length function is used to get the character length.

 

Best Regards,
Liang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @V-lianl-msft 

This works for me. Thank you so much.

Greg_Deckler
Community Champion
Community Champion

@Anonymous - Take your mid result, turn it into a table using Text to Table (word version), filter out blank rows and then use COUNTROWS across the table.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Text-to-Table/td-p/1312929



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

I just wanted the text between "given" and "when". Then I can find the length of the text string (excluding space) using LEN() and TRIM().

Note: These 2 keywords can be in one line or 2 different lines.

Ex 1.

Given    When 

Ex 2.

Given I am an Administrator

When I view Requirement Table

Then It should include IDD rules

But I am stuck to find the total string between "given" and "when". Sometimes it is giving correct result and sometimes exceeds beyond "when"

Below DAX is not giving correct result. Where am I going wrong?

 
given-when Text  =
var BeginText = SEARCH("given",lower(Requirements[Acceptance Criteria]),1,BLANK())
var EndText = SEARCH("when",lower(Requirements[Acceptance Criteria]),1,BLANK())

RETURN
IF(ISBLANK(BeginText),BLANK(), MID(LOWER(Requirements[Acceptance Criteria]), BeginText,EndText))
 
TIA

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.