How to extract a date number and income from text string with a fixed width

Some­times, our data is com­bined into one string and we want to extract only a piece of infor­ma­tion, which will help us prop­er­ly under­stand our data. 

How to extract a date num­ber and income from text string with a fixed width

Let’s start with extract­ing the date. What is impor­tant in our exam­ple, is that each string of infor­ma­tion is of the same length, i.e. it has got a fixed width. 

Fig. 1 Data of the same string lengths

We can clear­ly see, that the date starts with the eighth sign and has got eight signs itself (mm/dd/yy). If we want to extract a cer­tain piece of infor­ma­tion, we have to use the MID func­tion. First, we have to write A2 in the argu­ment, because our infor­ma­tion is locat­ed in this cell. Then, we write 8 because we start with the eighth sign, and then 8 again because we want to extract eight chars. And we have our for­mu­la (Fig. 2).

=MID(A2,8,8)

Fig. 2 For­mu­la for extract­ing a date

After copy­ing it down, we have the results. It’s impor­tant though, that the MID func­tion, just like all text func­tions, returns results as text. We can see it because each date is aligned to the left. It may be prob­lem­at­ic if we want to do some date oper­a­tions. We can change the text by adding 0 to our for­mu­la. In oth­er words, we can per­form any math­e­mat­i­cal oper­a­tion that won’t change the returned date (Fig. 3). 

=MID(A2,8,8)+0

Fig. 3 Adding 0 to dates

Now, we can see that the val­ues are aligned to the right. It means that Excel treats them like num­bers, i.e. like dates. We can extract Id num­bers the same way. They start with the first sign and have got 6 dig­its in total. We can also use the MID func­tion. First, we select cell A2, then write 1 because it starts from the first sign, then we write 6 because we extract 6 dig­its (Fig. 4).

=MID(A2,1,6)

Fig. 4 MID func­tion for­mu­la for extract­ing Id numbers

As we can see, Excel extract­ed Id num­bers. Since it is text, Excel left the lead­ing zero for us. We can leave Id num­bers as text if it’s impor­tant for us. If it isn’t, we can do the same as with dates, i.e. add zero (Fig. 5). 

=MID(A2,1,6)+0

Fig. 5 Adding 0 to Id numbers

We can see that Id num­bers moved to the right and the lead­ing zero van­ished. The last piece of infor­ma­tion we want to extract is the Income. Income val­ues don’t have a fixed width. But, since it’s the last piece of infor­ma­tion, we can extract more signs than we need. Let’s extract also the dol­lar sign. The dol­lar sign is in the sev­en­teenth posi­tion. We start writ­ing the MID for­mu­la, then we add cell A2, then 17, and then we have to add the num­ber of places we want too extract. Let’s write 10. It’s, in fact, more that we need, how­ev­er it’s not less either. The MID func­tion will extract the infor­ma­tion to the end with­out any errors (Fig. 6). 

=MID(A2,17,10)

Fig. 6 MID func­tion for extract­ing Income

As in our pre­vi­ous for­mu­las, the results are also text. By adding zero, we change the results into numbers.

MID(A2,17,10)+0

Fig. 7 Text into numbers

We can see that now the results are num­bers. How­ev­er, we can see that we lost the dol­lar sign. This is the way Excel con­verts num­bers with dol­lar signs before them. Indeed, the dol­lar sign isn’t impor­tant in our data, so we should start from the eigh­teenth sign (Fig. 8).

Fig. 8 Remov­ing the dol­lar sign

Now, we have only num­bers. If we want to add a cur­ren­cy, we just go to the Home tab, drop down the Gen­er­al bar and select the Cur­ren­cy option (Fig. 9). 

Fig. 9 Adding a currency

When we have our results with zeros at the end, we can remove them by press­ing the Decrease Dec­i­mal option (Fig. 10).

Fig. 10 Remov­ing zeros at the end

https://www.youtube.com/watch?v=O39qM233-68

How to extract last row

Today, we want to extract the last row with giv­en cur­ren­cies. Our data is sort­ed ran­dom­ly. If we want to con­sid­er the date col­umn, we will have to sort it first. We can do it man­u­al­ly, by going to the end of date and find the last row. How­ev­er, we want to sort it in Pow­er Query, which is more auto­mat­ic. We just have to select one cell from our table, go to Data tab, and click on the From Table/Range com­mand (Fig. 1). 

