Forum Discussion
How to count each time some rows sum x number?
- 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
jmontes1810 load your data in PQ and that would result in Source followed by #"Changed Type". Once you have that and as long as the structure of the table is similar to the sample, you don't need to change anything till the end except pasting it in the query editor. If you prefer you can adapt the dax code as well.
Hi smpa01
It almost works, but look... Are some count where the column sum over than 80, like the 5 and 6, in these two are 90 pcs.
I attach the data bellow...
| DN | QTY | Helper | Custom |
| 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 | 5 |
| 3964580778 | 40 | 420 | 6 |
| 3964056235 | 50 | 470 | 6 |
| 3964712764 | 50 | 520 | 7 |
| 3964712772 | 50 | 570 | 8 |
| 3964825696 | 80 | 0 | |
| 3965997185 | 680 | 0 |
- smpa014 years ago
Community Champion
jmontes1810 can you please show what is your desired output for the dataset you provided?
- jmontes18104 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