Power Query Problems

Create Dynamic Project Allocation Columns with Power Query M Code in WPS Office

Phi Hung VoPhi Hung Vo Nov 13, 2026 869 views

Question details

User needs Power Query M code to calculate project allocation columns dynamically from a table with changing project columns, multiplying each project value by a cost field.

Create Dynamic Project Allocation Columns with Power Query M Code in WPS Office
Product
WPS Office Spreadsheets with Power Query
Device & OS
not provided
Scenario
Working in a table that has identifier columns, a cost column, and multiple project columns that may change over time.
Observed behavior
Static M code would require manual updates each time project columns are added or removed; the goal is a dynamic query that creates allocation columns automatically.
Before you start

Make sure the source table is in the current workbook and named Table2, or adjust the table name in the M code. Confirm that the cost column is in the second position and the project columns start after it before pasting the query.

Solution 1Recommended

Create Dynamic Project Allocation Columns with Power Query M Code

Use this solution when your source table has identifier and cost columns followed by a changing number of project columns and you want each allocation column calculated automatically.

The M code dynamically reads the column names and field values from the source table. It skips the first two columns, creates an Allocation column name for every remaining project column, and calculates each allocation by multiplying the project value by the cost value.

1
Open Power Query Editor in WPS Spreadsheets

In WPS Office Spreadsheets, open the workbook containing the source table, click the Data tab, and choose Get Data > From Table/Range or Power Query Editor to open the query editor.

2
Paste the dynamic M code

In the Power Query Editor, create a blank query or open Advanced Editor. Paste the following code, replacing Table2 with your actual table name: let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content], newFieldNames = List.Transform(List.Skip(Table.ColumnNames(Source),2),(n)=>n & " Allocation"), addAllocation = Table.AddColumn(Source,"Custom",(r)=>Record.FromList(List.Transform(List.Skip(Record.FieldValues(r),2),(q)=>q*Record.FieldValues(r){1}),newFieldNames)), expanded = Table.ExpandRecordColumn(addAllocation,"Custom",newFieldNames) in expanded

3
Review and load the allocation columns

Check the new allocation columns in the preview. Click Close & Load to return the results to a new worksheet or table.

Create Dynamic Project Allocation Columns with Power Query M Code
Allocation columns update automatically: When project columns are added or removed from the source table, the query recalculates the allocation columns on the next refresh without manual M code changes.
Dynamic project allocation in WPS Office

Create Dynamic Project Allocation Columns in WPS Office Power Query

Use WPS Office Spreadsheets and its Power Query editor to paste dynamic M code that calculates allocation columns automatically, even when project columns change.

  1. 1. Open your workbook in WPS Spreadsheets: Launch WPS Office, open WPS Spreadsheets, and load the workbook that contains the source table for project allocations.
  2. 2. Open the Power Query Editor: Click the Data tab and select Get Data > From Table/Range or Power Query Editor to open the query editor.
  3. 3. Paste and adjust the dynamic M code: In the Advanced Editor, paste the M code and replace Table2 with your actual table name. Adjust the column skip and cost index only if your table layout differs.
  4. 4. Load the expanded allocation columns: Review the generated Allocation columns and choose Close & Load to place the results in the worksheet.
  5. 5. Refresh when source data changes: After adding or removing project columns, click Refresh in WPS Spreadsheets to recalculate the allocation columns automatically.
Dynamically recalculates project allocation columns without editing M code each time columns changeWorks with Excel workbooks and .xlsx files without converting the query modelFull compatibility with Microsoft Excel Power Query M code and table functionsFree and lightweight spreadsheet editing through WPS Office
microsoft office alternative - wps office

Frequently Asked Questions

What does the M code skip first two columns for?

The example assumes the first two columns are identifier and cost columns. If your table has a different layout, change the 2 in List.Skip and the 1 in Record.FieldValues(r){1} to match the actual column positions.

Where do I run this M code in WPS Office?

Open WPS Spreadsheets, click Data > Get Data > From Table/Range or Power Query Editor, then open Advanced Editor or a blank query and paste the M code.

Will allocations update automatically when project columns change?

Yes. The query builds allocation column names from the current column names, so after you refresh the connection, any added or removed project columns are picked up dynamically.

Can I use this for a table not named Table2?

Yes. Replace the table name in Excel.CurrentWorkbook(){[Name="Table2"]}[Content] with your actual table name, or create a query from your table first and use that source step.

Is this M code compatible with Microsoft Excel Power Query?

Yes. The M code uses Excel.CurrentWorkbook and standard Table functions, so it works in WPS Power Query and Microsoft Excel Power Query with the same table structure.