Sumproduct - counting equal pairs of numbers (and filtering them) - excel

In columns D&E I have a list of scores for a game, where D is points for and E is points against, like so
D E
1 3
4 2
3 3
3 1
I'm trying to create a formula that displays a win / draw / loss record based on whether column D is larger, equal to or smaller than column E. In this example it would display 2 / 1 / 1.
So far I have this
=(SUMPRODUCT(--(D12:D200>E12:E200)))&" / "&SUMPRODUCT(--(D12:D200=E12:E200))&" / "&(SUMPRODUCT(--(D12:D200<E12:E200)))
But there are two issues. One is that all the blank rows are being counted as equals, so the result is coming out as 2 / 186 / 1.
The second is that in another column I have a list of days of the week, and I would like to be able to filter out rows by day and have the results reflect this. I have different formulas using SUBTOTAL instead of SUM to count overall number of points, which works fine. But I don't know what the equivalent change I need to make would be for my formula. Any help would be appreciated.

As for your first issue, your formula indeed takes blanks into account and treats them as equals. You can adjust your middle SUMPRODUCT formula to omit the blanks, just like that:
=SUMPRODUCT(ISNUMBER(D12:D200)*(--(D12:D200=E12:E200)))
The second question is regarding filtering out rows by the day of the week. Here's the view before "Day" filter is applied - as you can see we have 5 wins (blue), 4 draws (orange) and 3 losses (green).
You need to use the following formula to make SUMPRODUCT dynamic (i.e. it will react to filtering out rows):
=SUMPRODUCT(SUBTOTAL(3,OFFSET(F12:F200,ROW(F12:F200)-ROW(F12),,1)),--(D12:D200>E12:E200))&" / "&SUMPRODUCT(SUBTOTAL(3,OFFSET(F12:F200,ROW(F12:F200)-ROW(F12),,1)),ISNUMBER(D12:D200)*(--(D12:D200=E12:E200)))&" / "&SUMPRODUCT(SUBTOTAL(3,OFFSET(F12:F200,ROW(F12:F200)-ROW(F12),,1)),--(D12:D200<E12:E200))
Here's the result just for Monday:

Related

Excel formula - Get the sum of each variable every 6 days

I have the following dataset where I want to get the sum of each variable every 6 days. I can get the total sum of every 6 days using
=SUM(OFFSET($A$2,,(COLUMNS($A$5:A5)-1)*6,,6))
And I can get the total sum of each variable using
=SUMIF(A1:S1,A1,A2:S2)
But I cant get the total sum of each variable within the block of 6 days. It won't increment when I drag the formula.
So the results should be
First batch Second batch Third batch
A B C A B C A B C
2 2 2 4 4 4 6 6 6
You can use SUMPRODUCT:
=SUMPRODUCT((1:1=A6)*2:2*(COLUMN(1:1)>(INT((COLUMN()-1)/3)*6))*(COLUMN(1:1)<=(INT((COLUMN()-1)/3+1)*6)))
Edit:
To shift the column by five position, you will need to change the following parameters in the formula:
Full row range change to exact range, i.e. 1:1 to e.g. $F$1:$W$1
Change COLUMN()-1 to COLUMN()-3
If you also want to change the number of columns to be summed, additionally replace the factor of 6 with a 7-1 for seven columns or 36-30 for thirty-six columns.
So formulas looks like:
batch of 6 cols
=SUMPRODUCT(($F$1:$W$1=F6)*$F$2:$W$2*(COLUMN($F$1:$W$1)>=((INT((COLUMN()-3)/3))*6))*(COLUMN($F$1:$W$1)<((INT((COLUMN()-3)/3+1))*6)))
batch of 7 cols
=SUMPRODUCT(($F$1:$Z$1=F6)*$F$2:$Z$2*(COLUMN($F$1:$Z$1)>=((INT((COLUMN()-3)/3))*7-1))*(COLUMN($F$1:$Z$1)<((INT((COLUMN()-3)/3+1))*7-1)))
batch of 36 cols
=SUMPRODUCT(($F$1:$WW$1=F6)*$F$2:$WW$2*(COLUMN($F$1:$WW$1)>=((INT((COLUMN()-3)/3))*36-30))*(COLUMN($F$1:$WW$1)<((INT((COLUMN()-3)/3+1))*36-30)))
Instead of creating a really, really, really complex formula that can be dragged right, I suggest you add a row to the data at the top that identifies the batch number. Then you can use that batch number as an additional parameter in the Sumifs(). you can hide the rows with the batch numbers if they upset your spreadsheet design.
=SUMIFS(3:3,1:1,A16,2:2,A17)
This is far easier than creating a formula that dynamically adjusts references in tiered steps of three and six.

Excel: Cumulative sum of min values of 2 columns without needing to create an extra column

