Power Query Locate next blank cell then delete all rows below

Joey_Ng

New Member
Joined
Mar 7, 2020
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a table with a column name Site with lots of rows. I want to locate the next blank cell (next row with blank cell in Site column) then delete all rows below that. The other option is delete the blank rows plus N number of rows below (eg, blank plus 20 rows). How do I go about doing this.

Thank You.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try the following code. You can use it for both scenarioby changing a single line that I explained in the comments:

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    // Add an index column
    AddIndex = Table.AddIndexColumn(Source, "Index", 0, 1),
    // Total row count
    TotalRows = Table.RowCount(AddIndex),
    // Find the first blank row index, use the total row count if blank not found
    BlankIndex = try Table.SelectRows(AddIndex, each [Site] = null)[Index]{0} otherwise TotalRows,

    // Rows to be deleted
    // Option 1: The following line deletes all after the blank
    TotalToDelete = TotalRows - BlankIndex, 
    // Option 2: To delete certain number of rows only, i.e. 20, then use the following line instead
    // TotalToDelete = 20 + 1,

    ToBeDeleted = if BlankIndex + TotalToDelete > TotalRows then TotalRows - BlankIndex else TotalToDelete,

    // Remove rows after the first blank row. If not found then the logic will take care about it
    DeleteRows = Table.RemoveRows(AddIndex, BlankIndex, ToBeDeleted),
    // Remove the index column
    RemoveIndex = Table.RemoveColumns(DeleteRows, {"Index"})
in
    RemoveIndex
 
Upvote 0

Forum statistics

Threads
1,223,951
Messages
6,175,586
Members
452,653
Latest member
craigje92

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top