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 / Calculating Moving Ran...
Power Apps
Answered

Calculating Moving Range Column In A Collection

(0) ShareShare
ReportReport
Posted on by 162
I have a collection with multiple columns. I have a DATE column and a RESULT column.
 
I need to figure out how to add a column that will calculate the difference between one row and the row before it for the RESULT column. Below I have indicated this as a DIFFERENCE column. I also need them to be absolute values but I think I can figure that out, I'm just having issues figuring out how to compare the 2 rows. 
 
Date Result Difference
01/01/2024 10  
02/01/2024 12 2
03/01/2024 15 3
04/01/2024 18 3
 
Thanks in advance
Categories:
I have the same question (0)
  • Verified answer
    Nandit Profile Picture
    1,568 Moderator on at
    Hi 
     
    Here's how you can do it. I have added a row number to reference the previous item.
    ClearCollect(
        testCol,
        {
            Date: "01/01/2024",
            Result: 10
        },
        {
            Date: "02/01/2024",
            Result: 12
        },
        {
            Date: "03/01/2024",
            Result: 15
        },
        {
            Date: "04/01/2024",
            Result: 18
        }
    );
    ClearCollect(
        testCol,
        ForAll(
            Sequence(CountRows(testCol)),
            Patch(
                Last(
                    FirstN(
                        testCol,
                        Value
                    )
                ),
                {RowNumber: Value}
            )
        )
    );
    ClearCollect(
        testCol2,
        AddColumns(
            testCol,
            Difference,
            If(
                ThisRecord.RowNumber = 1,
                0,
                ThisRecord.Result - Index(
                    testCol,
                    ThisRecord.RowNumber - 1
                ).Result
            )
        )
    )
     
    Here's how it looks:
    Hope this helps. 
     
    Kind regards, 
    Nandit
     
    If this answers your query, please mark this response as the answer.
    If its helpful, please leave a like. Thanks!
     
  • PeterDonohue Profile Picture
    162 on at
    Hey that worked great. Thanks so much!
     

  • Nandit Profile Picture
    1,568 Moderator on at
     
    Glad it worked. 
     
    What exactly is happening in the following part:
    Patch(
        testCol2,
        colAvgmR.ID,
        colAvgmR
    );
     
    I didn't get this part. It's giving me the same error. The syntax should be like this:
    Patch(testCol2, Item to Patch OR Defaults(testCol2), {column: ValueToPatch})
     
     
    Not sure how the first part is working for you. 
     
     
    Steps I understand:
    1. You are calculating the average
    2. Creating a new collection with Record IDs and putting average value against all
    3. ...
     
     
    It would be helpful if you could type the steps here or explain the two patch statements. 
     
     
    Thanks & regards,
    Nandit
     
    If this answers your query, please mark this response as the answer.
    If its helpful, please leave a like. Thanks!
     
  • PeterDonohue Profile Picture
    162 on at
    So the code has been working fine up until a few minutes ago, now the line 
     
    {RowNumber:Value}
     
    gives an error "The type of this argument 'RowNumber' does not not match the expected type 'ObjNull'. Found type 'Number'
     
    Any thoughts?
  • Nandit Profile Picture
    1,568 Moderator on at
     
    Where are you using this code? one place or multiple places? And how?
     
    Kind regards,
    Nandit
  • Suggested answer
    SaiRT14 Profile Picture
    1,990 Super User 2025 Season 1 on at
    // Initialize testCol with data
    ClearCollect(
        testCol,
        [
            { Date: "01/01/2024", Result: 10 },
            { Date: "02/01/2024", Result: 12 },
            { Date: "03/01/2024", Result: 15 },
            { Date: "04/01/2024", Result: 18 }
        ]
    );
    // Add RowNumber to testCol
    ClearCollect(
        testCol,
        AddColumns(
            testCol,
            "RowNumber",
            CountRows(
                FirstN(testCol, CountRows(testCol))
            )
        )
    );
    // Add Difference column to testCol2
    ClearCollect(
        testCol2,
        AddColumns(
            testCol,
            "Difference",
            If(
                RowNumber = 1,
                0,
                Result - LookUp(testCol, RowNumber = ThisRecord.RowNumber - 1).Result
            )
        )
    );
     

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 505

#2
WarrenBelz Profile Picture

WarrenBelz 502 Most Valuable Professional

#3
Haque Profile Picture

Haque 324

Last 30 days Overall leaderboard