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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Jagen007
Regular Visitor

Create a calculated column by creating a loop.

Given Maximum Qty in a pallet is 20.

 

I would like to created a loop in power query where by when each row is added downward and when the sum qty is more than 20 , the rows should be group (Pallet) and the loop run again.

 

Example:

Input

Material                                Qty                        
ABC-12310
ABC-1245
ABC-1256
ABC-1264
ABC-12710
ABC-12813
ABC-12915

 

Output

Material                             Qty                       Pallet                   
ABC-123101
ABC-12451
ABC-12562
ABC-12642
ABC-127102
ABC-128133
ABC-129153

 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Jagen007 

 

Download example file with the calculations shown below

 

Based on what you've described : when the sum qty is more than 20 the rows should be given the same pallet number.

Based on that, the table would look like this

 

Material                              Qty                        Pallet                   
ABC-123 10 1
ABC-124 5 1
ABC-125 6 2
ABC-126 4 2
ABC-127 10 2
ABC-128 13 3
ABC-129 15 4

 

To do this you can add an Index column, starting at 1, and use this to create the column

 

= Number.RoundUp(List.Sum(List.FirstN(#"Added Index"[Qty], [Index]))/20)

 

pallet.png

 

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

3 REPLIES 3
PhilipTreacy
Super User
Super User

Hi @Jagen007 

 

The code does this:

 

  • Use List.FirstN to get the values in Qty column up to that row (using the value of the Index column on that row to sum to that row)
  • List.Sum sums these values
  • Divide by 20
  • The resultant number is round up to the nearest whole numer - this is the result

 

Regards

 

Phil

  •  


Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


PhilipTreacy
Super User
Super User

Hi @Jagen007 

 

Download example file with the calculations shown below

 

Based on what you've described : when the sum qty is more than 20 the rows should be given the same pallet number.

Based on that, the table would look like this

 

Material                              Qty                        Pallet                   
ABC-123 10 1
ABC-124 5 1
ABC-125 6 2
ABC-126 4 2
ABC-127 10 2
ABC-128 13 3
ABC-129 15 4

 

To do this you can add an Index column, starting at 1, and use this to create the column

 

= Number.RoundUp(List.Sum(List.FirstN(#"Added Index"[Qty], [Index]))/20)

 

pallet.png

 

 

Regards

 

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Hi Philip, 

 

thank you very much for giving the solution as it working but can you please explain the formula/step ?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.