Forum

Using a resetting t...
 
Notifications
Clear all

Using a resetting total to manage groupings

5 Posts
2 Users
0 Reactions
91 Views
(@bh3428)
Posts: 3
Active Member
Topic starter
 

I am trying to create a column that will reset the running total to 0 or the next value in the list when the running total value hits a specific number (10000 in this case)

So it is taking in a list of numbers, adding them I need to figure out why the validation of the current sum value isnt working. 

fxRunningGrouping

(values as list) as list =>
let
RT = List.Generate
(
()=> [RT = values{0}, counter = 0],
each [counter] < List.Count(values),
each if {[RT]} >= 10000 then [RT = values{[counter]}, counter = [counter]+1] else
//each if _>= 10000 then [RT = values{[counter]}, counter = [counter]+1] else
each [RT = [RT]+values{[counter]+1}, counter = [counter]+1],

each [RT]
)
in
RT

 
Posted : 10/07/2021 1:47 pm
(@catalinb)
Posts: 1937
Member Admin
 

Hi William,

Looking at:

each if {[RT]} >= 10000

The curly brackets indicate a list type, normally, you cannot compare a list to a number, you have to make sure you compare the same data types.

 
Posted : 11/07/2021 1:33 pm
(@bh3428)
Posts: 3
Active Member
Topic starter
 

So I added a column that just has 10000 as the value would I be able to do something like 

if [RT] >= [newColumn]  then [RT = values{[counter]}, counter = [counter]+1] else ....

 

The idea is to reset the current total to the current value in the list. Thus resetting the total. 

What happens if it doesnt reset is the addition doesnt come out right because the values can be anything. 

 
Posted : 13/07/2021 2:13 pm
(@catalinb)
Posts: 1937
Member Admin
 

Hi William,

Here is an example based on:

https://www.myonlinetraininghub.com/quickly-create-running-totals-in-power-query

>code>

(values as list) as list=>
let
RT = List.Generate
(
()=> [RT = values{0}, counter = 0],
each [counter] < List.Count(values),
each [RT = if [RT]>=10000 then 0 else [RT] + values{[counter] + 1}, counter = [counter] + 1],
each [RT]
)
in RT

If you don't want to reset to 0 but to the current row value (which makes more sense):

each [RT = if [RT]>=10000 then values{[counter] + 1} else [RT] + values{[counter] + 1}, counter = [counter] + 1],

 
Posted : 13/07/2021 11:38 pm
(@bh3428)
Posts: 3
Active Member
Topic starter
 

Thanks that worked exactly the way I needed it to.

 
Posted : 16/07/2021 3:11 pm
Share: