Finding max of a range that fits a specific criteria like "MAXIF"

I have a table in Numbers that is sort of like a timeline tracking changes to various documents. Simplified it looks something like this …

User uploaded file


On another table I want to write a formula that will return the date a particular document was most recently modified. Basically I want to find a max where the range is all cells in Column B where Column A is “document title” (eg DOCUMENT A).


I haven't been able to get the IF function or reference functions to return a range that I can put inside a MAX.

Posted on May 6, 2018 7:29 PM

Reply
Question marked as Top-ranking reply

Posted on May 7, 2018 12:11 PM

pnthrr wrote:


I rather not have my formula be dependent on how the table is sorted


Then how about something like this?


User uploaded file


The formula in B2 of Table 1, filled down:


=COUNTIFS(A,A2,C,">"&C2)


This will return a value of 0 if there is no "larger" (later) date for that document name. So that gives you the most recent date for that document.


The formula in B2 of Table 2, filled down:


=SUMIFS(Table 1::C,Table 1::A,A2,Table 1::B,0)


This simply sums the dates for that document for which the value in the counts table is 0. 0 should be for the latest date only, so summing the date gives you that most recent date. Using SUMIFS (or AVERAGEIFS) is handy because it allows multiple criteria, much like MAXIFS in Excel.


If you are concerned you might have a particular document listed more than once on a specific date that might give you a spurious date, you could use AVERAGEIFS instead of SUMIFS.


SG

28 replies
Question marked as Top-ranking reply

May 7, 2018 12:11 PM in response to pnthrr

pnthrr wrote:


I rather not have my formula be dependent on how the table is sorted


Then how about something like this?


User uploaded file


The formula in B2 of Table 1, filled down:


=COUNTIFS(A,A2,C,">"&C2)


This will return a value of 0 if there is no "larger" (later) date for that document name. So that gives you the most recent date for that document.


The formula in B2 of Table 2, filled down:


=SUMIFS(Table 1::C,Table 1::A,A2,Table 1::B,0)


This simply sums the dates for that document for which the value in the counts table is 0. 0 should be for the latest date only, so summing the date gives you that most recent date. Using SUMIFS (or AVERAGEIFS) is handy because it allows multiple criteria, much like MAXIFS in Excel.


If you are concerned you might have a particular document listed more than once on a specific date that might give you a spurious date, you could use AVERAGEIFS instead of SUMIFS.


SG

May 8, 2018 8:10 PM in response to pnthrr

If I can assume this table is in order by date, maybe something like this would work for you:


User uploaded file


The formula for cell C2 is =IF(COUNTIF(A2:A$20,A2)=1,A2,"")

Fill down to complete the column

The cell reference of A20 in that formula is the last row in the table.

This formula is simply pointing out the lowest row that has the document name.


The formula in table 1-1 cell B2 is =LOOKUP(A2,Table 1::C,Table 1::B)

Fill down to complete the column


You could do it even more simply without having to add a column of formulas to your data table by relying on the property of LOOKUP whereby it does its search from the bottom up. In that case, the formula in Table 1-1 cell B2 would be =LOOKUP(A2,Table 1::A,Table 1::B). The lowest occurrence from the bottom will be the one returned.


One word of caution with LOOKUP: It may return a "close match" if not exact match is found. For instance, if you were looking for "Document M" and there is no "Document M", it might return the date for another document, such as "Document L". If this is a concern, INDEX(MATCH()) would be a better choice and you'll have to include the coilumn of formulas in your data table.


B2=INDEX(Table 1::B,MATCH(A2,Table 1::C,0))

May 7, 2018 10:04 AM in response to Yellowbox

Hi Ian,


I think that still wouldn’t work because I could probably only do one at a time? I need all of them simultaneously represented on a second table.


After reading through lots of threads on this issue, it’s pretty clear that there’s no good way to do this, which is a bit disappointing, because it would be very simple in Excel (I’m also lamenting the lack of CSE functions). However, I think I have found a way using IF/COUNTIF, followed by CONCATENATE, then VLOOKUP, which is a bit cumbersome, but will work.


The only problem I’m having is that COUNTIF doesn’t seem to be outputting the correct value for a few of my documents, but since the formula works for the other 90%, I think this must be something specific to those references and not an issue with the formula, so I think I'm good, I just need to go back over it to figure out what's wrong with the entries for these documents.

May 6, 2018 8:30 PM in response to Yellowbox

Thanks, but that won't work for me, because I have hundreds of documents, meaning I would have to add way too many columns to my spreadsheet. If that's the only solution, it would be better for me to manually update the last date modified (which I really rather not have to do).


I know I can sort the table, the point is I need the date to appear on a different table that has different summary information.

May 7, 2018 6:07 AM in response to pnthrr

Hi pnthrr,


I have hundreds of documents, meaning I would have to add way too many columns to my spreadsheet.

I was thinking that Cell C1 could be a Pop-Up Menu or a Copy and Paste to force the formula in Column C to "pick" a document name. No need for a Column for each document name!


What are you trying to do in a different table that has different summary information?


More information will lead to a solution!


Regards,

Ian.

May 8, 2018 8:09 PM in response to SGIII

I rather not have my formula be dependent on how the table is sorted since it will be useful to be able to change the sorting without messing up my other table.