I have two columns that I need to find the minimum value of, and then create a cumulative sum of them. I can do this by creating an extra column to hold the min value and then cumulative sum them, e.g.:
Col1 Col2 min(Col1, Col2) Cumulative Sum of Mins
1 3 1 1
4 2 2 3
3 5 3 6
Is there a way of doing this without creating the extra column?
I've tried sum(if(A$2:A2 < B$2:B2, A$2:A2, B$2:B2)) which I found (and modified) from another similar-but-not-similar-enough question, but this appears to just find the lowest value in the entire range and output that into a single cell; copying and pasting the formula into the other fields results in:
A value used in the formula is of the wrong data type
put this in C2 and copy down:
=IFERROR(--C1,0)+MIN(A2:B2)
Another approach is with SUMPRODUCT
=SUMPRODUCT(($A$2:A2<$B$2:B2)*($A$2:A2)+($A$2:A2>$B$2:B2)*($B$2:B2))
But this is an array type formula and as such every line this is copied down will increase the number of calculations exponentially. If too many lines are used the user will see a time delay in the calculations.

All combinations of 4 out of 7 columns with totals using excel

I have 7 columns to choose from and I need to pick 4 of those columns and generate a total for each row. I also need every combination of 4, which means I'll have 35 new columns with the totals for each of those combinations showing in each row. I need the code for this and if it can be done only using Excel. Here is an image of the columns and the grayed ones are the 7 columns I'm talking about. My knowledge of Excel is very limited. There are over 1,500 rows if that matters.
multi step approach that is going to use some helper rows. there may be a more elegant formula that will do this, and much slicker options in VBA, but this is a formula only approach.
Step 1 - Generate List of Column Combination
To generate the list 4 helper rows will need to be insert at the top of your data. either above or below you header row. These 4 rows will represent the column number you are going to pick. To keep the math simpler for me I just assumed the 1 for the first column and 7 for the last column. those numbers will get converted to later to account for column in between in your spreadsheet. For the sake of this example The first combination sum will occur in column AO and the first helper row will be row 1. The first combination will be hard coded and it will seed the pattern for the remainder of column combinations. Enter the following values in the corresponding cells:
AO1 = 1
AO2 = 2
AO3 = 3
AO4 = 4
In the adjacent column a formula will be placed and copied to the right. It will automatically augment the bottom value by 1 until it hits its maximum value at which point the value in the row above will increase by 1 and the the value of the current will be 1 more than the cell above. This will produce a pattern that covers all 35 combinations by the time column BW is reached. Place the formulas below in the appropriate cell and copy to the right:
AP1
=IF(AO2=5,AO1+1,AO1)
AP2
=IF(AO2=5,AP1+1,IF(AO3=6,AO2+1,AO2))
AP3
=IF(AO3=6,AP2+1,IF(AO4=7,AO3+1,AO3))
AP4
=IF(AO4=7,AP3+1,AO4+1)
Step2 - Sum The Appropriate Columns
I was hoping to use a some sort of array type operation to read through the column reference numbers above, but I could not get my head around it. Since it was just 4 entries to worry about I simply added each reference manually in a SUM function. Now the important thing to note is that we will be using the INDEX function over the 13 columns that cover the range of your columns so to convert the index number we figured out above, to something that will work to grab every second row, the number that was calculated will be multiplied by 2 and then 1 will be subtracted. That means 1,2,3,4 for the first column combination becomes 1,3,5,7. You can see this in the following formula. Place the following formula in the appropriate cell and copy down and to the right as needed.
AO5
=INDEX($AB5:$AN5,AO$1*2-1)+INDEX($AB5:$AN5,AO$2*2-1)+INDEX($AB5:$AN5,AO$3*2-1)+INDEX($AB5:$AN5,AO$4*2-1)
pay careful attention to the $ which will lock row or column reference and prevent them from changing as the formula is copied.
Now you may need to adjust the cell references to match your sheet.

Excel conditional SUMPRODUCT / SUMIFS / Array Formula for optional dimension

