Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.
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
Solved! Go to Solution.
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
Thanks so much for this solution. It does what I asked for. Much appreciated!!
See latest response
Hi @spinfuzer ,
Thanks for this.
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
The Source is just a manually entered table. You can click on the Cog on the right of the source step in the applied steps to manually edit the table values.
see latest reply for updated code
Hi @spinfuzer ,
Thanks so much for both replies.
I can see two codes here. Not sure which one to use. But both of them cap the sum of each column to 80.
Maybe it will be helpful if I explain it like this:
The limits (120, 100 etc. etc.) I want to use parameters that will change from an Excel cell. I can see why there was confusion previously. Sorry about that.
See a link with a file. I have granted access to it already:
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
Hi,
Thanks so much for this. It is really helpful and is working as I hoped. 👍
The next step would be to have headers of the columns being a date.
There will be an extra column on the left. (I have out it in the data)
At some point the date in the most left column will be the same as the header of a newly create column. In that case the amount should be stated in full regardless of the rules above.
The date for the first would come from cell C4.
Would I have to make a new thread for this addition?
Naveen
Hi @naveen73 ,
Generally a post only solves one problem, your original problem has been solved. Any effort deserves to be honored, please mark @spinfuzer 's answer as a solution, thank you!
If you have any other follow-up questions, please start a new thread! Thank you for your understanding and I apologize for any inconvenience!
Best Regards,
Dino Tao
@naveen73
Are you refering to the solution from @spinfuzer ?
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
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
@naveen73
Please grant access to view the file shared
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
Sorry about this. Just gave permission.
@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.
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
26 | |
20 | |
13 | |
10 | |
10 |