Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
jmontes1810
Frequent 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 List 2"
My required conditionals are:

  • Count or asign a number echa time the rows sum 80 or less, but not more than 80.
  • Only applyes for rows with qtys less than 80, reason why there's no count on the last two rows.


Picture2.jpg
 
I would really appreciate if you can help me Community, 
Thnks in advantage.

1 ACCEPTED SOLUTION

You can create a "conditional" running total using List.Generate; then use List.Accumulate to develop the Pallet List.

 

Data

 

ronrsnfld_0-1636423219448.png

 

 

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  
    tbl

 

 

 

Results

 

ronrsnfld_1-1636423376718.png

 

 

 

View solution in original post

19 REPLIES 19
smpa01
Super User
Super User

@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"

 

smpa01_0-1636058413343.png

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

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?

@jmontes1810  if you want to try out PQ, keep things unchanged from 

#"Removed Columns"
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

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 

@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.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

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

jmontes1810_1-1636130086791.png

 

DNQTYHelperCustom
396117842610101
396121067110201
396125406710301
396127515710401
396137740810501
396157827210601
396208569110701
396246434610802
396438245910902
3964382465101002
3964681873101102
3964733826101202
3964787919101302
3964907400101402
3964914982101502
3966179015101603
3966243202101703
3961079096201903
3961079104202103
3961322655202303
3964714777202504
3964892006202704
3966154221202904
3964580786303205
3966243208303505
3966246800303805
3964580778404206
3964056235504706
3964712764505207
3964712772505708
3964825696800 
39659971856800 

 

@jmontes1810  can you please show what is your desired output for the dataset you provided?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

@smpa01 
This is what we actually get...

jmontes1810_0-1636135240998.png

This is the desired output

jmontes1810_1-1636135377259.png

DNQTYHelperDesired Count
396117842610101
396121067110201
396125406710301
396127515710401
396137740810501
396157827210601
396208569110701
396246434610802
396438245910902
3964382465101002
3964681873101102
3964733826101202
3964787919101302
3964907400101402
3964914982101502
3966179015101603
3966243202101703
3961079096201903
3961079104202103
3961322655202303
3964714777202504
3964892006202704
3966154221202904
3964580786303205
3966243208303505
3966246800303806
3964580778404206
3964056235504707
3964712764505208
3964712772505709
3964825696800 
39659971856800 

You can create a "conditional" running total using List.Generate; then use List.Accumulate to develop the Pallet List.

 

Data

 

ronrsnfld_0-1636423219448.png

 

 

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  
    tbl

 

 

 

Results

 

ronrsnfld_1-1636423376718.png

 

 

 

@jmontes1810  will respond back to you

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

@jmontes1810  sorry I can't seem to resolve this 

@ronrsnfld  are you able to take a look please ?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

I posted a possible solution using List.Generate and List.Accumulate.  Thanks for the ping.

@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
    _2

 

You need to have an index column in the data in order for DAX to work

 

 

smpa01_0-1636059978029.png

 

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Greg_Deckler
Super User
Super User

@jmontes1810 Can you post sample data as text? You will need an Index column to define "before". Then something Cthulhu: Cthulhu - Microsoft Power BI Community


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thnks for your promp reply @Greg_Deckler 
Here are the sample data in text form, hoping it can help.
As my mortal view, I think Cthulhu cant works for what im seeking 'cause it count the times some same value/text appears in consecutive form due to an index order. 
My index order in this case is to sort from smaller to biger quantity. But, what about the sum conditional? To count each time the rows sum 80 and only for qtys less than 80.  I hope you can elp me.

DNQTYPallet List 2
3964811437            10                 1
3965357319            10                 1
3964870118            10                 1
3964811439            10                 1
3964870119            10                 1
3966266495            20                 1
3964892320            20                 2
3962033596            20                 2
3964892321            30                 2
3963548154            60                 3
3964892322            70                 4
3964730916            80-
3964906757          100-

 

@jmontes1810 Not sure QTY sort is going to work but will have to try some things.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Thnk you Greg, I hope you can find something that could work. 

@jmontes1810 If you add an index, try:

Column = 
    VAR __Limit = 80
    VAR __Value = SUMX(FILTER(ALL('Table8'),[Index]<=EARLIER('Table8'[Index])),[QTY])
RETURN
    TRUNC(DIVIDE(__Value,__Limit))+1

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg

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.

jmontes1810_0-1636129934739.png

 



Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.