What I did was an adaptation of what is suggested in this discussion:

need to simulate MAXIF.


User uploaded file


You can ignore columns D and F. In Column B I wrote a formula using IF and COUNTIF to return "R" for the most recent date of each document. Then in Column A I use CONCATENATE to combine Columns C and B to create a unique name for each entry with a most recent date. On my other table I can now write a formula like =VLOOKUP("DOC H3 WR,TABLE1::A:E,5) which will return 8/7/17. When I add a newer date "R" will move and so will the reference.


I am having a small problem. If you look at the two rows I bolded, Column B should have returned "R", however it is blank. This is because COUNTIF is returning 2 rather than 1. I think it might have something to do with the custom date formats I am using, because If I change it to Automatic it works. The weird thing is that I have used the same formats throughout the spreadsheet and sometimes they work and sometimes they don't. There's no correlation between the format and whether or not it works (the screenshot is only a small part of the data, all three formats have cases where they work and cases where they don't).

May 7, 2018 12:38 PM in response to SGIII

Hypothetically that should work and is a bit simpler. However, in practice it is returning more incorrect values than my other formula. I think the problem must somehow be how the dates are stored in my document, so I'm going to try to mess around with that and see if I can fix whatever is causing COUNTIF to read them wrong.


Thanks.

May 7, 2018 12:54 PM in response to pnthrr

pnthrr wrote:


Hypothetically that should work and is a bit simpler. However, in practice it is returning more incorrect values than my other formula.


You mention "incorrect" without giving a clear idea of the formulas you are using and the values in your table, or explaining why you think the results are "incorrect."


The results given by my most recent, and simpler, formula are "correct" based on the information you have posted about what you are trying to do. I test before posting. That's not "hypothetical."


If you think the suggested formulas don't give "correct" results, then why not post specifics so we can understand your difficulties?


SG

May 7, 2018 1:07 PM in response to SGIII

They're incorrect because I can see that they are wrong by doing the calculation myself. For example it might return a 0 for a row dated 12/10/16 when there is another row dated 12/16/16 that fits the same criteria for Column A (therefore should have returned a 1).


Like I said, I don't think the problem is your formula or my formula. The problem seems to be how I have entered the dates into the document. It doesn't seem to like that I have mixed multiple formats and might not be reading all of them as dates and times. I'm working on fixing that now. Once I get that figured out, both formulas should work perfectly.

May 7, 2018 1:55 PM in response to pnthrr

pnthrr wrote:


it might return a 0 for a row dated 12/10/16 when there is another row dated 12/16/16 that fits the same criteria for Column A (therefore should have returned a 1).



Some of the dates might not be recognized as dates (by default date-times are right-aligned) or it could also be that some of the document names are misspelled or have an extra space or other "invisible" character.


SG

May 7, 2018 3:59 PM in response to SGIII

The document names are all correct and I’m pretty sure all of the entries were proper dates, since there was no problem with sorting, and when I copy/pasted, then reformatted, everything (even the times) was exactly right.


I’m not entirely sure, but it seems the issue is how COUNTIF reads cell contents. If I have a cell whose value is 12/10/16, but I have it formatted as Dec 2016, I think COUNTIF reads it as “Dec 2016”, so the different formats mess it up and your formula sent back lots of “incorrect” values. My formula worked a bit better because I had already partially fixed the problem using DATEVALUE. However, DATEVALUE reads the cell that is formatted Dec 2016 as “12/1/16” not “12/10/16”, so in some cases I still had problems. It couldn’t recognize 12/16/16 as bigger than 12/10/16.


I have reasons for wanting to keep my formatting, but I created another column with uniform formatting to work around this. If I use this new column as the reference, both COUNTIF formulas work properly.

May 8, 2018 8:00 AM in response to pnthrr

pnthrr wrote:


I have reasons for wanting to keep my formatting, but I created another column with uniform formatting to work around this. If I use this new column as the reference, both COUNTIF formulas work properly.


Mysterious "formatting" preferences.🙂 It is useful to keep in mind that Numbers distinguishes between the value that is contained in a cell and the formatting used to display that value. For COUNTIFS it's a question of having valid date-time values in the cells. Anyway, it seems as if you've got your problem squared away, all by yourself, though it seems we'll just have to guess what you did.


SG

May 8, 2018 9:14 AM in response to SGIII

All I did was copy/paste all my dates to a new column and change the formatting from the various custom formats I had (2016, Dec 2016, 12 10 2016) to a single format (12/10/2016 0:00:00). I did not change any of the date or time values at all, so if COUNTIF reads the cell values, I have no idea why it wouldn’t read the first column but doesn’t have a problem with the new one, because the values are exactly the same. I guess why doesn’t really matter since there was an easy fix, but it would sort of be nice to understand.

May 8, 2018 9:30 AM in response to pnthrr

pnthrr wrote:


it would sort of be nice to understand.


To understand one would need to see more specifics than you seem willing to post.🙂


My guess is that COUNTIF is doing exactly as expected but you have something off in some of the dates. Why one would want to have a mixed format of dates in the same column is a mystery. If you have been able to get anything of value from what's been posted here (whether that is the case is another mystery) I would hope it would be that with spreadsheets maintaining consistency helps to obtain expected results.


SG

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.

Finding max of a range that fits a specific criteria like "MAXIF"

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