Power BI: extract keyword or everything after, before or between a key character

When I have a column with sentences like a description column, I may want to extract everything before or after a keyword or simply to extract the keyword. Let´s take an example, I have this column and I want to extract everything before or after this delimiter “:”:

power bi
  • Left: LEFT('table'[argument],SEARCH("value",'table'[argument])-1)
    power bi
  • Right: RIGHT('table'[argument],LEN('table'[argument])-(SEARCH("value",'table'[argument])+1))
    power bi

NOTE:

  • Replace “table”, “argument” and “value” by yours
  • For the right, after “:”, there is a space, if I want to include it, for the “right” formula, just remove “+1”
  • If searching for “?”, put “~?”

Let´s take another example, I have this column and I want to extract this word “new”:

power bi
  • Left: same formula as above
    power bi
  • Right: same formula as above except to have “+1”, I have “+3”
    NOTE: if I want to include the space, change “+3” by “+2”
    power bi
  • Word: MID('table'[argument],SEARCH("value",'table'[argument],,0),3)
    NOTE: if I want to include the space, change “3” by “4”
    power bi

Now, I have this column and I want to extract “new” (after, before and word) but only if “extract” is found:

power bi

  • IF(CONTAINSSTRING('table'[argument],"value1"),second example formula,"value2")
    power bi
    power bi
    power bi
    NOTE: if the word is not found, to put a blank result, change "value2" by ""
    power bi

If I want to extract words between 2 delimiters, for instance “ - “:

PATHITEM(SUBSTITUTE('table'[argument],"delimiter","|"),2)

power bi

To end, I want to extract only x characters after finding a word. For this, I have to go to the “query editor -> add column -> custom column” then put this formula:

power bi

Then put this formula:

Text.Start(Text.AfterDelimiter([argument],"value_find"),number_extract)

power bi

In my example, I am asking to extract the 4 characters after finding the word “new”

NOTE:

  • A space is considered as a character
  • “value_find” can be a sentence and not only a single word

Interesting Topics