Forum Discussion

naveen73's avatar
naveen73
Helper III
2 years ago
Solved

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

    • naveen73's avatar
      naveen73
      Helper III

      Hi spinfuzer  ,

       

      Thanks for this.

      1. 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.
      2. 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

  • naveen73 

    Can you share some sample data with the desired output to have a clear understanding of your question?

    You can either paste your data in the reply box or save it in OneDrive, Google Drive, or any other cloud-sharing platform and share the link here.