Fix Power Query Index Was Outside the Bounds of the Array Error
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.

- 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 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.
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.
Navigate to the 'Home' tab in the Power Query Editor and click 'Advanced Editor' to view your custom function.
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)'.
Inside your List.Generate loop, ensure your index calls are wrapped in error handling, for example: 'x = try [x] + a{2 * [z]} otherwise [x]'.
Click 'Done' to apply the updated M code and verify that the index out of bounds error is resolved.

Restrict Index Variables with Min and Max Bounds
Use List.Min and List.Max to dynamically force your generated index values to remain within the safe 0 to length-1 range of your array.
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. Install WPS Office: Download and install the free WPS Office suite on your device.
- 2. Open Your Spreadsheets: Launch WPS Spreadsheets and open your existing Excel workbooks seamlessly.
- 3. Analyze Your Data: Utilize built-in formulas and data processing tools to manage your records without complicated scripting.

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.




