Formula to determine max number in multiple dynamic arrays

jberylec

New Member
Joined
Jan 14, 2023
Messages
23
Office Version
  1. 2019
Platform
  1. Windows
This one is proving tough for me! Hope the experts can help :)

I need to create a formula in F2 that can be copied down through hundreds of location/batch rows, on a new version of this spreadsheet each day. The formula needs to find the max number in D but only for each location/batch section and report only that one into F on the same row. I've manually typed them in F just for display purposes.

A and B rows are always merged for that entire location and batch, as shown. So I was trying to build a formula that would recognize each time a row in B isn't blank <>"" which indicates a new location/batch starts here, then include that row and all blank rows below it (they're merged so treated as blank cells) to find the MAX number in D and show that in F. Then of course it starts over IF B isnt blank again.

In case of a tie, show both.

FYI, there are a lot more columns of data, not relevant for this though because none can be used as an identifier, I also have many other formula columns that I paste into row 2 each morning, then drag them all down the entire sheet, so trying to keep this just a formula not VBA. Hoping to add this new column to my daily paste and drag routine. Ah. Exhausting just describing this. Ha! Thanks in advance
 

Attachments

  • excel.png
    excel.png
    20.7 KB · Views: 20
.. the formula popup I talked about in post #4 and included a screenshot.
Ah, I see now. You can turn off 'Allow editing directly in cells' via File - Options -Advanced as shown below. You then edit in the formula bar instead. (I always have that setting off)

1673914136334.png
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Is there a small change possible for the INFA formula in column H to treat blanks in D as lowest instead of highest? All else seems to working very good :) Thanks in advance!
 
Upvote 0
Try
Excel Formula:
=IF(D2="","",IFNA(MATCH(COUNTIFS(E$2:E$1000,E2,D$2:D$1000,">"&D2)+1,{1,2,3},0),""))
 
Upvote 0
Hi again Peter, yes thats how I was doing it, but the problem is that just hides the result instead of excluding it from taking up the 1st spot. So the remaining results are inaccurate because it only show 2nd highest and 3rd highest, because the 1st highest was simply just hidden by the "". If there's a way to instead throw a third criteria in the match or count? Something like D2<>""

Also, I'm using this brilliant formula in reverse in other areas to find the lowest numbers per location by switching the ">" to "<", which works great! But having a similar issue where I have zeros in the data and it treats 0 as the lowest, which is technically true, but I need to somehow ignore those. So if there's a solution to the above issue, like an additional criteria, then I can use that same solution to ignore zeros here too...

Many thanks my friend!
 
Upvote 0
Hi again Peter, yes thats how I was doing it, but the problem is that just hides the result instead of excluding it from taking up the 1st spot. So the remaining results are inaccurate because it only show 2nd highest and 3rd highest, because the 1st highest was simply just hidden by the "".
Perhaps I am not understanding what you are saying but I don't believe that I am getting what you describe.
For the sample data below, which results are incorrect in columns G or H?

Otherwise, can you provide some XL2BB sample data that demonstrates the problem?

jberylec.xlsm
ADEFGH
1LocationMaxMaxTop3Top3
21861 862
387187871
4841 843
51   
62772 772
7712   
82   
92   
10742 743
1181281811
121973 972
1303   
1498398981
15903   
16953 953
174624 623
184   
194   
204   
214   
224   
23674 672
2470470701
Max per group (3)
Cell Formulas
RangeFormula
E2:E24E2=E1+(A2<>"")
F2:F24F2=IF(D2=MAXIFS(D$2:D$1000,E$2:E$1000,E2),D2,"")
G2:G24G2=IF(D2>=AGGREGATE(14,6,D$2:D$1000/(E$2:E$1000=E2),3),D2,"")
H2:H24H2=IF(D2="","",IFNA(MATCH(COUNTIFS(E$2:E$1000,E2,D$2:D$1000,">"&D2)+1,{1,2,3},0),""))
 
Upvote 0
In the live spreadsheet, D is a formula with a string of IFs, the final value when false is ""
Example (but much longer formula): IF(this>that,this,"")

When D results in "" the IFNA in both G and H are treating it as the max value in range D. I cant think of a way to paste the XL2BB without including a bunch of columns that the formula in D references.

In short, if the D cell is actually blank, like in the above XL2BB example, or if I delete the formula from the cell, then IFNA is behaving correctly, but if its blank as in the resulting "" value from the IF formula then IFNA is treating it as text or something else greater than the actual numbers in the rest of the D range.

