Remove Top or Bottom Rows Automatically

Remove Top or Bottom Rows Automatically

Dynam­i­cal­ly remov­ing top and bot­tom rows in Pow­er Query can sig­nif­i­cant­ly stream­line your data clean­ing process when deal­ing with incon­sis­tent data imports. This tech­nique allows you to auto­mat­i­cal­ly elim­i­nate unnec­es­sary head­er rows or foot­er infor­ma­tion based on spe­cif­ic con­di­tions rather than fixed row counts, mak­ing your data trans­for­ma­tion process more robust and adapt­able to chang­ing source files.

The abil­i­ty to use con­di­tions rather than sta­t­ic num­bers is par­tic­u­lar­ly valu­able when work­ing with reg­u­lar­ly updat­ed reports that may con­tain vary­ing amounts of meta­da­ta or sum­ma­ry information.

In my video, I demon­strate how to imple­ment this dynam­ic row removal tech­nique that I learned at an Excel Lon­don Meetup:

Understanding the Problem with Static Row Removal

When import­ing data from text files, CSV files, or oth­er sources, you often encounter extra­ne­ous infor­ma­tion at the top and bot­tom of your dataset. These might include title rows, explana­to­ry notes, sum­ma­ry sta­tis­tics, or foot­er infor­ma­tion that aren’t part of the actu­al data you need to ana­lyze. Using Pow­er Query’s stan­dard “Remove Top Rows” or “Remove Bot­tom Rows” func­tions with a fixed num­ber works fine when your source data struc­ture nev­er changes, but becomes prob­lem­at­ic when the num­ber of these rows varies.

In the exam­ple shown in my video, we have mul­ti­ple rows of meta­da­ta before the actu­al head­er row (which con­tains “Date” as the first col­umn val­ue), and sev­er­al rows of addi­tion­al infor­ma­tion at the bot­tom of the data that need to be removed. There are also some miss­ing val­ues through­out the data that make sim­ple fil­ter­ing ineffective.

Dynamically Removing Top Rows Based on Conditions

The con­ven­tion­al approach to remov­ing top rows in Pow­er Query involves spec­i­fy­ing a fixed num­ber. How­ev­er, this can be prob­lem­at­ic when the num­ber of head­er rows changes. The dynam­ic solu­tion involves using a con­di­tion rather than a fixed count.

Here’s how to imple­ment this technique:

  1. Go to the Home tab in Pow­er Query Editor
  2. Select “Remove Rows” and then “Remove Top Rows”
  3. Instead of enter­ing a num­ber, mod­i­fy the for­mu­la to use the “each” key­word fol­lowed by a condition

The for­mu­la will look some­thing like this: each [Column1] <> “Date” (assum­ing “Date” is the head­er text in your first col­umn). This tells Pow­er Query to keep remov­ing rows until it finds a row where the first col­umn con­tains the text “Date”.

The Technical Details of the Table.Skip Function

Behind the scenes, Pow­er Query uses the Table.Skip func­tion when you remove top rows. This func­tion has a hid­den capa­bil­i­ty that isn’t obvi­ous from the user inter­face — it can accept either a count or a con­di­tion parameter.

When using a con­di­tion, the syn­tax changes from sim­ply pro­vid­ing a num­ber to using the format:

each [condition]

The con­di­tion is eval­u­at­ed for each row, start­ing from the top, and rows are removed until the con­di­tion is no longer true. This allows for dynam­ic adap­ta­tion to vary­ing source data structures.

Dynamically Removing Bottom Rows

Sim­i­lar­ly, we can apply the same con­cept to remove rows from the bot­tom of our dataset. This is par­tic­u­lar­ly use­ful when deal­ing with files that con­tain sum­ma­ry infor­ma­tion, notes, or oth­er foot­er data that should be exclud­ed from analysis.

The process for remov­ing bot­tom rows dynam­i­cal­ly is:

  1. Go to the Home tab in Pow­er Query Editor
  2. Select “Remove Rows” and then “Remove Bot­tom Rows”
  3. Replace the sta­t­ic num­ber with a con­di­tion using the “each” keyword

For exam­ple, you might use a for­mu­la like: each [Mer­chant] = “” to remove rows from the bot­tom where the Mer­chant col­umn con­tains an emp­ty text string. Or you might use each [Rev­enue] = null to remove rows where the Rev­enue col­umn con­tains null values.

