rows.com

Replace dynamic table references in Power Query without Regex

Are you struggling to automate your reporting in Power Query because of dynamic table names?

3 min readMicrosoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community

Here's a common pain point in Power Query: a dynamic table reference that breaks every time the source file changes its internal naming convention. The user here has already automated file path changes but is stuck on the table name, specifically, `Table_query__23_Table` where the number shifts unpredictably. Our take is straightforward: this isn't a Regex problem. It's a naming problem, and the solution is simpler than most people expect.

The core issue is that Power Query's M language creates a named step based on the table name it finds in the workbook. That name is static in the code, but the source data can change it. The user correctly identifies the critical line: `Table_query__23_Table = Source{[Item="Table_query__23",Kind="Table"]}[Data]`. The fix they propose, renaming the step to something short and static like `TQTable`, is exactly right. It bypasses the dynamic reference entirely. Once the step name is generic, the code no longer cares what number follows "Table_query". The query simply looks for whatever table is in that position, regardless of its label.

What this means in practice is that you stop fighting the tool and start working with its logic. Power Query doesn't require you to match a variable string; it requires you to point to a specific location or condition. In this case, the user can reference the table by its position in the workbook's table collection rather than its name. If the table is always the first one, or always the only one with "Table_query" in its name, you can filter with `Table.SelectRows` or use `Table.Combine` to grab all tables and then isolate the one you need. No Regex, no complex string replacement, no fragile manual updates.

The lesson here applies to anyone automating reporting: when a reference breaks, don't assume you need a more sophisticated pattern-matching tool. Ask whether the reference itself is the problem. Static step names, positional indexing, or conditional filtering often solve what looks like a dynamic naming challenge. The user is on the right track by wanting to shorten that step name. They should take it further and make the entire table lookup logic independent of whatever numeric suffix the source file throws at them. That's not a workaround; it's a more resilient design.

From Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community

It's me again. Been working on a way to fully automate the reporting for my boss and now I've ran into this problem.

First, here is my m-code (removed most everything after = but left steps). How do I replace every instance of Table_query__23_Table where the 23 is unknown but the new one is known? Reading the m-code, I think it happens 2-3 times, where 1-2 are have "_Table" and the remaining does not.

Read the original at Microsoft Excel | Help & Support with your Formula, Macro, and VBA problems | A Reddit Community