Excel match, sum and subtract from a single total - excel-formula

In the table below, IF C12:C21 match either D3 or e4, I want to sum the SWAG column per row and then subtract either D5 or E5 (based on the match) from the additive sum per column until I know that I've reached the value in either D5 or E5 (based on the team match)

I'm not sure if I understand correctly what you mean, since you did not give an example of how the result you're looking for would look in given data. But this might be what you mean or at least usable to get to what you want:
=LET(swag,D12:D21,
s,SEQUENCE(ROWS(swag)),
r,MIN(
IFERROR(XMATCH(D5,SCAN(0,swag,LAMBDA(a,b,IF(OFFSET(b,,-1)=D4,a+b,a))),1),9^9),
IFERROR(XMATCH(E5,SCAN(0,swag,LAMBDA(a,b,IF(OFFSET(b,,-1)=E4,a+b,a))),1),9^9)),
IF(s=r,OFFSET(swag,,-1),""))
This uses the formula SCAN to conditionally sum the SWAG scores matching the correct team. Column F & G in the screenshot below demonstrate what SCAN does.
The MIN part of the formula compares the first matches equal to or greater than the values in row 4 to the scan value of it's team. If it results in no match it will give a high number as a result 9^9, so it will not be the MIN value.
The min value is the nth row in the SCAN ranges or if both result in 9^9 it'll result in that high value.
Then, if a sequence to the number of rows of the swag range equals the min value it'll show the team name of that row. If both didn't match they will not have a match to the sequence and all rows remain blank.

Related

Find last occurrence of specific value in a row

I have a table that keeps scores from fantasy football league. The lowest scoring week is not included in the total.
I'm able to find the lowest score using the =min function [=min(B2:R2)]
I can find the first instance using the =match function. [=MATCH(S2,B2:R2,0)]
However I would like a formula that returns the latest occurrence of the lowest scoring week.
For example Portia lowest score is 8 the first occurrence is week 4 but I would like it to return the latest occurrence which is week 11
You can also do this using LOOKUP function and avoiding array formulas, e.g. assuming S2 has the minimum value you can get the last week with that value with this formula in T2 copied down
=LOOKUP(2,1/(B2:R2=S2),B$1:R$1)
This works because (B2:R2=S2) returns an array of TRUE/FALSE values, and then dividing 1 by that array gives you an array of 1s or #DIV/0! errors. When you use 2 as the lookup value on that array it won't find any 2s so will therefore match with the last number (the last 1) in the array. That corresponds to the last week with your smallest value so using the top row as the "return vector" means that the formula returns the corresponding week number as required
You will need to use an Array Formula to achieve this. The following will return the "highest" lowest value if you are using columns B:R as the data columns and column T as the Low Week column. You need to enter this as an array (CSE) formula by copying and pasting it into cell T2 and pressing Ctrl+Shift+Enter.
=IFERROR(INDEX($A$1:$R$1,1,SMALL(IF(B2:R2=MIN(B2:R2),COLUMN(B2:R2),FALSE),COUNTIF(B2:R2,MIN(B2:R2)))),"")
Explanation
First, the COUNTIF(B2:R2,MIN(B2:R2)) will count the number of times the minimum value occurs. This is needed because a simpleMIN(B2:R2) will only grab the first minimum value, not the nth minimum value.
Next, the IF(B2:R2=MIN(B2:R2),COLUMN(B2:R2),FALSE) is the actual array formula part of the equation (the reason why you need to use Ctrl+Shift+Enter). It will only return the references for when you have a minimum and returning FALSE for all other entries greater than the minimum. Now, you have an array of only the column numbers the smallest values in the dataset.
Then, the SMALL(IF(B2:R2=MIN(B2:R2),COLUMN(B2:R2),FALSE),COUNTIF(B2:R2,MIN(B2:R2))) will take the column numbers of the smallest values and find the latest occurrence of this, which comes from the COUNTIF(B2:R2,MIN(B2:R2)) code.
Now that the last smallest occurrences' column number is know, you can use the INDEX function to find the value in the first row INDEX($A$1:$R$1,1.
Finally, the IFERROR will display the Low Week row if the SMALL function finds a match, otherwise it will display a blank cell. This could be used to copy this array formula further down the page for rows that you don't yet have users for.
Result

