Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago

Removing all text except certain characters

Hello, I'm new to using PowerQuery and I'm trying to clean up a column on one table to create a relationship with another table.

 

The column I'm attempting to clean up is a free text field from a maintenance system, and the goal is to remove all unrequired text and leave only what we want EX: MA-1405. 

 

I've played around with the Replace Values tool, trying to remove and change as much as I can to then extract text before delimiter, however with it being a free text field there are plenty of delimiters and occassionally there is text before the MA# 

 

I know that in Excel you can use ** to represent a string of characters and was wondering if a similar feature existed here as well?

 

Ideally I'd like to put in one formula to replace all values except "MA-***"

3 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello Anonymous 

     

    you can use a combination of Text.PositionOf and Text.Middle. Adding a try otherwise enablse to identifying those rows that don't contain any MA-****

     

    let
    	Source = #table
    	(
    		{"FreeTextColumn"},
    		{
    			{"xxasdfMA-1450…sdf"},	{"sssktest MA-1111"},	{"kkMA-11558888884"},	{"88oeoeMA-15444"}
    		}
    	),
    
    	AddColumn = Table.AddColumn
    	(
    		Source,
    		"extract MA",
    		(add)=> try
    		Text.Middle
    		(
    			add[FreeTextColumn],
    			Text.PositionOf(add[FreeTextColumn], "MA-"),
    			7
    		)
    		otherwise 
    		"contains NO MA-"
    	)
    in
    	AddColumn

     

     

    Copy paste this code to the advanced editor to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello Anonymous 

    have you been able to solve the problem with the replies given?

    If so, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    All the best

    Jimmy

  • edhans's avatar
    edhans
    Community Champion

    You can add a column with this formula, then remove the original column, and presumably filter out the rows that return null.

     

    if Text.Contains([Column1],"MA-") = true 
    then Text.Middle([Column1],Text.PositionOf([Column1],"MA-"),7)
    else null