I have a sheet of data with multiple dimensions like this:
A B C D E
1 COUNTRY FLAVOUR SIZE DATE SALES ($)
2 Japan Strawberry 100ml 10/12/14 100
3 Japan Banana 100ml 10/03/15 100
4 China Orange 200ml 14/04/15 30
5 France Strawberry 200ml 11/04/15 400
6 UK 200ml 23/04/15 250
7 ....
I want to aggregate this data over a date range, where the summary sheet has each dimension (country & flavour), and if I do not specify a dimension it sums all rows for that dimension.
A B C
1 COUNTRY FLAVOUR SALES TOTAL
2 Japan Strawberry 100
3 Japan 200
4 Strawberry 500
I can do this if all the dimensions are present (i.e. row 2 above) using a SUMPRODUCT or SUMIFS:
=SUMPRODUCT((data!A$2:A$100=A1)*(data!B$2:B$100=B1)*(data!D$2:D$100>[start_date]*(data!D$2:D$100<[end_date])*(data!E$2:E$100))
However I have not been able to figure out how to include all rows for a dimension if that input cell is empty (e.g. row 3 above). I tried:
Adding an IF statement or OR statement within the criteria (e.g. OR(data!A$2:A$100=A1,isblank(A1))).
Using a + in a SUMPRODUCT as an OR statement, (per this answer https://stackoverflow.com/a/27536131/1450420)
One solution is to have different branches of the formula depending on which summary dimensions are present, but that would quickly get out of control if I extend this same behaviour to further dimensions like Size.
Any help appreciated!
(I'm running Excel Mac 2011).
EDIT
Per #BrakNicku's comment one of the formulas I tried was =SUMPRODUCT(((data!A$2:A$100=A2)+ISBLANK(A2))*((data!B$2:B$100=B2)+ISBLANK(B2))*(data!E$2:E$100))
The reason this doesn't work is that sometimes my data has blank attributes (edited above). For some reason this formula double-counts rows where the attribute present matches (e.g. data!A6) but the other attribute is missing (e.g. data!B6).
EDIT 2
I can see why this double-counting is happening, because the + is summing the match because data!A$2:A$100=A2 (they match because they are both blank) and the match because ISBLANK(A2) (it is indeed blank). The question would remain how to achieve this without double counting. If needed a workaround could be to fill all blank cells on my data with some placeholder value.
The reason for double-counting values is here:
((data!A$2:A$100=A2)+ISBLANK(A2))
If a cell in A column is blank, both parts of this sum are equal 1. To get rid of this problem you can change it to:
(((data!A$2:A$100=A2)+ISBLANK(A2))>0)
Try this (I only included the first two, I left the dates out):
=SUMPRODUCT((((Data!$A$2:$A$5=A2)+(A2=""))>0)*(((Data!$B$2:$B$5=B2)+(B2=""))>0)*(Data!$E$2:$E$5))

Ranking in Excel with multiple criteria

For example, I need to create a merit list of few student based on total marks (column C), then higher marks in math (column B) -
A B C D
-------------------------
Student1 80 220 1
Student2 88 180 3
Student3 90 180 2
Expected merit position is given in column D.
I can use RANK function but I can only do that for one column (total number). If total number of multiple student is equal, I could not find any solution of this.
You can try this one in D1
=COUNTIF($C$1:$C$99,">"&C1)+1+SUMPRODUCT(--($C$1:$C$99=C1),--($B$1:$B$99>B1))
and then copy/fill down.
let me know if this helps.
Explanation
Your first criteria sits in column C, and the second criteria sits in Column B.
Basically, first it is counting the number of entries ($C$1:$C$99) that are bigger than the entry itself ($C1). For the first one in the ranking, you will get zero, therefore you need to add 1 to each result (+1).
Until here, you will get duplicate rankings if you have the same value twice. Therefore you need to add another argument to do some extra calculations based on the second criteria:
To resolve the tie situation, you need to sumproduct two array formulas and add the result to the previous argument, the goal is to find the number of entries that are equal to this entry with $C$1:$C$99=C1 and have a bigger value in the second criteria column $B$1:$B$99>B1:
you add -- to convert TRUE and FALSE to 0s and 1s so that you can multiply them:
SUMPRODUCT(--($C$1:$C$99=C1),--($B$1:$B$99>B1))
the first array is to see how many ties you have in the first criteria. And the second array is to find the number of bigger values than the entry itself.
Note you can add as many entries as you like to your columns, but remember to update the ranges in the formula, currently it is set to 99, you can extend it to as many rows as you want.
Sometimes a helper column will provide a quick and calculation-efficient solution. Adding the math marks to the total marks as a decimal should produce a number that will rank according to your criteria. In an unused column to the right, use this formula in row 2,
=C2+B2/1000
Fill down as necessary. You can now use a conventional RANK function on this helper column like =RANK(D2, D$2:D$9) for your ranking ordinals.
Very simple (or, at least, much more simpler that the one provided by the best answer) 'math' solution: do a linear combination with weights.
Do something like
weighted_marks = 10*colC + colB
then sort weighted marks using simple rank function.
It does solve your problem, bulding the ranking you need.
If you don't like to limit the number of rows or the numbers used in the criteria, Jeeped's approach can be extended. You can use the following formulas in cells D2 to L2, assuming that there are three criteria, the first one in column A, the second one in column B, and the third one in column C:
=RANK($A2,$A:$A,1)
=RANK($B2,$B:$B,1)
=D2*2^27+E2
=RANK(F2,F:F,1)
=RANK($C2,$C:$C,1)
=G2*2^27+H2
=RANK(I2,I:I,1)
=J2*2^27-ROW()
=RANK(K2,K:K,0)
The formulas have to be copied down. The result is in column L. Ties are broken using the row number.
If you like to add a fourth criterion, you can do the following after having the formulas above in place:
Add the new criterion between columns C and D.
Insert three new columns between columns I and J.
Copy columns G:I to the new columns J:L.
Copy column G to column M, overwriting its content.
Change the formula in column L to point to the new criterion.
The factor 2^27 used in the formulas balances the precision of 53 bits available in double-precision numbers. This is enough to cover the row limit of current versions of Excel.

Resources