How to extract last row
 From Table/Range command
Fig. 1 From Table/Range command

This com­mand exports our data to Pow­er Query. We can see, that Pow­er Query con­sid­ers our data as a date and time. In our exam­ple, it isn’t impor­tant so let’s leave it like that. If we want to sort our dates, we just click on the A to Z sort­ing com­mand on the Home tab (Fig. 2). 

A to Z sorting command
Fig. 2 A to Z sort­ing command

Now, the last row is the newest date (Fig. 3). 

The newest date in the last row
Fig. 3 The newest date in the last row

Since we are inter­est­ed in cur­ren­cies, we want to group our data by the Cur­ren­cy col­umn. We have to click one cell from the Cur­ren­cy columns and go to the Group By com­mand in the Home tab. In the Group By win­dow that has appeared, we can see that the Cur­ren­cy bar is select­ed. In the Oper­a­tion bar, we need to select All Rows and, in the New col­umn name, let’s write tem­po­rary. Then we are press­ing the OK but­ton (Fig. 4).

Group By window
Fig. 4 Group By window

And we have our data grouped by cur­ren­cies (Fig. 5). 

Data grouped by currencies
Fig. 5 Data grouped by currencies

There is a table for each cur­ren­cy that con­tains all rows with a giv­en cur­ren­cy. We have sort­ed the dates ear­li­er, so in the tables, the last date is also the newest exchange rate. And the newest exchange rate is what we want (Fig. 6). 

The newest exchange rate
Fig. 6 The newest exchange rate

Let’s go to the Add Col­umn tab, then to the Cus­tom Col­umn com­mand. A win­dow will appear, where we write the name. Let’s call it Last. Then, we need to write a for­mu­la in the Cus­tom col­umn for­mu­la box. We are writ­ing Table.LastN com­mand to attract the last row of the table. The func­tion we are writ­ing needs a table, so we are choos­ing our table called tem­po­rary, then we need to write the num­ber of rows from the end. We want just one row, so let’s write it, then close the for­mu­la. This is the whole for­mu­la we need, so let’s press the OK but­ton (Fig. 7). 

=Table.LastN(temporary),1)

The formula
Fig. 7 The formula

We can see that we have the Last col­umn. In the col­umn, there are tables, but each table con­tains only one row for each cur­ren­cy (Fig. 8). 

The Last column
Fig. 8 The Last column

We don’t need the tem­po­rary col­umn, so let’s delete it (Fig. 9).

Deleting the temporary column
Fig. 9 Delet­ing the tem­po­rary column

Now, we can expand the Last col­umn by press­ing the icon shown in Fig. 10. In the win­dow that has appeared, we des­e­lect the Cur­ren­cy and the Use orig­i­nal col­umn name as pre­fix check­box­es. Then, we press the OK but­ton (Fig. 10). 

Expanding the Last column
Fig. 10 Expand­ing the Last column

Now, we can see a prop­er table with cur­ren­cy, and the newest date with the exchange rate. Let’s go to the Home tab, and Close&Load to com­mand (Fig. 11). 

Close&Load to command
Fig. 11 Close&Load to command

We moved back to Excel, where we have to select the Exist­ing work­sheet radio but­ton, then click on cell F1 and des­e­lect the Add this data to the Data Mod­el check­box, and press the OK but­ton (Fig. 12). 

Import Data
Fig. 12 Import Data

And we have our results. How­ev­er, we have to remem­ber that Pow­er Query not always extracts prop­er data for­mat­ting. That’s why we have to go to the Home tab and select prop­er data for­mat­ting (Fig. 13). 

Results with proper dates
Fig. 13 Results with prop­er dates

Let’s make a test and change some some dates in dif­fer­ent cur­ren­cies, then refresh our table (Fig. 14). 

Refreshing the table
Fig. 14 Refresh­ing the table

We can see that the Query is auto­mat­ic when we refresh it (Fig. 15). 

Automatic refreshing in Query
Fig. 15 Auto­mat­ic refresh­ing in Query

https://www.youtube.com/watch?v=f0uORcZw73g