cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Thrive
Frequent Visitor

running total in a collection

How do I add a running total to an existing collection below?  The running total aggregates year after year within the same service.  Aka, running total calculates for Service 1 from years 2021 thru 2024, then calculates a new running total for Service 2 for the same years.  The example column in the table below illustrates what I want to achieve.

 

ServiceYearDataRunning Total column example that I want to add to this collection
Service 120211010
Service 120221020
Service 12023525
Service 12024227
Service 2202111
Service 2202212
Service 2202368
Service 22024311
etc...etc...etc.. 
1 ACCEPTED SOLUTION

Accepted Solutions
v-bofeng-msft
Community Support
Community Support

Hi @Thrive :

I've made a test for your reference:

1\I assume there is a table:

ClearCollect(
  TheCollection,
 {Service:"Service1",Year:2021,Data:10,'Running Total column example that I want to add to this collection':0},
 {Service:"Service1",Year:2022,Data:10,'Running Total column example that I want to add to this collection':0},
 {Service:"Service1",Year:2023,Data:5,'Running Total column example that I want to add to this collection':0},
 {Service:"Service1",Year:2024,Data:2,'Running Total column example that I want to add to this collection':0},
 {Service:"Service2",Year:2021,Data:1,'Running Total column example that I want to add to this collection':0},
 {Service:"Service2",Year:2022,Data:1,'Running Total column example that I want to add to this collection':0},
 {Service:"Service2",Year:2023,Data:6,'Running Total column example that I want to add to this collection':0},
 {Service:"Service2",Year:2024,Data:3,'Running Total column example that I want to add to this collection':0}
)

vbofengmsft_1-1634009678086.png

 

2\Add a timer control:
 Duration

500

OnSelect

Set(Totoladd,0);Set(LastSeviceName,"");Set(CountNumber,1);

OnTimerEnd

Set(CurrentItem,Last(FirstN(TheCollection,CountNumber)));
Set(ServiceName,CurrentItem.Service);
If(
    CurrentItem.Service=LastSeviceName,
    Set(Totoladd,Totoladd+CurrentItem.Data),
    Set(Totoladd,CurrentItem.Data)
);
Patch(
     TheCollection,
     CurrentItem,
     {'Running Total column example that I want to add to this collection':Totoladd}
);
Set(LastSeviceName,CurrentItem.Service);
Set(CountNumber,CountNumber+1);

Repeat

CountNumber<CountRows(TheCollection)

Finally,select the timer control and you will get the result:

vbofengmsft_0-1634009658933.png

Best Regards,

Bof

 

View solution in original post

4 REPLIES 4
BCLS776
Super User
Super User

It's not exactly the answer to your question, but I wouldn't add a column specifically to do this. Instead, I would use a Sum() & Filter() together to calculate the total where/when I needed it:

Sum(
    Filter(myCollection, Service = "Service 1", Year <= 2023),
    Data
)

This would calculate a value of 25 for Service 1 up to year 2023, assuming the Year column is a number. If you like, you can plus in control values or variables where this code indicates the Service and Year values.

 

Hope that helps,

Bryan

_________________________________________________________________________________________
Help the community help more users by choosing to "Accept as Solution" if this post met your needs. If you liked the post and want to show some appreciation, please give it a Thumbs Up.
v-bofeng-msft
Community Support
Community Support

Hi @Thrive :

I've made a test for your reference:

1\I assume there is a table:

ClearCollect(
  TheCollection,
 {Service:"Service1",Year:2021,Data:10,'Running Total column example that I want to add to this collection':0},
 {Service:"Service1",Year:2022,Data:10,'Running Total column example that I want to add to this collection':0},
 {Service:"Service1",Year:2023,Data:5,'Running Total column example that I want to add to this collection':0},
 {Service:"Service1",Year:2024,Data:2,'Running Total column example that I want to add to this collection':0},
 {Service:"Service2",Year:2021,Data:1,'Running Total column example that I want to add to this collection':0},
 {Service:"Service2",Year:2022,Data:1,'Running Total column example that I want to add to this collection':0},
 {Service:"Service2",Year:2023,Data:6,'Running Total column example that I want to add to this collection':0},
 {Service:"Service2",Year:2024,Data:3,'Running Total column example that I want to add to this collection':0}
)

vbofengmsft_1-1634009678086.png

 

2\Add a timer control:
 Duration

500

OnSelect

Set(Totoladd,0);Set(LastSeviceName,"");Set(CountNumber,1);

OnTimerEnd

Set(CurrentItem,Last(FirstN(TheCollection,CountNumber)));
Set(ServiceName,CurrentItem.Service);
If(
    CurrentItem.Service=LastSeviceName,
    Set(Totoladd,Totoladd+CurrentItem.Data),
    Set(Totoladd,CurrentItem.Data)
);
Patch(
     TheCollection,
     CurrentItem,
     {'Running Total column example that I want to add to this collection':Totoladd}
);
Set(LastSeviceName,CurrentItem.Service);
Set(CountNumber,CountNumber+1);

Repeat

CountNumber<CountRows(TheCollection)

Finally,select the timer control and you will get the result:

vbofengmsft_0-1634009658933.png

Best Regards,

Bof

 

View solution in original post

@v-bofeng-msft , this is awesome!  Just curious, is there an advantage or need to tie this to a timer?

Hi @Thrive :

Because Canvas App itself does not have a fully functional function to build a loop (the Forall function has a relatively large limit), it is necessary to use the timer control to build this loop.

Best Regards,

Bof

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (3,003)