Forum Discussion
Extract number from text string based on conditions
- 7 years ago
Here's one method:
- Create a function using the following code. This function takes a text input and returns the first 7-digit number found (if any).
- Invoke this function to create a custom column
(InputText as text) => let RequiredLength = 7, Digits = {"0".."9"}, CharacterList = Text.ToList(InputText), FirstNumber = List.Accumulate( CharacterList, "", (String,CurrentChar)=> if Text.Length(String) = RequiredLength then String else if List.Contains(Digits,CurrentChar) then String & CurrentChar else "" ) , ReturnValue = if Text.Length(FirstNumber) = RequiredLength then FirstNumber else null in ReturnValueThe function works by taking the characters of InputText from left to right, and building up a string of numbers (stopping when 7 numeric characters are accumulated), otherwise resetting to an empty string when it encounters a non-numeric character.
Here's another idea using table grouping to group consecutive digits together (also a function):
(InputText as text) =>
CharacterList = Text.ToList(InputText), CharacterTable = Table.FromList(CharacterList, Splitter.SplitByNothing(), type table[Character = text], null, ExtraValues.Error), AddedIndex = Table.AddIndexColumn(CharacterTable, "Index", 1, 1), AddedDigitFlag = Table.AddColumn(AddedIndex, "Digit", each List.Contains({"0".."9"},[Character]), type logical), DigitGroups = Table.Group(AddedDigitFlag, {"Digit"}, {{"Number", each Text.Combine(Table.Sort(_,{"Index"})[Character]), type text}}, GroupKind.Local), FilterNumbersLength7 = Table.SelectRows(DigitGroups, each [Digit] = true and Text.Length([Number])=7), FirstNumber = try FilterNumbersLength7{0}[Number] otherwise null in FirstNumberAnother option might be using some R code to find text matching an appropriate regular expression.
Regards,
Owen :)
Here's one method:
- Create a function using the following code. This function takes a text input and returns the first 7-digit number found (if any).
- Invoke this function to create a custom column
(InputText as text) =>
let
RequiredLength = 7,
Digits = {"0".."9"},
CharacterList = Text.ToList(InputText),
FirstNumber =
List.Accumulate(
CharacterList,
"",
(String,CurrentChar)=>
if Text.Length(String) = RequiredLength then String
else if List.Contains(Digits,CurrentChar) then String & CurrentChar
else ""
) ,
ReturnValue =
if Text.Length(FirstNumber) = RequiredLength then FirstNumber else null
in
ReturnValueThe function works by taking the characters of InputText from left to right, and building up a string of numbers (stopping when 7 numeric characters are accumulated), otherwise resetting to an empty string when it encounters a non-numeric character.
Here's another idea using table grouping to group consecutive digits together (also a function):
(InputText as text) =>
CharacterList = Text.ToList(InputText), CharacterTable = Table.FromList(CharacterList, Splitter.SplitByNothing(), type table[Character = text], null, ExtraValues.Error), AddedIndex = Table.AddIndexColumn(CharacterTable, "Index", 1, 1), AddedDigitFlag = Table.AddColumn(AddedIndex, "Digit", each List.Contains({"0".."9"},[Character]), type logical), DigitGroups = Table.Group(AddedDigitFlag, {"Digit"}, {{"Number", each Text.Combine(Table.Sort(_,{"Index"})[Character]), type text}}, GroupKind.Local), FilterNumbersLength7 = Table.SelectRows(DigitGroups, each [Digit] = true and Text.Length([Number])=7), FirstNumber = try FilterNumbersLength7{0}[Number] otherwise null in FirstNumber
Another option might be using some R code to find text matching an appropriate regular expression.
Regards,
Owen :)
Hello Owen, is there any way to get this into a Flow action? I tried getting the file, copying your text into a Compose action, and replacing InputText with the Filename from Dynamic Content, but the result in Body looks like it displays your text, not the evaluated function... Thanks In Advance.
- OwenAuger6 years agoSuper User
Anonymous
The M code I posted earlier can't be directly translated to Flow.
I'm sure there is a way of performing the same actions using Flow functions but I don't know enough to tell you what that would be. Perhaps try posting on the Flow forums.
You can use Power Query actions with a SQL Server data source using the Premium "Transform data using Power Query" action, but I suspect that may not be what you are wanting.
Regards,
Owen
- Anonymous6 years agoNot applicable
OwenAuger Thank you, no worries some bright spark in the Flow forum translated my requirement into 2 split functions: split at ' - ' and then split at '-'. For completion (and anyone else stuck on a similar problem), this is the link.