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 can you try this
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ndJLDsIwDATQq6Csi2R7/InPUvX+14BSqKipQCFLa54msTLPDenamRXRpnZ5O0xlsE3bMj2QwQKcY0h7EHMfRev1/moaRC7umlYy8qspBR+ZcyQvJARY+hjamrhk8B3B7vszLRk/RTg2ScnEKdIdBSi5vqmv6Lpnkjzs+NWYnpHlBg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DN = _t, QTY = _t, #"Pallet List 2" = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"DN", Int64.Type}, {"QTY", Int64.Type}, {"Pallet List 2", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Pallet List 2"}),
Value = #"Removed Columns"[QTY],
Loop = List.Generate(
()=>[i=0,k=if Value{i}>=80 then 0 else Value{i}],
each [i]<List.Count(Value),
each [i=[i]+1, k=if Value{i}>=80 then 0 else Value{i}+[k]],
each [k]
),
Custom1 = Table.FromColumns(Table.ToColumns(#"Changed Type")&{Loop},List.Combine({Table.ColumnNames(#"Changed Type"),{"Helper"}})),
#"Added Custom" = Table.AddColumn(Custom1, "Custom", each if [QTY]<80 then Number.IntegerDivide([Helper],80)+1 else null)
in
#"Added Custom"
- smpa014 years ago
Community Champion
jmontes1810 in DAX
Column = VAR _0 = CALCULATE ( MAX ( tbl[Index] ) ) VAR _1 = IF ( tbl[QTY] >= 80, 0, CALCULATE ( SUM ( tbl[QTY] ), FILTER ( ALL ( tbl ), tbl[Index] <= _0 ) ) ) VAR _2 = IF ( tbl[QTY] < 80, QUOTIENT ( _1, 80 ) + 1, BLANK () ) RETURN _2You need to have an index column in the data in order for DAX to work
- jmontes18104 years agoFrequent Visitor
Im about to try it...
Just to confirm, in my new table, in the advanced editor I just need to paste the code youre providen me but replacing each "Table.XXX" for the name of my table uploaded table. Right?- smpa014 years ago
Community Champion
jmontes1810 if you want to try out PQ, keep things unchanged from
#"Removed Columns"- jmontes18104 years agoFrequent Visitor
I think the PQ form works, but, when I use the code, the data just retrive me the data of this example table. Now, what about if i want to use another Sheet/data? What should I do or change? My data wil be updated daily