Forum Discussion
Power Query - find option in text then select
Hi, this one might be ambitious but, I can't be the only person who's faced this challenge.
As a part of an address data cleansing exercise where I have values in a bunch of address fields that do not follow a proper pattern, I want to work my way from address_line_4 backwards to address_line_2 and see if any column contains a country name from list and where it does exist, then set country to that value.
Then, with or without country found, I want to do the same for city name from list and where it does exist, set city to that value AND for city, the corresponding country as well.
Then, (need to get a good data set for this one) I want to do the same for suburb name and where it does exist, set suburb to that value AND corresponding city and country.
Then I can include state in the mix as an additional so that if it sees VIC or NSW it might figure out its Australia. The issue with this one is VIC might be a part of a word and there's no gaurantee there will be a comma space VIC or comma VIC or space VIC no extra letters or spaces after, this one might be harder than the others.
A nice to have: The possibility of then also recognising the format of a street name line would be great but I think it might be way more complex, examples, understanding that both 99 STONNY BATTER ROAD and 11 HIGH STREET are both street street names with number and street type. The unit 5 in this case would be a pre-address line value like floor number, building name, etc.
Addresses are always a bit of a mission but, anything is better than the data I've got at the moment.
Addresses to be cleansed
| address_line_1 | address_line_2 | address_line_3 | address_line_4 |
| UNIT 5 | 99 STONNY BATTER ROAD, | MINTO | SYDNEY |
| 5/15, VANDALUR ROAD | PADAPPAI, CHENNAI | TAMIL NADU | ? |
| 11 HIGH ST | DUNEDIN | ? | ? |
| 79-81 SOUTH-CENTRE ROAD | MELBOUNE AIRPORT BUS, PARK | TULLAMARINE, VIC | ? |
| 15 ELDON STREET | TAKAPUNA | NEW ZEALAND | ? |
Country list
| country_name | country_code |
| AUSTRALIA | AU |
| INDIA | IN |
| NEW ZEALAND | NZ |
City list
| city_name | state | country_name | country_code |
| SYDNEY | NEW SOUTH WALES | AUSTRALIA | AU |
| CHENNAI | TAMIL NĀDU | INDIA | IN |
| MELBOURNE | VICTORIA | AUSTRALIA | AU |
End result
It wouldn't be a perfect address but at the very least, I should be able to resolve the city and country from the data set above to produce this:
| address_lines | city | state | country | country_code |
| UNIT 5, 99 STONNY BATTER ROAD, MINTO | SYDNEY | NEW SOUTH WALES | AUSTRALIA | AU |
| 5/15, VANDALUR ROAD, PADAPPAI | CHENNAI | TAMIL NĀDU | INDIA | IN |
| 79-81 SOUTH-CENTRE ROAD, MELBOURNE AIRPORT BIS, PARK, TULLARINE | MELBOURNE | VICTORIA | AUSTRALIA | AU |
| 15 ELDON STREET, TAKAPUNA | ? | ? | NEW ZEALAND | NZ |
Note in my end result above, since I don't have the suburb list, I wouldn't have been able to resolve TAKAPUNA as a suburb or AUCKLAND that line simply say NEW ZEALAND in the address_line_3.
Thanks, Matt
4 Replies
- Ashish_Mathur
Super User
- AnonymousNot applicable
wow, that is totally awesome Ashish, thank you!
- Ashish_Mathur
Super User
You are welcome.
- AnonymousNot applicable
I think maybe my question is similar to https://community.powerbi.com/t5/Power-Query/If-text-contains-value-from-list-then-return-that-value/td-p/958481 but not exactly, I will have a play with that to see if it helps, however mine is a bit more complex I think since there are 3 different ways of setting country for example.