Power Query Problems

Fix Power Query Index Was Outside the Bounds of the Array Error

Steven ParkerSteven Parker Oct 19, 2026 870 views

Question details

The user needs to resolve an out-of-bounds index error when calculating numerical differences using List.Generate and Text.ToList in Power Query M.

Fix Power Query Index Was Outside the Bounds of the Array Error
Product
Power Query
Device & OS
not provided
Scenario
Writing custom M code to iterate over a list and calculate differences between numbers in alternating positions using dynamically generated indices.
Observed behavior
Power Query returns an 'Index was outside the bounds of the array' error when calculated variables like z result in negative indices or values exceeding the maximum list length.
Before you start

Before modifying your M code, duplicate your query to create a backup and open the Advanced Editor in Power Query so you can safely test changes to your list indices.

Solution 1Recommended

Convert Text to Numbers Before Indexing

Converting the list of text characters to numbers prior to array indexing prevents Power Query evaluation inconsistencies and safely allows try-otherwise error handling.

In Power Query, accessing list items with out-of-bounds indices inside a text list can sometimes bypass the try-otherwise error handler due to delayed evaluation. By transforming the text list into a number list beforehand, the guarded access logic functions correctly.

1
Open the Advanced Editor

Navigate to the 'Home' tab in the Power Query Editor and click 'Advanced Editor' to view your custom function.

2
Update the List Transform Logic

Modify your Text.ToList function call by wrapping it with a numeric transformation. Replace 'Text.ToList(u)' with 'List.Transform(Text.ToList(u), Number.From)'.

3
Apply Guarded Access

Inside your List.Generate loop, ensure your index calls are wrapped in error handling, for example: 'x = try [x] + a{2 * [z]} otherwise [x]'.

4
Save the Query

Click 'Done' to apply the updated M code and verify that the index out of bounds error is resolved.

Convert Text to Numbers Before Indexing
Optimization Tip: You can further simplify the transformation step by using 'List.Transform(Source, Fx)' instead of iterating with 'each Fx(_)'.
Free Microsoft Office alternative

Analyze Complex Data Seamlessly with WPS Office

If you are managing large datasets and need a reliable, lightweight alternative to Microsoft Excel, WPS Office provides powerful data processing capabilities in a highly compatible spreadsheet environment.

  1. 1. Install WPS Office: Download and install the free WPS Office suite on your device.
  2. 2. Open Your Spreadsheets: Launch WPS Spreadsheets and open your existing Excel workbooks seamlessly.
  3. 3. Analyze Your Data: Utilize built-in formulas and data processing tools to manage your records without complicated scripting.
Seamless format compatibility with Microsoft Excel (.xlsx, .csv, .xls) files.Built-in advanced formulas, pivot tables, and comprehensive data analysis tools.Lightweight architecture ensures large spreadsheets open instantly without lag.Familiar user interface makes transitioning from MS Office quick and effortless.
microsoft office alternative - wps office

Frequently Asked Questions

What causes the 'Index was outside the bounds of the array' error in Power Query?

This error is triggered when your M code attempts to reference a position in a list that does not exist. This usually happens if the computed index is less than 0 (like -1) or is greater than the total number of items in the list minus one.

Why does 'try...otherwise' sometimes fail to catch index errors on text lists?

Power Query handles lazy evaluation differently for various data types. When attempting arithmetic on out-of-bounds text list items inside a 'try' block, internal evaluation inconsistencies can sometimes bypass the error handler. Pre-converting the text list to numbers usually resolves this behavior.

How do I edit custom M code in Power Query?

You can edit your custom M code by opening the Power Query Editor, navigating to the 'Home' tab, and clicking on 'Advanced Editor'. This window lets you manually adjust variables, implement bounds, and add error handling.