Forum Discussion
DON_DCS
Jul 06, 2023Copper Contributor
Power Query List
Hi all, could anyone please help with M language to show how to calculate project allocation dynamically when number of projects can be changed in the example provided, e.g. Project 1 allocation = Co...
- Jul 06, 2023
DON_DCS Perhaps the attached example helps you on your way. It's a rather simplistic, step by step approach that probably can be made more effective, but then it would most likely be more difficult to follow.
The query unpivots the data first. Then it adds the calculation of the allocation for each row and adds a column to hold allocation column names.
Then it pivots only the columns relevant to the allocations and merges it with the original source, based on the project name. Expand the merged column based on the dynamic list of allocation column names created earlier.
See if you can follow the step and apply them to your own file. If not come back here.
DON_DCS
Copper Contributor
SergeiBaklan it's perfect! Thank you so much!
SergeiBaklan
Jul 10, 2023MVP
DON_DCS , you are welcome, glad to help.
- DON_DCSJul 20, 2023Copper ContributorSergeiBaklan I'm thinking if it's possible to use Table.ReplaceValue to override the project columns with the allocation calculation, i.e. eliminate the additional columns creation?
- LorenzoJul 20, 2023Silver Contributor
See Table4 in Sheet3:
// Table4 let Source = Excel.CurrentWorkbook(){[Name="Table4"]}[Content], PCols = List.Select(Table.ColumnNames(Source), (c)=> Text.StartsWith(c,"P") ), ChangedTypes = Table.TransformColumnTypes(Source, {{"Name", type text}, {"Division", type text}, {"Cost", type number}} & List.Transform(PCols, each {_, type number}) ), ReplacedValues = Table.ReplaceValue(ChangedTypes, each [Cost], null, (p,c,n) as number => p*c, PCols ) in ReplacedValues