If, Then, Duplicate Excel Formula

Column "A" is a numbering column for each Row, some numbers are the same, ie..
A1 is 1
A2 is 3
A3 is 1
A4 is 3
I need a formula that will show how many cells with content are in this column without counting duplicates, as above would be 2. I was figuring an "If-Then" formula but am unable to get it straight. Any help out there? Thank you in advance!
If you're using Excel 2013, I want to say that there's a count distinct function. Nonetheless, you can do it like this:
=SUM(IF(FREQUENCY(A1:A4,A1:A4)>0,1))
EDIT: Adding an explanation. The FREQUENCY function gets the frequency of the unique values within the array A1:A4 (first parameter), binning it using the values within A1:A4 (second parameter). The IF checks to see if that returns anything, i.e. if the frequency is greater than 0, in which case it returns 1 for each unique value it finds. Then the SUM adds the number of 1s returned by the IF statement, in turn giving you the number of unique values within the array A1:A4.

Formula with IF statement and condition

I am trying to use an excel formula to determine the proportion (pDistance) of the total Distance for each Position by Site. For example if the (total) Distance was 50 and the Position was 10 the proportion of the total distance (pDistance) would be 0.2, the last pDistance of any Site should always be 1. The formula I used (=IF(B3<B2, 1, (B2/C2))) mostly works, except that I have some values of Position that are -10.
In these cases every Position in that Site should have a value of 10 added to it before calculating the pDistance. Can this be done with one formula, that is calculate the pDistance and add 10 when necessary? Or would it be best to make a dummy column first? If creating a dummy column is the way to go, how does one use formula to do something until a condition is met (e.g. add 10 the Position until a 0 , or new Site, is reached)?
From the sample data, I understand that for any Site which has a value of -10 somewhere in column B, then the calculation of (B / C) needs to be ((B + 10) / C). (Although cell D11 doesn't follow this, I'm assuming it's just a typo :) )
The formula below would work in this case:
D2 = (SUMPRODUCT(($A$2:$A$22=$A2)*($B$2:$B$22=-10))*10 + B2) / C2
This formula works as follows:
SUMPRODUCT(($A$2:$A$22=$A2)*($B$2:$B$22=-10))
--> This will return the total number of rows which contain the value of A2 in column A and a -10 in column B :
($A$2:$A$22=$A2) looks at all values in the cells between A2 & A22, and finds which ones match A2. That is, it finds which rows that have the same Site number as the row where the formula is entered
($B$2:$B$22=-10) looks at all values in the cells between B2 & B22, and finds which ones equal -10. That is, it finds all rows that have a Distance value of -10.
The SUMPRODUCT finds the overlap of these. So, it counts how many rows have both the right Site number and a distance value of -10.
This value is than multiplied by 10. If there were no -10 values paired with the site, then it is 0 * 10 = 0. If there was one pair, it will be 1 * 10 = 10.
That result is then added to B2, and then divided by C2.
Note: this formula assumes that there will only be a maximum of one -10 value for a particular site. If there is more than one, then the SUMPRODUCT will return the total number of matches, and the calculation will be wrong (but the formula can be updated to correct this).
Also note that the formula refers to the ranges $A$2:$A$22 and $B$2:$B$22. These ranges must match the first and last numerical entry in your data (and not include any text - this will break the SUMPRODUCT formula). The simplest way to handle this (if you don't want to have to update the formula when adding values at the end of data set) is to set up a defined range name and refer to that in the formula.
To apply the formula, just paste it into D2, and copy/drag the formula down into the cells below.
As a conclusion, I can recommend that you create a "dummy" / "helper" column to store the (SUMPRODUCT * 10) + B2 results, and use this as the input for the division calculation. This will help to visualize and check the data that is being used for the calculation. It can be hidden until you want to check the values or change the formula in case the requirements change in the future.

extract the row number of a function result

I have a simple table with 5 names and 5 grades if you will.
In another column I order the grades using the LARGE function.
Now is there a way to know the row of each of the "ordered" grades to obtain something like that?
White 23 31 5
Red 15 23 1
Green 23 23 3
Blue 18 18 4
Grey 31 15 2
The column I can't calculate is the last one!
You should use the rank() function if you want to rank these grades. Not large().
=RANK(D2,$D$2:$D$6,1)
You can try this
=MATCH(LARGE(B1:B5,1),B1:B5,0)
The result is a number of row...
In Cell D1 Put =INDEX($A$1:$A$5,MATCH(C1,$B$1:$B$5,0))
Then in Cell D2 put =IF(D1<>INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
This will also work when duplicate Grades are present
But I Strongly Suggest using Sort as Follows:
*****Also: ***** Here is the explanation on the above Formulas.
To get the Row that contains the Number we are looking for (the number in Column C) you need yo use the Match() Function. We enter =MATCH(C1,B1:B5,0) in D1:
What this is doing: IS looking to the value in C1, this is 31
It is looking in Range("B1:B5"), And 0 is for an Exact match.
So when look for a match to C1 or 31 we get 5. This tells us that 31 is in Row 5
Now, to get the Value of Column A on Row 5 we use INDEX() Function as Follows:
We add to the =MATCH(C1,B1:B5,0) in D1 as =INDEX(A1:A5,MATCH(C1,B1:B5,0))
This will look in Range("A1:A5") for Row 5 (This is because =MATCH(C1,B1:B5,0) = 5)
And the result will be Grey
Now if we drag this formula down we will find the first problem:
Here are our 2 Issues:
1) We get an `N/A` error in the last row.
2) Although `Green` is only in `Range("A1:A5")` one time we see it twice
even though it would seem that `White` should be twice.
These are cause because:
1) We need to add `$` to the range that will remain the same so when we drag down
the formula is won't shift the range. As is the formula in `D5` is
`=INDEX(A5:A9,MATCH(C5,B5:B9,0))` and we receive the error *because*
`Range("A5:A9")` does not contain `15`, but the issue is we meant
to look in `Range("A1:A5")`
So we change the Formula as so: =INDEX($A$1:$A$5,MATCH(C1,$B$1:$B$5,0))
Take note that we do not use the $ on C1 in the formula cause we WANT this value to change as we move down.
But we still have the issue of double values when they shouldn't be there.
Because D1 is the first cell we won't change the formula in it. As anything that is equal to the greatest value is simply tied with it and I don't see any reason why the order of the tie would matter.
Instead we will start in D2 and enter =IF(D1<>INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
What this is doing is checking if the value of =INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0))
is not equal to the value in the row above. (being a sorted list means all double values would be on top of each other) and If it is NOT the same then use the value, but if it is the same we need to do a little more work.
If the value is not the same we use the Formula INDEX($A$1:$A$5,MATCH(C2,$B$1:$B$5,0)+MATCH(C2,INDIRECT("$B$"&MATCH(C2,$B$1:$B$5,0)+1&":$B$5"),0)))
Now to explain it I will use our example of double values. In D3 we find the formula: =IF(D2<>INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)),INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)+MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0)))
And because we know that INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)) will be equal to the above cell (White), and we have gone over how the if true works, I will focus on the if false value of: INDEX($A$1:$A$5,MATCH(C3,$B$1:$B$5,0)+MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0))
We know MATCH(C3,$B$1:$B$5,0) is the Row that contains the first instance of C3 in this case 23 and the row is Row 1 so we need to look for 23 in the row Under Row 1. So we use MATCH(C3,INDIRECT("$B$"&MATCH(C3,$B$1:$B$5,0)+1&":$B$5"),0) which is equal to MATCH("23", B2:B4,0) because we are adding a 1 to the row that has the 1st match for 23 or C3.
that will now return us the Value of 2 as, the value 23 is in the second row of Range("A2:A5"), Red is in Row 1 and Blue in Row 3 of that range as shown:
but we don't want Row 2 we know that 23 relates to Green and that Green is in Row 3 So we add the row the we last found the value 23 (1 or MATCH(C3,$B$1:$B$5,0))to the row we currently found it (2) and get Row 3.
Here is a formula approach based on the methodology outlined in this link. The final layout of this approach is shown below.
I have assumed that there is 1 header row and I use 2 helper columns (D & E). While additional rows can be added to the header, the table must begin in column A in order for the formulas in column E to work correctly.
Although the helper columns could be eliminated by consolidating their formulas into the formulas in column F, I do not recommend it: the resulting formulas would be a pain to maintain.
Formulas Needed
Cell C2: =LARGE(B:B,ROW(A2)-ROW($A$1)) [Copy down to bottom of data]
Cell D2: =MATCH(C2,B:B,0) [Copy down to bottom of data]
Cell E2: =D2
Cell E3: =IF(D3<>D2,D3,E2+MATCH(C3,INDIRECT("B"&(E2+1)&":B"&COUNTA(A:A)),0))
[Copy down to bottom of data]
Cell F2: =OFFSET($A$2,E2-ROW($A$2),0) [Copy down to bottom of data]
Explanation of Answer
There are four steps to getting the answer:
Sort the grades from highest to lowest (as you showed in your example data)
Create a partial ordering of the row numbers for the sorted grades
Get the row numbers for duplicate grades
Use that ordering to show the name for each sorted grade
Sort the grades from highest to lowest
As you have done, my sort uses the LARGE function, which returns the nth largest value in a range or array. As shown, the LARGE function in cell C2 takes the grades in column B. The "n" for LARGE is calculated as the current row number minus the number of rows in the header, in this case the 1 row for cell A1. When the formula is copied down, "n" progresses from 1 to 2 to 3, etc.
Partially order the grade row numbers
The next step is to determine the row numbers for the unsorted grades that correspond to the sorted grades.
To do that, I use the MATCH function to find where each of the sorted grades lies in the list of unsorted grades in column B. MATCH takes three arguments--the value to be matched, the range in which to make the match, and optionally, the type of match, with a value of 0 or FALSE for an exact match--and returns an index number which represents where in the lookup range the match is found (1 for the first row in the match range, 2 for the second row, etc.).
In the formula for cell D2 shown above, the MATCH function on the grade 31 returns 6 since 31 is in the sixth row of column B.
The result for cell D4 shows why it is only possible to get a partial ordering with this formula. While we are trying to lookup the row for the second instance of a grade of 23, the formula returns a value of 2, which corresponds to the row for the first instance of 23. That's because MATCH will always return the first match for 23 it finds, which is on row 2!
Get correct row numbers for duplicate grades
The next step is to get the correct row references for the duplicated row numbers in column D. The formulas that accomplish this are shown for the first three cells in column E of the table.
There are three cases that have to be dealt with in column E:
For the first (and possibly only) instance of the highest grade, it is possible to just use the row number calculated in cell D2.
The second case deals with the first instances of the row references of the remaining grades. For these the rows numbers calculated in column D can again be used (via the TRUE branch of the IF statement in the column E formulas). For example, in cell E2 -- which corresponds to the first instance of grade 23 -- the row number in cell D3 can be used.
The final case is the rows for duplicate grades. Here, the MATCH for each duplicate in column B is recalculated using a sliding range that excludes the previous matches for that grade. For example, for the duplicated grade of 23 in column C, the match is on the range B3:B6, rather than the range of B2:B6 used in the column D calculation.
Diplay the names in sorted order
This final step is straight forward: Get the name corresponding to the sorted grade. Here the OFFSET function is used; its arguments are a cell reference and the number of rows and columns from that reference that the desired value is to be found.

Count rows until the sum value of the rows is greater than a value

I want to count how many rows have the sum value no greater than X.
Same as this Sum until certain point - MySql but just with an excel formula and only the row count.
Using the same examples as in the above, first limit should give the value 2 and the second 4.
It's easy to do by adding an extra column. In that column you would keep a running total by filling down the a formula like this
Imagining your data has a header row in row 1 and is in A1 to C6 put this in D2 and fill down
=SUM($C$2:C2)
Then in E2 put this
=COUNTIF(D2:D6,"<500")
Changing the number 500 will give you a different limit.

Resources