Handling Different Types of Empty Values

When work­ing with bot­tom rows, it’s impor­tant to under­stand the dif­fer­ent types of emp­ty val­ues that might appear in your data:

  • Emp­ty text strings — rep­re­sent­ed by “” in formulas
  • Null val­ues — rep­re­sent­ed by null in formulas
  • Miss­ing val­ues — which might be null or emp­ty depend­ing on the data source

In the video demon­stra­tion, I show how to han­dle both emp­ty text strings and null val­ues as con­di­tions for remov­ing bot­tom rows. The key is to iden­ti­fy which col­umn and which type of emp­ty val­ue reli­ably indi­cates the foot­er sec­tion of your data.

Practical Applications and Benefits

This dynam­ic row removal tech­nique is par­tic­u­lar­ly valu­able in sev­er­al scenarios:

When work­ing with reg­u­lar­ly updat­ed reports where the struc­ture might change slight­ly between ver­sions, this approach ensures your Pow­er Query solu­tion remains robust. It’s also help­ful when deal­ing with data exports from sys­tems that include vary­ing amounts of meta­da­ta or when con­sol­i­dat­ing mul­ti­ple files that might have dif­fer­ent head­er structures.

The major ben­e­fits include:

  • More resilient data trans­for­ma­tion process­es that don’t break when source for­mats change slightly
  • Reduced need for man­u­al inter­ven­tion when pro­cess­ing new data
  • Abil­i­ty to han­dle files with incon­sis­tent struc­ture automatically
  • Greater flex­i­bil­i­ty com­pared to sta­t­ic row removal or sim­ple filtering

This tech­nique demon­strates the pow­er of M lan­guage in Pow­er Query, allow­ing for solu­tions that go beyond what’s imme­di­ate­ly avail­able in the user inter­face. By under­stand­ing and lever­ag­ing these more advanced capa­bil­i­ties, you can cre­ate more robust data trans­for­ma­tion processes.

Important Considerations

When imple­ment­ing this tech­nique, keep in mind a few impor­tant points:

The con­di­tion you use must reli­ably iden­ti­fy the bound­ary between the rows you want to keep and those you want to remove. Choose col­umn val­ues that are con­sis­tent­ly present (or con­sis­tent­ly absent) at these bound­aries. Also be aware that if your con­di­tion nev­er eval­u­ates to false, you could poten­tial­ly remove all rows from your dataset, so test­ing with rep­re­sen­ta­tive sam­ple data is essential.

Addi­tion­al­ly, remem­ber that this tech­nique works even when you have miss­ing val­ues in your actu­al data. As shown in the video, the rows are only removed when they match the spe­cif­ic con­di­tion you’ve defined, allow­ing rows with some miss­ing val­ues to be retained as long as they don’t match your removal condition.

Extracting only letters from text

If I need to extract only let­ters from my text, what should I do? Lets find out.

Extract only let­ters from text 

When I need to extract only let­ters from my text, it means that I also need to extract spaces and enters (new lines). 

With Pow­er Query this task is quite easy. 

I need to select one cell in my table, then go to the Data tab, and click on the From Table/Range com­mand in order to take our data to Pow­er Query (Fig. 1)

From Table/Range command
Fig. 1 From Table/Range command

Here, I have four sim­ple pieces of text. One in Eng­lish and three in Pol­ish. We need to go to the Add col­umn tab, then to the Cus­tom col­umn com­mand (Fig. 2)

Custom column command
Fig. 2 Cus­tom col­umn command

In the Cus­tom Col­umn win­dow, we have the space to name our col­umn. Let’s call it Clean, and below we have the Cus­tom col­umn for­mu­la box, where we write the Text.Select func­tion. In the func­tion we need to work with text, so let’s select the right col­umn (1). Then, I need let­ters. Since there will be string of char­ac­ters, I have to use curly brack­ets, where we write upper case let­ter in the first sequence, which is “A”..“Z”. Then let’s write low­er case let­ters, which is “a”..“z”. Writ­ing two dots between let­ters means that there is a sequence. Let’s close the curly brack­et and the paren­the­sis. Now, it should be fine (Fig 3)

