web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / running total in a col...
Power Apps
Answered

running total in a collection

(0) ShareShare
ReportReport
Posted on by 34

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.. 
Categories:
  • BCLS776 Profile Picture
    8,994 Moderator on at

    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

  • Verified answer
    v-bofeng-msft Profile Picture
    Microsoft Employee on at

    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

     

  • Thrive Profile Picture
    34 on at

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

  • v-bofeng-msft Profile Picture
    Microsoft Employee on at

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 342 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 258

#3
Mohsin Ali Profile Picture

Mohsin Ali 231

Last 30 days Overall leaderboard