Forum Discussion
jmontes1810
4 years agoFrequent Visitor
How to count each time some rows sum x number?
Hi community, This time, I need some help from you to count or assign a count each time some rows sum X quantity. For example, in the next table I would like to know how to get the column "Pallet L...
- 4 years ago
You can create a "conditional" running total using List.Generate; then use List.Accumulate to develop the Pallet List.
Data
let Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"DN", type text}, {"QTY", Int64.Type}}), //generate list of pallets //replace >=80 with nulls to satisfy that condition qty= List.Buffer(List.Transform(#"Changed Type"[QTY], each if _ >=80 then null else _)), //Generate a running total of the qtys that start over when there is a null // (ie an entry that was >=80) or when the running total reaches a Max of 80 qtyList = List.Generate( ()=>[qt= qty{0}, idx=0], each [idx] < List.Count(qty), each [qt= if qty{[idx]+1} = null then null else if [qt] = null then qty{[idx]+1} else if [qt] + qty{[idx]+1} <=80 then [qt] + qty{[idx]+1} else qty{[idx]+1}, idx = [idx]+1], each [qt]), //generate the Pallet list by incrementing the list whenever the qty entry = the entry in the qtyList //Note that the seed has to be {0} as {} or {null} + any number => null //So we make the seed a {0} and remove that first entry when done generating the list. palletList = List.RemoveFirstN( List.Accumulate({0..List.Count(qty)-1},{0},(state,current)=> if qtyList{current} = null then state & {null} else if qtyList{current} = qty{current} then state & {List.Max(state)+1} else state & {List.Max(state)} ),1), //add to table tbl = Table.FromColumns( Table.ToColumns(#"Changed Type") & {palletList}, Table.ColumnNames(#"Changed Type") & {"Pallet List"}) in tblResults
smpa01
Community Champion
4 years agojmontes1810 can you please show what is your desired output for the dataset you provided?
jmontes1810
4 years agoFrequent Visitor
smpa01
This is what we actually get...
This is the desired output
| DN | QTY | Helper | Desired Count |
| 3961178426 | 10 | 10 | 1 |
| 3961210671 | 10 | 20 | 1 |
| 3961254067 | 10 | 30 | 1 |
| 3961275157 | 10 | 40 | 1 |
| 3961377408 | 10 | 50 | 1 |
| 3961578272 | 10 | 60 | 1 |
| 3962085691 | 10 | 70 | 1 |
| 3962464346 | 10 | 80 | 2 |
| 3964382459 | 10 | 90 | 2 |
| 3964382465 | 10 | 100 | 2 |
| 3964681873 | 10 | 110 | 2 |
| 3964733826 | 10 | 120 | 2 |
| 3964787919 | 10 | 130 | 2 |
| 3964907400 | 10 | 140 | 2 |
| 3964914982 | 10 | 150 | 2 |
| 3966179015 | 10 | 160 | 3 |
| 3966243202 | 10 | 170 | 3 |
| 3961079096 | 20 | 190 | 3 |
| 3961079104 | 20 | 210 | 3 |
| 3961322655 | 20 | 230 | 3 |
| 3964714777 | 20 | 250 | 4 |
| 3964892006 | 20 | 270 | 4 |
| 3966154221 | 20 | 290 | 4 |
| 3964580786 | 30 | 320 | 5 |
| 3966243208 | 30 | 350 | 5 |
| 3966246800 | 30 | 380 | 6 |
| 3964580778 | 40 | 420 | 6 |
| 3964056235 | 50 | 470 | 7 |
| 3964712764 | 50 | 520 | 8 |
| 3964712772 | 50 | 570 | 9 |
| 3964825696 | 80 | 0 | |
| 3965997185 | 680 | 0 |
- ronrsnfld4 years ago
Super User
You can create a "conditional" running total using List.Generate; then use List.Accumulate to develop the Pallet List.
Data
let Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"DN", type text}, {"QTY", Int64.Type}}), //generate list of pallets //replace >=80 with nulls to satisfy that condition qty= List.Buffer(List.Transform(#"Changed Type"[QTY], each if _ >=80 then null else _)), //Generate a running total of the qtys that start over when there is a null // (ie an entry that was >=80) or when the running total reaches a Max of 80 qtyList = List.Generate( ()=>[qt= qty{0}, idx=0], each [idx] < List.Count(qty), each [qt= if qty{[idx]+1} = null then null else if [qt] = null then qty{[idx]+1} else if [qt] + qty{[idx]+1} <=80 then [qt] + qty{[idx]+1} else qty{[idx]+1}, idx = [idx]+1], each [qt]), //generate the Pallet list by incrementing the list whenever the qty entry = the entry in the qtyList //Note that the seed has to be {0} as {} or {null} + any number => null //So we make the seed a {0} and remove that first entry when done generating the list. palletList = List.RemoveFirstN( List.Accumulate({0..List.Count(qty)-1},{0},(state,current)=> if qtyList{current} = null then state & {null} else if qtyList{current} = qty{current} then state & {List.Max(state)+1} else state & {List.Max(state)} ),1), //add to table tbl = Table.FromColumns( Table.ToColumns(#"Changed Type") & {palletList}, Table.ColumnNames(#"Changed Type") & {"Pallet List"}) in tblResults
- smpa014 years ago
Community Champion
jmontes1810 will respond back to you
- smpa014 years ago
Community Champion
jmontes1810 sorry I can't seem to resolve this
ronrsnfld are you able to take a look please ?
- ronrsnfld4 years ago
Super User
I posted a possible solution using List.Generate and List.Accumulate. Thanks for the ping.