Limit the number of results when using Excel array functions - excel

I am trying to use the SortBy function to return the top 3 results from a table that I have. If I put the function somewhere else in the sheet, I can just reference the first three cells, and that works fine. But I would like to do this with just one function.
I have created a lambda with the Filter function to help, but if I get multiple items with the same sum, it will give me more than three results and cause a spill. Is there another way I can approach this to give me the top 3 know that I might not see some ties if they are there.
=LAMBDA(rDesc,rSum,top,default,
LET(
sDesc,SORTBY(UNIQUE(rDesc),SUMIFS(rSum,rDesc,UNIQUE(rDesc)),-1),
sSum,SORT(SUMIFS(rSum,rDesc,UNIQUE(rDesc)),,-1),
range,AppenColRange(sDesc,sSum,"-"),
rowindex,SEQUENCE(top)
result,FILTER(range,sSum>=LARGE(sSum,top),"-"),
topResults,IF(
rowindex<=
)
IFERROR(result,default)))
The data would look something like this.
Food Sold
Apple 20
Banana 10
Apple 30
Apple 3
Carrot 5
Milk 10
Peas 10
Kale 1
Then I'm looking for a result like this
Apple 53
Banana 10
Milk 10
Peas also had ten, but got cut because I only want 3 items returned.

We can use TAKE and HSTACK:
=LAMBDA(rdesc,rsum,top,default,
LET(unq,UNIQUE(rdesc),
IFERROR(TAKE(SORT(HSTACK(unq,SUMIFS(rsum,rdesc,unq)),2,-1),top),default)))
And since at the time of writing this HSTACK and TAKE are not available to all Office 365 users, we can use CHOOSE and INDEX respectively:
=LAMBDA(rdesc,rsum,top,default,
LET(unq,UNIQUE(rdesc),
IFERROR(INDEX(SORT(CHOOSE({1,2},unq,SUMIFS(rsum,rdesc,unq)),2,-1),SEQUENCE(top),{1,2}),default)))

Related

Determining a value depending on where you are on a list in Excel

I am tracking the progress of students reading books in class. For the number of books they read they get a reward. It is not 1 book = prize, 2 books = prize. Instead there are dead spaces along the way, for example there is no reward for books 3 and 4 but there is for 5 books. I want to be able to input the number of books each student read and have it update as to what the next reward will be. For example:
List one
Name Books Next Tier Prize
Sally 4 5 Candy Store
Luke 1 2 Extra coloring time
Jane 8 10 10 Extra minutes on the playground
And so on
The table for rewards would be
Books Prize
1 Ribbon
2 Extra coloring time
5 Candy Store
7 Prize bucket
10 10 Extra minutes on the playground
And so on
This is just an abbreviated list and I have used if then statements previously. However the previous list that had 18 values was cumbersome as it was, the new list has 35 values.
I have used if/then statements in the past in combination with vlookup, but with the increased number of values, it just seemed daunting. I could still use an if statement but was hoping there would be an easier way.
Put this in C2 and copy over and down:
=INDEX(F$2:F$6,IFERROR(MATCH($B2,$F$2:$F$6),0)+1)

count how often a piece of information appears and calculate that average

I do not want to know the traditional frequency or the traditional averages; so I'll give an example below:
I have this data:
1
3
5
5
2
3
5
5
1
3
The analysis that I would like to obtain is the following:
for example number 1 appears once every eight rows, number 3 appears once every four rows, number 5 appears twice every two rows....
I did it by hand, but now I have more than 21000 rows of data and I'm stuck.
I searched but I can not find a function that does it; But before I started developing my own, I decided to ask for a guide on how to achieve it.
I believe that I was able to achieve the desired result:
The formula is:
Or, if you want to copy/paste:
=IF(CONCATENATE("1-",MATCH(D1,INDIRECT(ADDRESS(MATCH(D1,A1:A17,0)+1,1,4)&":A17"),0))="1-1",CONCATENATE("2-",MATCH(D1,INDIRECT(ADDRESS(MATCH(D1,A1:A17,0)+2,1,4)&":A17"),0)-1),CONCATENATE("1-",MATCH(D1,INDIRECT(ADDRESS(MATCH(D1,A1:A17,0)+1,1,4)&":A17"),0)))
Note that the IF function solves the duplicates (like the number 5). In case you have triplicates you will have to add another instance of IF and adjust the formula accordingly.
Hope that helps!
Well this doesn't exactly reproduce your results, but you could start by looking at the max and min separation of the numbers:
=IF(COUNTIF(A$1:A$10,C2)<=1,"",MIN(IF((ROW(A$1:INDEX(A$1:A$10,COUNTIF(A$1:A$10,C2)+1))>1)
*(ROW(A$1:INDEX(A$1:A$10,COUNTIF(A$1:A$10,C2)+1))<=COUNTIF(A$1:A$10,C2)),
FREQUENCY(IF(A$1:A$10<>C2,ROW(A$1:A$10)),IF(A$1:A$10=C2,ROW(A$1:A$10)))))+1)
=IF(COUNTIF(A$1:A$10,C2)<=1,"",MAX(IF((ROW(A$1:INDEX(A$1:A$10,COUNTIF(A$1:A$10,C2)+1))>1)
*(ROW(A$1:INDEX(A$1:A$10,COUNTIF(A$1:A$10,C2)+1))<=COUNTIF(A$1:A$10,C2)),
FREQUENCY(IF(A$1:A$10<>C2,ROW(A$1:A$10)),IF(A$1:A$10=C2,ROW(A$1:A$10)))))+1)
This gives the min or max number of rows between each occurrence of the particular number.
Must be entered as an array formula using CtrlShiftEnter
You could add other statistics (like mean, standard deviation) the same way although the average could be calculated just by (lastrow-firstrow)/(count-1) e.g. for 5 it would be (8-3)/(4-1)=5/3.

