How to calculate the percentual difference of speeds between 2 consecutive runs or 2 consecutive hikes, in the Numbers.app?

Hello,


I have a Numbers 13.2 file on my M1 MacBook Air, running Sonoma, in which I have a 15-year data of my runs and hikes.


Firstly, I had 13 years and 4 months of only runs; it was followed by 1 year 8 months of only hikes and starting in September I have both, i.e., runs and hikes.


Among others, the spreadsheet has these columns, that function as 'fields' of a database:

i) in column C, the activity: Run or Hike;

ii) in column D, the 'distance';

iii) in column H, the 'duration';

iv) there are columns that automatically calculate the 'speed' -- 'Column I', in "m/s" & 'Column k', in "km/h" -- and the 'pace', 'Column N', in "min/km"; these calculations only appear "if" the 'date/time' -- ascending order -- is written;

v) I have several other columns with 'summaries' and another 'sheet' with extra 'summaries'.

vi) The 'Column M', '∆v', is a calculated 'field', that shows the % increase (in black) or % decrease (in red), between the "last speed" of 'Column K' and the "former speed" of 'Column K': "=IF(ISBLANK('Date/Time'),"",K2033÷K2032−1)".


This formula served me well until the the spreadsheet completed its 15 anniversary on Sep 7, because I was comparing the speed variation between runs or between hikes.

But now, that I could have a hike followed by a run, it makes no sense to know the ∆v between different activities.


The problem to be solved:

Every time that I write the data of an activity in 'Column C' -- "Run" or "Hike" -- I need a formula,  or an AppleScript script, to insert in the 'Column M', '∆v', that automatically calculate the '∆v' between the speed of the present cell of 'Column k' with the speed of former cell of 'Column k" that has the same activity.


Any light is welcome,


Jorge Lucas (the guy from Rio Grande do Sul)

MacBook Air (M1, 2020)

Posted on Oct 5, 2023 3:59 PM

Reply
Question marked as Top-ranking reply

Posted on Oct 5, 2023 5:53 PM

Below is another way.


In a new column, say column N, put the formula =ROW() in every data row (not needed in any headers). This will give every row its row number.


A formula to get the percentage increase in "hike" or "run" speed from column K would be

=K/INDEX(K,MAXIFS(N,C,C,N,"<"&N))-1


The MAXIFS part of the formula returns the maximum row number where column C has the same value as the current row (i.e,, "hike" or "run") and the row number in column N is less than the current row number. INDEX then gets the speed from column K in that row.


For upper rows where there is no earlier "hike" or "run", MAXIFS will return 0. INDEX(K,0) apparently gives the current row's value so the result of the formula will be a 0% increase.


For lower rows where there is no data, the formula will give an error (divide by 0 error). You can put IFERROR around the formula or handle it some other way.


These formulas will allow sorting, manual rearranging of rows, and deletion or addition of rows.



Similar questions

10 replies
Question marked as Top-ranking reply

Oct 5, 2023 5:53 PM in response to Jorge Lucas

Below is another way.


In a new column, say column N, put the formula =ROW() in every data row (not needed in any headers). This will give every row its row number.


A formula to get the percentage increase in "hike" or "run" speed from column K would be

=K/INDEX(K,MAXIFS(N,C,C,N,"<"&N))-1


The MAXIFS part of the formula returns the maximum row number where column C has the same value as the current row (i.e,, "hike" or "run") and the row number in column N is less than the current row number. INDEX then gets the speed from column K in that row.


For upper rows where there is no earlier "hike" or "run", MAXIFS will return 0. INDEX(K,0) apparently gives the current row's value so the result of the formula will be a 0% increase.


For lower rows where there is no data, the formula will give an error (divide by 0 error). You can put IFERROR around the formula or handle it some other way.


These formulas will allow sorting, manual rearranging of rows, and deletion or addition of rows.



Oct 5, 2023 5:24 PM in response to Jorge Lucas

Create two new columns to count the runs and hikes. In my example they are O and P with headers Run and Hike, it's important they match the texts you enter in column C.




Enter in O2, then drag-copy to P2:

=IF(COUNTMATCHES($C2,O$1)>0,COUNTMATCHES(TEXTJOIN(",",TRUE,OFFSET($C$1,1,0,ROW()−1,1)),O$1),"")


Enter in M2:

=IFERROR(IF(ISBLANK('Date/Time'),"",IF(O2≠"",K2÷XLOOKUP(O2−1,run,'Km/h',0,0)−1,K2÷XLOOKUP(P2−1,Hike,'Km/h',0,0)−1)),"")


Then copy down those three formulas in their respective columns.


Explanations:

The count of runs and hikes is established by concatenating the content of the C column from row 2 to the row of the formula and counting how many times the column header appears in the concatenated text.

XLOOKUP then looks for that value minus 1 to establish the row of the previous similar event, from which it extracts the speed which is compared to the speed on the current row.

Oct 6, 2023 12:05 PM in response to Jorge Lucas

It does not matter that the first data row is the third row and the first number is 3.


You need the column of ROW() formulas so that MAXIFS has something to find the maximum of. It looks in that column for the maximum number that is less than the one in its own row and where the value in column C is the same as in its own row. You can hide the column of row numbers after you have everything set up and working.


There are other ways to approach the problem that that do not require the extra column but they have other drawbacks. They either require OFFSET (or other function like that) or they are not sortable and rows cannot be moved, added, or deleted without redoing the column of formulas.


Here is an OFFSET version, starting at row 4:


M4 =C4/XLOOKUP(C4,OFFSET(C$1,2,0,ROW()−3,1),OFFSET(K$1,2,0,ROW()−3,1),K4,0,−1)-1


