I have a problem where my total percentages for each site must reconcile to a total of 1. So if the total percentage adds up to 1.01, then I need to reduce the row with the largest percentage by 0.01. If the total percentage adds up to 0.99, then I need to increase the row with the largest percentage by 0.01. If the Total Percentage adds up to 1, then default to the original percentage.
I have tried the following code, but it applies any newly calculated percentage to all rows, not just the row with the maximum value.
let maximum=List.Max([Table][Percentage]) in
Table.AddColumn([Table],"New Percentage", each if [Total Percentage] < 1 then maximum + 0.01 else if [Total Percentage] > 1 then maximum - 0.01 else [Percentage])
Another powerquery method
Add index. Group. Sort by percentage. Replace the first percentage with 1-[sum]. Expand. Resort
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1, Int64.Type),
#"Grouped Rows" = Table.Group(#"Added Index", {"Site"}, {{"data", each
let x= Table.Sort(_,{{"Percentage", Order.Descending}})
in Table.ReplaceValue(x,x{0}[Percentage],1-List.Sum(List.RemoveFirstN(x[Percentage],1)),Replacer.ReplaceValue,{"Percentage"})
, type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Percentage", "Index"}, {"Percentage", "Index"}),
#"Sorted Rows" = Table.Sort(#"Expanded data",{{"Index", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"})
in #"Removed Columns"