Forum Discussion
How could I extract location name from a text field
EdEvetts - Here a much better "M" way. The trick is the ability to reverse the string in M versus not having that ability in DAX.
Create a blank query and paste this in:
let
fnUglyStringBeGone = (MyBigFatZombieString) =>
let
MyBigFatZombieList = Text.ToList(MyBigFatZombieString),
MyBigFatZombieCrossEyedList = List.Reverse(MyBigFatZombieList),
MyBigFatZombieCrossEyedString = Text.Combine(MyBigFatZombieCrossEyedList),
MyShorterBigFatZombieCrossEyedString = Text.End(MyBigFatZombieCrossEyedString ,Text.Length(MyBigFatZombieCrossEyedString ) - Text.PositionOf(MyBigFatZombieCrossEyedString," ") -1 ),
MyEvenShorterBigFatZombieCrossEyedString = Text.End(MyShorterBigFatZombieCrossEyedString ,Text.Length(MyShorterBigFatZombieCrossEyedString ) - Text.PositionOf(MyShorterBigFatZombieCrossEyedString ," ") -1 ),
MyCrossEyedLocationString = Text.Start(MyEvenShorterBigFatZombieCrossEyedString,Text.PositionOf(MyEvenShorterBigFatZombieCrossEyedString," ")),
MyCrossEyedLocationList = Text.ToList(MyCrossEyedLocationString),
MyLocationList = List.Reverse(MyCrossEyedLocationList),
MyLocationString = Text.Combine(MyLocationList)
in
MyLocationString
in
fnUglyStringBeGoneI then added this custom column called "Location" to my "StringSadnessAndMisery" table, which only contains a single column [StringSadnessAndMisery].
=fnUglyStringBeGone([StringSadnessAndMisery])
This will work as long as your Location sits in between the 3rd delimiter from the end and the 2nd delimiter from the end. In other words, you always have 2 spaces after the actual location you are trying to identify.
Actually, in order to extract the 3rd last element in your text-string, you simply need to add a column with this formula in it:
List.First(List.LastN(Text.Split([Payee], " "), 3))
- EdEvetts10 years ago
Helper II
Thanks Greg_Deckler and ImkeF.
Indeed an ugly set of data and to make things worse, the name of the location isnt always in the same place in the text string, I was wondering if you could have some form of lookup list that recognised words in a text string and selected these out? I think I saw you could use dictionaries or lists in Python to do this. I then wondered about splitting as I think is contained in ImkeF solution.
Really appreciate your help, I shall have a go with your solutions and see what I can do
thanks Ed
- ImkeF10 years ago
Community Champion
Yes, this sounds good, but will be slow if we need to lookup for combinations like "Great Yarmouth" for example (as it would be split as well if using my method above - so you would need to iterate using Text.Contains instead). It is also error-prone due to misspelling.
How about using the postcode as a lookup-element instead? Or is it also international adresses?
- EdEvetts10 years ago
Helper II
Its mainly UK addresses and I could try to use the postcode, but its not always shown and never complete. I think I am going to have to concede defeat on this venture, but thanks for the code it will be useful in other projects
thanks
Ed