Whole function
Fig. 3 Whole function

After press­ing OK, the sys­tem extract­ed for us only let­ters. How­ev­er, the text pre­sent­ed like this is just gib­ber­ish. That’s why we have to add spaces. To do that, we need to go to the View bar, then select the For­mu­la bar check­box and start mod­i­fy­ing our for­mu­la. Let’s write a com­ma and a space in dou­ble quotes (1). If we need oth­er signs, we should also write them, e.g. a dot (2). As we can see, we have spaces and dots in our new text (Fig. 4)

Modifying the formula
Fig. 4 Mod­i­fy­ing the formula

How­ev­er, I don’t need dots in my text. What I real­ly need are enters. We can remove the dot from the for­mu­la and write #(lf) in its place. After enter­ing the for­mu­la, we can see that we have extract­ed enters, low­er case let­ters, spaces. But, I did­n’t extract Pol­ish let­ters. In row 4 there are strict­ly Pol­ish let­ters. In rows 2 and 3 there are some of them. The ques­tion is: how can I extract unusu­al signs from text using the Text.Select func­tion. First of all, we have to check the Uni­code. We can see that let­ters from A to Z is a sequence of num­bers from 65 to 90. It’s sim­i­lar in low­er case let­ters. The most impor­tant les­son from it is that we have a sequence (Fig. 5) 

Unicode
Fig. 5 Unicode

Now, let’s go back to our for­mu­la. If I want to have Pol­ish let­ters, I can write a sequence of “Ó”..“ż”. After enter­ing the for­mu­la, we have extract­ed also Pol­ish signs (Fig. 6)

 Extracting Polish signs
Fig. 6 Extract­ing Pol­ish signs

Now, that we have extract­ed all signs we need, we can go to the Home tab and click on the Close&Load to com­mand to trans­fer our results to Excel (Fig. 7)

Loading results to Excel
Fig. 7 Load­ing results to Excel

In the Import Data win­dow, we have to select a cell (Fig. 7)

Choosing the target cell
Fig. 7 Choos­ing the tar­get cell

As we can see, the text is writ­ten in one line, so we need to wrap it. Let’s go to the Home tab and click on the Wrap Text com­mand (Fig. 8)

Wrap Text command
Fig. 8 Wrap Text command

Now, we clear­ly see upper case let­ter, low­er case let­ter, spaces, enters. I removed signs that I did­n’t need (Fig. 9)

Final table
Fig. 9 Final table

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

Combine all values within group with delimiter | Excel Power Query 03

Today, we want to com­bine all val­ues from the same group into one cell. Let’s go.

Com­bine all val­ues with­in group with delim­iter | Excel Pow­er Query 03

In our exam­ple, we want to com­bine all pub­lish­ers. It means that we’re doing an oper­a­tion reverse to the one in the pre­vi­ous post. This time, we want to go from this table (Fig. 1):

 Source table
Fig. 1 Source table

to this table (Fig. 2):

Target table
Fig. 2 Tar­get table

We can do it with Pow­er Query. First, select the table. Then, go to the Data tab and select the From Table/Range com­mand (Fig. 3).

 Going to Power Query
Fig. 3 Going to Pow­er Query

We can see that we have our two columns. The first thing we want to do is group our pub­lish­ers. Let’s select the Pub­lish­er head­er, then go to the Home tab and then Group By com­mand. In the new win­dow, we can see that we are group­ing by the Pub­lish­er col­umn. We still need a new aggre­ga­tion. Let’s change the Oper­a­tion bar into All Rows and write ‘Series’ in the New col­umn name bar. We can­not change any­thing in the Col­umn bar. Let’s press OK (Fig. 4).

Group By window
Fig. 4 Group By window

Now, we have the Pub­lish­ers group, and in the cells from the Series col­umn, we have all rows from the pre­vi­ous stage. The whole table is in one, sin­gle cell (Fig. 5).

Whole table in one cell
Fig. 5 Whole table in one cell

How­ev­er, we don’t need the whole table, which con­sists of two columns. We only need the Series col­umn. We can change it by mod­i­fy­ing the code. Let’s go the View tab, and let’s check whether the For­mu­la bar is ticked. If it is so, we can start mod­i­fy­ing the code in the for­mu­la bar (Fig. 6).