Subtract two range values for matching ones excel

I have come across of the problem that, I don't know how to solve in efficient way. So I have two tables like these:
Table 1
Company Score
Apple 10
Banana 5
Kiwi 4
Pineapple 2
Table 2
Company Score
Apple 4
Pineapple 3
Kiwi 2
Strawberry 1
Then in my result table in which I have company names listed in one column, like below. Right next to this I would like to show the differences which should be the first table values - the second table values where company names match(the list can be in different order that is why, I need to check it before subtracting).
Table 3
Company Result
Kiwi 2
Apple 6
Banana -5
Pineapple -1
I can do this with nested IF function to check every cell one by one, but I assume there might be more efficient way. I wonder if you have one.
Thanks to #ScottCraner's comment, I solved it with two sumifs subtracted from one another with this formula. I entered this in Column D66 and dragged down
=SUMIF($C$36:$C$45;$C$66:$C$75;D$36:D$45)-SUMIF($C$51:$C$60;$C$66:$C$75;D$51:D$60)
1st Sum if: Where C36-C45 is my first list of companies, C66-C75 is the list of companies that I have in the results table, D36 -D45 is the score list from the first table.
2nd Sum if: Where C51-C60 is my second list of companies, C66-C75 is the list of companies that I have in the results table, D51 -D60 is the score list from the second table.

Equal distribution formula based on preset values. Excel

e.g. There are 300 apples. There are 100 people. Each person has a preset value (represented as a number 1 to 5). 1=1 apple, if their value is 5 they get 5 apples etc. But there are 300 apples available so each person is going to get more then the value says they "deserve". Or one day there are only 200 apples and every one gets less then what the value states says they "deserve". Is this possible in excel?
NAME VALUE
john 5
james 5
sam 4
matt 5
mike 3
steve 2
etc...
This absolutely sounds like a perfect problem for Solver to handle. As you know, this in included within Excel's addins. It can deal with all the variables you mentioned.

sports scoring in multiple phases in excel

I have a question about excel.
There is a sports tournament with multiple phases. And the results page looks something like this:
rank phase1 phase2
1 TOM ALBERT
2 MATT TOM
3 ROBIN MATT
The first place gets 5 points, second 3 and third 1 points.
So the summary I would like to get is like this
rank name phase1 phase2 total
1 TOM 5 3 8
2 ALBERT 0 5 5
3 MATT 3 1 4
4 ROBIN 1 0 1
I cant figure out how to generate it simply, so if I copy-paste the results from phase3 to somewhere in my excel sheet, the summary page would also update.
If you can support me with advice or just a working template, I would be thankful!
Thank you for your time!
I've done you a basic example below...just list all 50 players in the summary page and copy the formula down as it is - as per the question it only records a value for the top 3 places, hope this helps if not please let me know :)
Example file here
Assuming the structure you defined and that when you paste phase 3 it will have its header ("phase3") on the same row as the other results, you could have the following formula on your Table 2:
{=SUM(IF(IFERROR(MATCH($B2,INDIRECT("Sheet1!"&CHAR(64+MATCH(C$1,Sheet1!$1:$1,0))&":"&CHAR(64+MATCH(C$1,Sheet1!$1:$1,0))),0)-1,0)={1,2,3},{5,3,1},0))}
Note: entered as Array formula: CTRL + SHIFT + ENTER
Formula uses the Table 2 headers ("phase1","phase2", etc.) and the players names to find the results for each phase. It then uses the predefined arrays that determine points given per position. The final sum is to bring the result that are in array (other values in array are 0) into single value.
Total would sum results per player.
Under the rank cells you would place the following formula:
=RANK(F2,$F:$F)
Which would provide you the rank of each player (Column F containing the totals).
Note that this would not sort your rank automatically but you could easily do this with Sorting or Autofilter. Hope this helps. Cheers.

Resources