How to Extract Matching Log Sections with Power Query in Excel
Question details
The user needs to locate a specific text marker in a log file and extract that line along with a defined number of subsequent lines into a table using Power Query.

- Product
- Excel / Power Query
- Device & OS
- not provided
- Scenario
- Parsing large server or system log files to isolate and extract specific error or event blocks (such as a 'MAC Flow Control' event and its following 16 rows) without causing performance bottlenecks.
- Observed behavior
- A method to generate a combined table containing only the relevant log sections is required, optimizing for speed and avoiding repeated file reads.
Ensure you have the text or log file saved locally on your computer. Note the exact marker string you need to identify and the exact number of subsequent rows required for your data extraction.
Extract Target Log Sections Using Advanced Power Query M Code
Utilize a custom M script incorporating list buffering and block generation to read the file, locate target positions, and extract the specified row ranges.
This method uses specific Power Query list functions to efficiently locate strings and slice rows. Because Power Query evaluates data line by line, utilizing List.Buffer is critical when working with large log files to load the data into memory once and prevent recurring disk reads.
Launch Excel, navigate to the Data tab on the ribbon, click Get Data, select From Other Sources, and then choose Blank Query to open the Power Query Editor.
In the Power Query Editor, go to the Home tab and click on the Advanced Editor button to insert custom M code.
Read the log file as a list of text lines and buffer it into memory by writing: SourceList = List.Buffer(Lines.FromBinary(File.Contents("C:\YourPath\logfile.txt"))).
Find the row indices that contain your target text by adding a variable: MarkerPositions = List.Positions(SourceList, each Text.Contains(_, "0xB89C NR5G MAC Flow Control")).
Use List.Transform (or List.Generate) on 'MarkerPositions' to pull 17 rows via List.Range(SourceList, _, 17). Finally, convert these combined block arrays into a structured output via Table.FromList.
Analyze and View Log Files Easily with WPS Spreadsheet
While Power Query offers advanced scripting for bulk extraction, WPS Spreadsheet provides a free, lightweight, and incredibly fast alternative for opening, filtering, and manually analyzing large text and CSV log files with an interface you already know.
- 1. Open the Log File: Launch WPS Spreadsheet, go to Menu > Open, and select your .txt or .csv log file to import the raw data.
- 2. Apply Text to Columns: Select the imported data column, navigate to the Data tab, and click Text to Columns to properly separate log timestamps and messages.
- 3. Utilize AutoFilter: Under the Data tab, click AutoFilter. Click the drop-down arrow on your column header, select Text Filters > Contains, and type your marker string to instantly locate relevant events.

Frequently Asked Questions
Why is Power Query slow when reading large text files?
Power Query evaluates queries in a streaming fashion, which can cause the engine to re-read the source text file multiple times during range extraction. Utilizing the List.Buffer function forces Power Query to load the entire text file into system memory once, drastically improving the speed of positional searches.
How do I deal with overlapping log row ranges in Power Query?
If two markers are found closer together than your extraction limit (e.g., fewer than 16 rows apart), the List.Range function may pull overlapping data. You can either use Table.Distinct at the end of your query to clean up duplicate rows, or write conditional M code that checks the distance to the next index.
Is it possible to extract a dynamic number of rows instead of exactly 16?
Yes. Instead of hardcoding '17' (the marker plus 16 rows) in the List.Range function, you can calculate the count dynamically by taking the index of the current marker subtracting it from the index of the subsequent marker to strictly grab the variable-length block.
What is the best way to convert the extracted text lists back into a single table?
Once you have generated a list containing multiple sub-lists (each representing a 17-row block), simply wrap your final step with the Table.FromList function. This converts the collections of text strings back into a structured single-column table layout.




