Let’s say you got some text values and want to extract the amounts from them. Something like this:
Note: Thanks to Monty for posting this problem on our forum.
How to go about it?
We could use a variety of techniques to extract the values.
- Formulas – not easy given the unstructured nature of data. But almost possible. See this for an example.
- VBA – possible, read this forum discussion few ways to do it.
- Power Query – at first glance it might seem tricky, but PQ makes this all too easy. Read on.
Extracting amounts from text items – Power Query Tutorial
- Specify a list of currency codes. Create a table in Excel and mention the codes. Something like below. Let’s name this table as codes
- Load text data in to Power Query (Use Power Query > from Table or Data > from Table). We get this:
- Now, let’s bring in currency codes as a cross join. To do this, just insert a new custom column. In the formula section, type
- This will bring a new column with all currency codes as tables. Expand the column to cross join both tables. See below demo.
- Now, add a conditional column to check which currency code is present in the text data. You can use below settings:
- At this stage, our PQ data looks like this:
- Now, let’s filter away any nulls in the Found? column.
- Splitting each row by the currency code in next column: this is the tricky part. We can use Text.Split() to split a text value by delimiter. But the result will be a list. We just want one of the items of that list. Simple, we can pass the result of Text.Split() to List.Last() to get that. Use below formula:=List.Last(Text.Split([String],[Custom.Codes]))
- We get this:
- Now, convert the Amount column to decimal number. This will throw errors for incorrect values like .530.680268. Simply remove all these errors.
- Tidy up by removing unnecessary columns and renaming the rest. Load in to Excel. Here is a snapshot of cleaned data.
Download Example Workbook
Click here to download sample workbook. Try cleaning the data in first tab yourself using PQ. You will realize how awesome and simple this approach is compared to either formula or VBA driven methods.
How are you using Power Query?
These days, PQ (or get data & transform as it is known in newer versions of Excel) has become my go to tool for most data polishing, cleanup and reshaping problems. What about you? Are you addicted to PQ yet? Please share your experiences and wins in the comments section.
More ways to extract, clean and massage data with Power Query:
If this looks interesting, check out below tuts to learn more about PQ.
- Introduction to Power Query – Podcast
- SUMPRODUCT Vs. Power Query
- Figuring out Employee Churn with Power Query [HR Analytics]
- Unpivot data quickly with Power Query [tutorial]