Going to the Formula bar
Fig. 6 Going to the For­mu­la bar

Look­ing at the code, we can see that we don’t want to choose under­score for each step. It means that we need only one col­umn. That’s why we must write ‘Series’ in the code (1). Now, let’s delete the type from the code (2) and write ‘type as list’. It’s impor­tant to write ‘list’ with a low­er­case (Fig. 7). And we see that we have only lists when we click on any of the Series cells. 

 Code modification
Fig. 7 Code modification

Now, as Pow­er Query treats val­ues as a list, we can extract them by using the Extract Val­ues option (Fig. 8).

Extracting values
Fig. 8 Extract­ing values

We are extract­ing the val­ues by com­bin­ing them with a delim­iter. We want to use a com­ma and a space, so let’s select the Cus­tom option first and then write what we want (Fig. 9). Let’s close it with OK.

Delimiter
Fig. 9 Delimiter

Now, we can see that we com­bined all Series names into one cell for each pub­lish­er. We can now go to the Home tab, then Close & Load com­mand, and press the Close & Load to option (Fig. 10).

Loading data to Excel
Fig. 10 Load­ing data to Excel

Let’s select the Table and the Exist­ing Work­sheet radio but­tons in the Import Data win­dow (Fig. 11)

Data importing
Fig. 11 Data importing

We can see that all val­ues with­in one group are com­bined into one cell (Fig. 12)

Grouped values
Fig. 12 Grouped values

https://www.youtube.com/watch?v=GFHsSJXeEaA&

Combine all unique values within a group | Excel Power Query 04

Let’s assume that we are in the future and we sell space­ships. We want to find out what class of a space­ship each coun­try has bought bas­ing on our table of unique space­ship classes. 

Com­bine all unique val­ues with­in group | Excel Pow­er Query 04

With Pow­er Query, this task is very sim­ple. We have to select our main table, then go to the Data tab and choose the From Table/Range com­mand in order to export our data from Excel to Pow­er Query (Fig. 1).


Fig. 1 Exporting data to Power Query
Fig. 1 Export­ing data to Pow­er Query

Since we are inter­est­ed which coun­try has bought which space­ship, we want to group our data by the coun­try. We have to select the Coun­try col­umn and go to the Home tab (1) and choose the Group By com­mand (2). In the win­dow, we need to choose the Advanced option (3) because we want to add more aggre­ga­tion. I want to count how many ships each coun­try has bought as well as know the space­ship class names. Since there isn’t any prop­er oper­a­tion, let’s choose the Sum option (4), which we will mod­i­fy lat­er in the code. In the col­umn bar, we’re choos­ing the Ship class (5). As a final step, let’s write a new name, which is going to be ‘Unique ship class­es’ (6) (Fig. 2)

Data grouping
Fig. 2 Data grouping 

In the new table, there are some errors in the last col­umn, because we want­ed to sum up text, which is impos­si­ble. It means that we need to mod­i­fy our code. We need to go to the View (1) tab and tick the For­mu­la bar check­box (2). We can see that the for­mu­la uses the List.Sum func­tion (3) (Fig. 3)

Formula bar
Fig. 3 For­mu­la bar

What we want to do is com­bine all val­ues, so let’s change the List.Sum into the Text.Combine func­tion (1). We also need to add a sep­a­ra­tor, which will be a com­ma and a space (2) (Fig. 4)

Code modification
Fig. 4 Code modification

Now, we can see that Pow­er Query has changed its cal­cu­la­tions. All ship class­es are com­bined. How­ev­er, as I said before, we need only unique val­ues with­out repeats. We just need to add one func­tion, which is the List.Distinct func­tion. (Fig. 5)

List.Distinct function
Fig. 5 List.Distinct function

Now, we can see there are no repeats. We can go to the Home tab (1) and to the Close & Load com­mand (2) and the Close & Load to option (3) (Fig. 6)

Going back to Excel
Fig. 6 Going back to Excel

In Excel, we have to remem­ber to select the Table (1) and Exist­ing work­sheet (2) radio but­tons. Then choose a tar­get cell (Fig. 7)

Data importing
Fig. 7 Data importing

