Share via

Extract data based on a pattern

Ashish Mathur 101.8K Reputation points Volunteer Moderator
2021-05-25T02:54:50+00:00

Hi,

From a running text entry in a column, i would like to extract a pattern of XY/Z where X, Y and Z will always be numbers.  In cells where there is no XY/Z pattern the result be blank/null.  Please refer to data and the expected result here.

I am looking for either a Power Query solution or a formula based solution.  I do not want a VBA solution.

Thank you.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Lz365 38,191 Reputation points Volunteer Moderator
    2021-05-25T12:13:38+00:00

    Hi Ashish (it's been a while)

    Assuming data in Table1

    (EDIT when is this site going to be fixed re. formatting??? Adding picture for clarity)

    let

    Source = Excel.CurrentWorkbook(){[Name="**Table1**"]}[Content], 
    
    ChangedType = Table.TransformColumnTypes(Source, 
    
        {{"PRODUCT\_DESCRIPTION", type text}, {"Ashish Expects", type text}} 
    
    ), 
    
    Result = Table.AddColumn(ChangedType, "Result", each 
    
        List.First( 
    
            List.RemoveNulls( 
    
                List.Transform(Text.PositionOf([PRODUCT\_DESCRIPTION], "/", Occurrence.All), 
    
                (x)=> 
    
                    let 
    
                        TextRange = try Text.Range([PRODUCT\_DESCRIPTION], x-2, 4) 
    
                                    otherwise null, 
    
                        TryNumber = if TextRange is null then null 
    
                            else if Text.Length( Text.Trim(TextRange) ) < 4 then null 
    
                            else try Number.From( Text.Remove(TextRange, "/") ) 
    
                                 otherwise null 
    
                    in 
    
                        if TryNumber is null then TryNumber else TextRange 
    
                ) 
    
            ) 
    
        ), type text 
    
    ) 
    

    in

    Result
    

    Ideally this should be turned into an external function - Any challenge with this let me know

    Your updated file with the above query is available here

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments

24 additional answers

Sort by: Most helpful
  1. Ashish Mathur 101.8K Reputation points Volunteer Moderator
    2021-05-25T07:54:28+00:00

    Hi,

    I want to do this in MS Excel - not in PowerBI Desktop.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2021-05-25T06:47:30+00:00

    First this is now possible using formula since Excel only accept these wildcards ("*", "?", "~"). This is only possible using regex which VBA support it.

    When I tried to check my Power Query, the implementation that I've done before is using regex also. The issue is Power Query doesn't support Regex also, in my case I installed R language (script) to support it..

    https://docs.microsoft.com/en-us/power-bi/conne...

    You can check first the article above if you want to install the R language and update some settings in it.

    Was this answer helpful?

    0 comments No comments
  3. Ashish Mathur 101.8K Reputation points Volunteer Moderator
    2021-05-25T06:28:04+00:00

    There should not be such a case.  However, should such an exception arise, take the first one.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2021-05-25T06:20:05+00:00

    Hi Ashish. I’m RN, an Independent Advisor and Microsoft user like you.

    Is it possible to have more than one XY/Z pattern? Is yes, what would be the expected result for it?

    Was this answer helpful?

    0 comments No comments