So, to get around that, instead of using "" as my final value when false, I changed it to zero, for example: IF(this>that,this,0)
But this introduces the second problem I was referring to where these zeros are now reported as my smallest value in the range, rather than the actual smallest I'm looking for. So I'd rather keep the "" but have it treated as a blank, not the max value.

Sorry if I'm not making sense :cry:
 
Upvote 0
When D results in "" the IFNA in both G and H are treating it as the max value in range D.
Not for me.

jberylec.xlsm
ADEFGHIJK
1LocationMaxMaxTop3Top3
21861 86286
38718787187
4841 84384
5 1   
62772 77277
7712   71
8 2   
9 2   
10742 74374
118128181181
121973 97297
13 3   0
149839898198
15903   90
16953 95395
174624 62362
18 4   
19 4   
20 4   
21 4   
22 4   
23674 67267
247047070170
Max per group (3)
Cell Formulas
RangeFormula
D2:D24D2=IF(K2,K2,"")
E2:E24E2=E1+(A2<>"")
F2:F24F2=IF(D2=MAXIFS(D$2:D$1000,E$2:E$1000,E2),D2,"")
G2:G24G2=IF(D2>=AGGREGATE(14,6,D$2:D$1000/(E$2:E$1000=E2),3),D2,"")
H2:H24H2=IF(D2="","",IFNA(MATCH(COUNTIFS(E$2:E$1000,E2,D$2:D$1000,">"&D2)+1,{1,2,3},0),""))


Check your column D formula. I suspect that instead of
IF(this>that,this,"")
I think that you may have this
IF(this>that,this," ")
 
Upvote 0
Yes I see it working in yours. But not mine. Its not a space in the "", checked that long ago. The only thing I can think of is maybe because my IF formula in D is much more complex than this simple example. For instance, mine starts with the blank, then moves on to check many things before giving the result, like this: IF(THIS,"",IF(AND(THAT,THIS),"",IF(YADA,"",IF(OR(YEAH,AND(YAH,NAH)),"", and finally if I get through these tests give me the number in K2 )))

The formulas all work well, other than their results are not being treated correctly by countifs and ifna, etc. I'll see if I can work up an example of this problem. Any way I can copy this sheet from the thread? I cant for the life of me find the original, I must've deleted. Bummer

Thanks again for all your help!
 
Upvote 0
The length/complexity of the formula should make no difference as my formulas use the result of the formula only.
In blank cells in the sheet put these formulas and point them at one of the column D cells where the blanks are not being ignored. Report the results.

=LEN(D5)
=CODE(D5)

Any way I can copy this sheet from the thread?
You can copy my sheet to a blank worksheet by clicking this icon at the top left of my sheet, selecting A1 in your blank sheet, Paste.

1674799328673.png


Can you post a small section from your actual worksheet (first disguising any sensitive data and/or hiding any irrelevant columns/rows) with XL2BB?
 
Upvote 0
Ok I'll work on a live example, but its difficult because the cells referenced are spread out so far the XL2BB wont paste it all, and if I only grab the relevant section they all turn to #REF because the missing cells. So anyhow, for now until I mock up a new sheet, here's a screen snip of the live data.

This is an example of the second issue I was talking about in earlier posts, which could be just me misusing/modifying this formula. I changed the > to a < to try to get the lowest instead of the highest. Its counting blanks as the lowest. On the right you have the LEN, CODE, and the IFNA. They're all referencing the far left column, which in this case is column AZ, where the 62.50 should be the only 1 lowest, but you can see Excel is counting the blanks as the lowest also. I've triple checked the AZ cells, they are all resulting in "" because they all fail the IF formulas. All rows were copied down, so they're exactly the same formulas.

Here's the formula in the far right column: IFNA(MATCH(COUNTIFS(BF$2:BF$1500,BF112,AZ$2:AZ$1500,"<"&AZ112)+1,{1,2},0),""))

BF is our anchor column, they all have the same 14, and I've checked the entire spreadsheet for any weirdness, nope, this part of the range creation is working perfectly. There are no other 14's in the sheet.

I've tried removing the +1, and changing it to +0 and -1, each giving different results, but still not the expected, so perhaps I just butchered this formula to try to get the lowest 2.

Hopefully an easy fix/oversight on my part, but if not I'll of course keep working on a mock up file that fits inside the copy/paste limits and doesn't #REF out all the cells.

Also I'll keep scouring for an example of the other issue where the unaltered formula is considering the blanks as the highest/largest.

Thanks again!
 

Attachments

  • SAMPLE.png
    SAMPLE.png
    24.7 KB · Views: 4
Upvote 0

Forum statistics

Threads
1,215,833
Messages
6,127,156
Members
449,366
Latest member
reidel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top