Forum Discussion
Power Query columns depending on value
Hi all,
I have a row of values. See below screenshot (I am not able to attach the Excel file 🤔).
In each of the columns to the right I want the total of each value that is 70 at maximum if the whole of the value is used. If the whole of the value will be higher than 70, then take the part of the value that reaches the value of 50.
I want to do this in Power Query as the column of values changes every month. So the number of columns to the right will change too. The number of column will stop when the last value is used.
Thanks,
Naveen
Deleting old replies and pasting newest code here.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjNXitWJVjIxBVOmZmDKHEIZm4ApQyMIbWKOLGkG1BELAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}}), dif_cols = Table.Combine(let limit1 = 120, // change upper limit limit2 = 100, // chagne lower limit min = limit2, //List.Max({70,limit2}), old code for testing buffer = List.Buffer(#"Changed Type"[Value]), // Change to TableName[ColumnName] ct = List.Count(buffer) in List.Generate( () => [ n = 0, curr = {buffer{n}}, total = curr{0}, remainder = 0, col = 1 ], each [n] < ct, each [ n = [n] + 1, curr = if [total] <= min then { buffer{n} } & [curr] else {buffer{n}} , total = List.Sum(curr) + [remainder], remainder = if total >= limit1 then List.Max({total - limit2,0}) else if total >= min then 0 else [remainder], col = if [total] <= min then [col] else [col] + 1 ], each #table( {//"test", "Col " & Text.From([col]),"Col " & Text.From([col]+1)}, // Change "Col " to change prefix { //{[total]} & (if [total] >= limit1 then {[curr]{0}-[remainder],[remainder]} else {[curr]{0},null}) } ) )), add_cols = Table.FromColumns( Table.ToColumns(#"Changed Type") & Table.ToColumns(dif_cols), Table.ColumnNames(#"Changed Type") & Table.ColumnNames(dif_cols) ) // Change to #Changed Type to your prior table name in add_cols
14 Replies
- spinfuzerSolution Sage
See latest response
- naveen73Helper III
Hi spinfuzer ,
Thanks for this.
- I probably should have mentioned this earlier but when the total is higher than 70 code subtract. No subtraction should take place regardless of the amount.
- At first glance it seems to work but when I change the number 70 to 90 and 50 to 80, the result can be seen in the screenshot 1.
However, I expected cell B4 to be 17, cell C4 to be 24 and cell C6 21 as in screenshot 2
I get my data from another query in Power Query so should I change the following?
“Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjFQitWJVjIyBlMmhmDK2BSFZwbhQZQYG6HImSvFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]), “
Thanks so much!!!. It is much appreciated!
Naveen
- FowmySuper User
- naveen73Helper III
Hi,
Please see the link of the file. All is demo data anyway but in the spreadsheet the text is more readable.
Also, when providing the answers, could you add some links that I use for future reference when trying to sovle similar problems?
Thanks again,
Naveen
Naveen