Average only the last X numbers in a column (basing on criteria)

  • Hi all,


    thank you for your ospitality in this forum.


    This is my first question:


    Let's say i have (column A and B as follow):


    A 12
    B 5
    C 6
    A 8
    D 4
    D 4
    A 3
    C 7
    B 1
    A 7
    A 9
    D 3
    C 4
    B 1


    I need to calculate average for text value A in column B (but not the whole average of the column for A (5 entries), only the LAST 3 (or N values) entries for A so -> 9-7-3). So the result should be 19/3=6,3333. Hope i was clear! Thank you for your help!

  • Re: Average only the last X numbers in a column (basing on criteria)


    Try next UDF while someone found a formula


    and somewhere put =AvgLast(A1:B14,"A",3)

    Triumph without peril brings no glory: Just try

  • Re: Average only the last X numbers in a column (basing on criteria)


    Another possibility
    =SUMPRODUCT(($B$1:$B$14)*(((($A$1:$A$14="A")*ROW($A$1:$A$14)))=LARGE((($A$1:$A$14="A")*ROW($A$1:$A$14)),{1,2,3})))/3
    Where you have to adjust
    {1,2,3} and /3 for 3 last numbers
    {1,2,3,4} and /4 for 4 last numbers
    etc...
    and ="A" for the string to search in column A

    Triumph without peril brings no glory: Just try

  • Re: Average only the last X numbers in a column (basing on criteria)


    Thank you to all of you, i've found this excellent solution for my case:


    =SUM(IF(ISNUMBER(MATCH(ROW($D$4:$D$3678);LARGE(IF($D$4:$D$3678=C1080;ROW($D$4:$D$3678));ROW(INDIRECT("1:"&AN1080)));0));$O$4:$O$3678))


    this was also to be used on another cell (AN1080 in my case) to reduce the number of entries to the minimum available (in case the occurrencies were less than 5):


    =MIN(5; COUNTIF($D$4:$D$3678; C1080))




    This functions are just amazing ...


    NOTE: The above function sum up the last 5 cell values (even if there are 50 occurrencies for that particular string), referring to a certain text string (present in cell C1080).

  • Re: Average only the last X numbers in a column (basing on criteria)


    =AVERAGE(SUBTOTAL(9,OFFSET(B3,LARGE(IF(A3:A16="A",ROW(A3:A16)-ROW(B3)+1),{1,2,3}),0,1)))
    or for N values
    =AVERAGE(SUBTOTAL(9,OFFSET(B3,LARGE(IF(A3:A16="A",ROW(A3:A16)-ROW(A3)+1),ROW(INDIRECT("A1:A"&C1))),0,1)))
    where C1 is the number of largest

  • Re: Average only the last X numbers in a column (basing on criteria)


    Quote from PCI;685145

    Does the first formula working for average ??


    No, it only sum up the last five occurrencies, you have to use the results of the other formula "=MIN(5; COUNTIF($D$4:$D$3678; C1080))" and divide it for the other to have the average.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!