This will work in all rows except row 3. For that row (the first of your data rows), you can replace it with 0%. If you add or remove a header row, the formula needs to be edited. I think the column of row numbers makes for a simpler, easier to understand formula that is less prone to errors/bugs but some would rather not have extra columns. If you will never insert rows between other rows or delete rows or sort the table or move rows around manually, you can use a much simpler formula but you might find you forgot to make an entry in the table and you will not be able to insert a row for it.


M4 =XLOOKUP(C4,C$3:C3,K$3:K3,0,0,−1)

Oct 6, 2023 10:28 AM in response to Recycleur

Recycleur,


Thanks for your reply, although I could not make it work because I will be busy until next Monday.

What happens is that my spreadsheet has, today, 2035 entries and I would have to adapt your formulas to paste them on the first rows; besides that, I have a feeling -- I am not sure of this -- that without wanting you wrote a comma "," in the formula to be pasted in 'Column O', and replicated in 'Column P'.

That comma is between "TEXTJOIN" and "true".

Anyway, I will study this, and I writing you before because you spent your time, trying to help me.


Jorge Lucas (the guy from Rio Grande do Sul)


Oct 6, 2023 10:42 AM in response to Badunit

Badunit,


Although I am busy, your formula was easy to test and it worked.

But since I have the headers in 2 lines, the first row to insert data was the 3rd.

When I created a column with "Row()", the first number that appeared was "3".

This means that the "Column N" is equal to the column of the green circumference, that identify each row by default.

Then, comes a question: the creation of that extra column was really necessary?

Or I can not refer to it in a formula?


Thanks for your time.


Jorge Lucas (the guy from Rio Grande do Sul)

Oct 6, 2023 12:28 PM in response to Jorge Lucas

The comma is there on purpose, it's the separator for all joined values, the first argument of the TEXTJOIN function.


I wonder why you say that the formula would have to be adapted for the first rows (except changing the 2's for 3's since you have two header rows). Did you change the format of the rows at one point?


Anyway, kudos to Badunit for an amazing formula. So simple and elegant. However I still can't wrap my head around the fact that a range can be used for the MAXIFS criterias (third and fifth parameters). It works (as do also single cells on the row of the formula), so given a range Numbers will just take the cell on the current row! A non-documented trick to remember, for sure.

Oct 6, 2023 4:32 PM in response to Recycleur

However I still can't wrap my head around the fact that a range can be used for the MAXIFS criterias (third and fifth parameters). It works (as do also single cells on the row of the formula), so given a range Numbers will just take the cell on the current row! A non-documented trick to remember, for sure.


I'm sure it is documented somewhere, or at least it used to be. If a function requires a singular value, it will interpret it as a single cell in the current row (or in the current column if the reference was to a row like 3:3). If it is a range parameter, it will be interpreted as a range. I don't know if it is ever necessary to use it like this but it is a neat way to write a formula that can be pasted into all rows without change. One thing it does let you do is pick up the cell from the referenced column and drag it elsewhere without the reference following it around. Not that that matters here but it is one feature of it.

Oct 6, 2023 5:45 PM in response to Recycleur

Recycleur,


I had to do some changes to my 2-line header: the 'Run' and the 'Hike' were in the 2nd row of the header and I putted them on the 1st; same for the 'Date/Time', that was also in the 2nd row and was in Portuguese (my native language). The "Run" and the "Hike", of the column 'Activity' were already in English, because they are smaller words than their correspondents in Portuguese ("*******" and. "Caminhada').


After these changes I was able to get the ordinal number of each run and each hike -- that the Badunit's solution did not bother to get (also because I did not ask for), but it was an extra data that I got and I will keep that in these 2 extra columns. So, I asked for 1 data and I am getting an extra data.


The 3rd formula you sent me, although I changed the 2's for 3's did not get the result I was expecting; maybe I need more time to study and adapt with more care, and I will try to get it right -- although the Badunit's solution is already running well. The thing is that I want more than the solution; I want to understand the formulas.

By the way, the sophisticated solution of Badunit just writing the column of a cell of the same row of another appeared in several examples of the Numbers.app.


Thanks again, for clarifying your solution.


Jorge Lucas (the guy from Rio Grande do Sul)

Oct 6, 2023 6:11 PM in response to Badunit

Badunit,


I am satisfied with your sharp formula, and your reason to choose it is sufficient.


Since I saw your 'name' here, it came to my memory the name of a Brit Rock'n'Roll band that appeared in 1973: Bad Company. Did it serve as an inspiration for you?


As an user of ClarisWorks/AppleWorks since 1999, I was away from this forum for a lot of years (around 10), and I still miss that software because it includes the 'Database Mode' -- that Apple never included in the iWork Suite, and FileMaker Pro is so expensive.


I congratulate you, Recycleur and all the others that keep this forum alive -- and I can see the immense reward that you get when you provide a solution to an unknown person.

As a retired High School Math teacher, the satisfaction it is not measured by the money we got in return for our work.


Thanks again,


Jorge Lucas (the guy from Rio Grande do Sul)



Oct 6, 2023 7:02 PM in response to Jorge Lucas

Bad Company is a great band and I listened to them a lot back in the day but "bad unit" was something a friend of mine said back in the 80's when talking about a broken piece of equipment. It might have been from a line in the movie Short Circuit but I don't know for sure. I used it as a handle for games way back then and for other things along the way.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to calculate the percentual difference of speeds between 2 consecutive runs or 2 consecutive hikes, in the Numbers.app?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.