Oh man, sometimes we get data like this and we need to convert it from a single column to three columns: ID, date, and amount. We're going to see how to do it with Text to Columns, Flash Fill, Power Query, and Excel worksheet formulas.
Now, we use Text to Columns when it's a one-time cleaning event, which means we don't need our solution to update if the data changes. We highlight the column, go up to the Data ribbon tab, and in the Data Tools group there it is, Text to Columns. It needs to know what the delimiter is, meaning what separates the data. Spaces separate our data, so we select Delimiter. Next, we uncheck Tab and check Space. Notice it's going to treat consecutive delimiters as one, which is what we want, and there's our preview. Next, the way they set up Text to Columns is that when we use the General format, it interprets numbers and dates correctly, so all we need to do is select the destination. I'm going to say D5, collapse, click Finish, done. For me, if it's a one-time cleaning event, Text to Columns is the fastest.
Flash Fill is pretty fast too. If I give it one or two examples and then go up to the Data ribbon tab and click Flash Fill, it sees the pattern established. Get everything before the first space and extract. For date, I type my example and hit Enter. It got the right date but not the right format, so Ctrl+1. If I add the correct format, mmm/dd/yyyy, click OK, and now I can use the keyboard to access that button, Ctrl+E. Last, Flash Fill saw the pattern: get everything between the spaces and extract for amount. 700, Enter, Ctrl+E. Flash Fill got everything after the last space. So with Flash Fill, we give it an example of the pattern in the data and it does the rest.
Power Query is another option, and when we use Power Query, if the data changes we can refresh. We definitely want to use Power Query if we're importing the data from an external source, and we are, a text file. So we go up to the Data tab, Get & Transform, and there it is, From Text/CSV. We navigate to the location of our text file and double-click. We do not want to load our data yet; we want to transform it. This is the Power Query Editor. We want to name our query. The data came with a header in the first row, so in the upper left corner I click the drop-down and select Use First Row as Headers. Over here we can see it's added these steps. We don't need Change Type yet, so I'm going to X this out. Now we need to split. I select the column, Home ribbon tab, Transform group, Split Column drop-down, By Delimiter. In the drop-down I want to select Custom, and then I want to type three spaces. When I click OK, I've split it into three columns. We see that it added two steps. We do want to check the data types. If we click the data type icon, we can see the different pictures and what they represent. Whole number is fine, date is fine, but this one I want to change to Decimal. It politely asks us whether we want to replace the automatic step, and I do. Now we can rename the columns: double-click ID, enter; double-click Date, enter; double-click Amount, enter. Now it added that last step. We're ready to load, and if that source data file gets new data, these steps will be rerun when we click Refresh. We want to load it, so we go up to the Home ribbon tab, Close & Load drop-down. We want the second option, Close & Load To. For our example, we want a Table in the Excel worksheet, and I want it on the existing worksheet, B4. Click OK, done. Now I added one record to that data set, so we can come up and click Refresh. It reran all the steps, and there's our data.
Now we want to see how to accomplish this with Excel worksheet formulas. Formulas are going to be the hardest of all these methods, and the reason we would use formulas is if we want our solution to instantly update when the data changes. That's when you use formulas. Before we can create formulas, we have to recognize patterns in each bit of our data. For the first bit of data, we just need to extract everything before the first space. That means we need a formula that can find the position of that space, and the perfect function for that is SEARCH. The text we want to find is in double quotes, a space. Even though we have three, we just need to find the first one within that text. Close parentheses, Ctrl+Enter, and when I copy it down to twos and threes, that's the correct location of the first space. But my goal is to get from the left a certain number of characters, so that's one too many. Notice the cells are highlighted, and the light-colored active cell at the top is highlighted, so I hit the F2 key. I'm going to subtract one, and to populate this formula throughout the highlighted range I use Ctrl+Enter. Now I have the number of characters from the left. Then F2, I can use the LEFT function. It needs to know where the text is and how many characters from the left you want to extract. Close parentheses, and that's our formula, Ctrl+Enter. Oh, it's aligned to the left, which means Excel thinks it's text. Any time we use functions like LEFT, RIGHT, MID, or any of the other text functions, they always deliver text. If you want to convert a number or a date from text back to a number or date, you can do any math operation, times one, divided by one. I'm going to add zero. It doesn't change the value, but that math operator will convert it to a number. Ctrl+Enter, and now we have extracted the ID. That formula will work no matter what is on the other side of that space.
Now the pattern for our date: it looks like we're always going to have ten characters, and if I search for that forward slash and go back two, I have a starting point. Ten characters is the perfect situation for the MID function. We'll put our text there, then give it the starting position and the number of characters. We'll start with our search, and we're going to search for forward slash, comma, within that text. That'll be two too many, so we'll subtract two, Ctrl+Enter, double-click, and send it down. Now we have the starting position of the text we want from the middle part of our larger text. Now we use MID: there's the text, comma, that's the starting position, comma, and how many characters from that starting position, 10, close parentheses, Ctrl+Enter. Now we've extracted our date, but it's text, and we want it as a real date, which is a serial number date in Excel. So we hit F2 and do a math operation. When I Ctrl+Enter, these are the serial number dates, the number of days since December 31, 1899. So we Ctrl+1 and add some date number formatting. I'm going for custom mm/dd/yyyy, click OK. Now we have our dates.
The last bit of data we need to extract: the logic is to get everything after the last space, and there is a formula for that. It's a pretty cool formula, but it uses five functions. I'm going to try to do this differently since I already have these two bits of data. If I could gather that up here and use the SUBSTITUTE function to say get rid of all that and just leave that, that might be a shorter formula. Our formula will behave that way, and I'm going to have to put the spaces back in, one, two, three, and then join it too. Of course, I can't look at that date, because formulas do not see number formatting, so I have to use the TEXT function, which allows us to apply number formatting like we would in a cell, but inside our formula. Comma, double quote, and we'll do our custom number formatting in double quotes. If I enter that, we can see we have almost what we need, and we'll join it with three spaces. Now I have the first part. F2, and I can say substitute: there's the text, comma, that's the old text we want to get rid of, and we come to the end, comma, the new text. Well, we'll use the formula syntax for nothing, double quote, double quote, close parenthesis, Ctrl+Enter. F2, plus zero, Ctrl+Enter, double-click, and send it down. And there we have our three formulas. Much more complicated than any of the other methods we've seen in the video, but if for some reason there was a demand that if this changed we needed to update instantly, there we go. Formulas are the only feature inside Excel that can do that.
All right, we saw Text to Columns, Flash Fill, Power Query, and formulas, all to accomplish our goal. Now I want to hear in the comments below which one you like and when you use each one. Maybe you have a different solution. All right, be sure to click that thumbs up, leave a comment, and subscribe, because there are always lots more videos to come from ExcelIsFun. And if you want to check out some more videos about cleaning data, check out these videos.