As we can see, we have count­ed how many ships each coun­try has bought as well as the prod­uct class (Fig. 8)

 The final table
Fig. 8 The final table

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

List of file names from a folder | Excel Power Query 05

Today, we want to find all file names from a folder.

List of file names from a fold­er | Excel Pow­er Query 05

Let’s see our fold­er. We have dif­fer­ent kinds of files and even a sub­fold­er. There is also an emp­ty fold­er called Emp­ty, which must stay emp­ty. Our task is to find all files con­nect­ed to Excel, which means that we will need to do some fil­ter­ing (Fig. 1)

Folder content
Fig. 1 Fold­er content

We know the path, so we can copy it, then go to Excel, and then use Pow­er Query, which is the sim­plest solu­tion I know. We need to go the Data tab (1), then to the Get Data com­mand (2), then to the From File (3) and From Folder (4) options (Fig. 2)

From Folder option
Fig. 2 From Fold­er option

In the new win­dow, we can find our fold­er, for exam­ple, by the nav­i­ga­tion bar, where we can paste the whole path and press Open (Fig. 3) 

Navigation bar
Fig. 3 Nav­i­ga­tion bar

We can see that Pow­er Query has pre­pared a whole batch of infor­ma­tion from the giv­en fold­er, as well as from the Sub­fold­er for us. We don’t see the Emp­ty fold­er, because there aren’t any files. In our sit­u­a­tion, it’s fine for us. Then, we need to click on the Trans­form Data but­ton (Fig. 4)

 Information prepared by Power Query
Fig. 4 Infor­ma­tion pre­pared by Pow­er Query

The Trans­form Data option gives us the chance to add some fil­ter­ing to get the data we are inter­est­ed in. In the win­dow that appeared, we can see that we can fil­ter the infor­ma­tion by the Fold­er Path (1). If we are inter­est­ed only in the main fold­er, we can check only the main fold­er (2) (Fig. 5)

Filtering by the Folder Path
Fig. 5 Fil­ter­ing by the Fold­er Path

How­ev­er, we want all fold­ers, togeth­er with the Sub­fold­er, so let’s close this fil­ter­ing and go to the Exten­sion fil­ter­ing. Since we are inter­est­ed in Excel files, we are look­ing for the ones with an .xls exten­sion. Remem­ber that Pow­er Query is case sen­si­tive, so if we have some sus­pi­cions that there may be some upper case let­ters, we have to trans­form our Exten­sion col­umn. We need to go to the Trans­form tab, then to the for­mat com­mand and the low­er­case option (Fig. 6)

Lowercase option
Fig. 6 Low­er­case option

Now, we can fil­ter this col­umn by Text Fil­ters and the Begins with option (Fig. 7)

Begins with option
Fig. 7 Begins with option

We want the text to begin with .xls, which should get all exten­sions con­nect­ed with Excel (Fig. 8)

.xls extension
Fig. 8 .xls extension

We can see that we have our cho­sen exten­sions as well as the file names also with exten­sions. This is the infor­ma­tion that we need­ed, so let’s right click on the Name bar, and select the Remove Oth­er Columns option (Fig. 9)

Remove Other Column option
Fig. 9 Remove Oth­er Col­umn option

We are left only with one col­umn, where we have file names with exten­sions. Some­times, we don’t need any exten­sions. In those cas­es, we can add one more step to Pow­er Query. We have to select the col­umn, then go to the Trans­form tab, then to the Extract com­mand and the Text Before Delim­iter option (Fig. 10)

Text Before Delimiter option
Fig. 10 Text Before Delim­iter option

Our delim­iter is just a dot, so let’s write it (Fig. 11)

Delimiter
Fig. 11 Delimiter 

Now, we have only file names with­out exten­sions. How­ev­er, let’s say that I still want those exten­sions. In such a case I need to delete this step (Fig. 12)

Deleting the last filtering
Fig. 12 Delet­ing the last filtering

Now, I go to the Home tab, click on the Close & Load com­mand, then the Close & Load to option (Fig. 13)

Going back to Excel
Fig. 13 Going back to Excel

Let’s choose the tar­get cell and select the Table radio but­ton (Fig. 14)

Data importing
Fig. 14 Data importing

Here’s the final result (Fig. 15)

 Final table
Fig. 15 Final table

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