Related
I have the below table, and have created the Wk2-Wk3 column by using a nested IF/AND function
=
IF(AND(C3="Newbie",D3="Promise"),"N-P",
IF(AND(C3="Newbie",D3="Sleepy"),"N-S",
...
IF(AND(C3="Broken Promise",D3="Newbie"),"Bp-N",
IF(AND(C3="Fallen Star",D3="Newbie"),"Fs-N"
)))))))))))))
ID
Wk 1
Wk 2
Wk 3
Wk2-Wk3
1
Newbie
Promise
Waning Promise
P-Wp
2
Newbie
Sleepy
Hibernating
S-H
3
Promise
Promise
Star
P-S
4
Promise
Waning Promise
Broken Promise
Wp-Bp
5
Hibernating
Hibernating
Newbie
H-N
6
Newbie
Newbie
Promise
N-P
However, as the weeks progress I will be adding additional 'Wk' columns and therefore the last column will be changing to Wk3-Wk4, Wk4-Wk5,
.. and so on.
I don't want to have to keep altering cell references. Is there a way to find the last column using a fixed formula that always uses the two latest weeks.
I tried using OFFSET, but cannot find a way to reference in relation to the formula cell.
The following would not work:
=
IF(AND(((OFFSET(E2,0,-2))="Newbie",((OFFSET(E2,0,-1))="Promise"),"N-P",
IF(AND(((OFFSET(E2,0,-2))="Newbie",((OFFSET(E2,0,-1))="Sleepy"),"N-S",
...
IF(AND(((OFFSET(E2,0,-2))="Broken Promise",((OFFSET(E2,0,-1))="Newbie"),"Bp-N",
IF(AND(((OFFSET(E2,0,-2))="Fallen Star",((OFFSET(E2,0,-1))="Newbie"),"Fs-N"
)))))))))))))
Using latest version of Excel, thank you in advance
Alternatively use INDEX() to retrieve the two cells to the left of the cell holding the formula:
Formula in E2:
=REDUCE("",INDEX(2:2,COLUMN()-{2,1}),LAMBDA(a,b,TEXTJOIN("-",,a,PROPER(CONCAT(LEFT(TEXTSPLIT(b," ")))))))
Looking at the logic in your IF() it seems you don't really need all these nested statements if you actually just need the first letter of each word. I did mimic this logic and used TEXTSPLIT() to retrieve every leftmost character of each word in the cells, concatenate them and use PROPER() before TEXTJOIN().
This would, however, require access to ms365's insiders channel.
If I understand correctly, then OFFSET is indeed what you can use, it just might seem momentarily confusing until you see it in action.
Try this demo on a new worksheet:
In the first 3 cells (A1:C1) enter "whatever" random values. I used 1,2,3, like this:
In D1 enter formula: =OFFSET(D1,0,-2)
In E1 enter formula: =OFFSET(E1,0,-2)
Note that both formulas refer to themselves. With most functions, this would create a circular reference error, but in this case, it's not looking for a value in that cell, just a starting point.
After the cell reference, the next two values in the function are the number of rows, and then number of columns, to offset by. Negative numbers would count "up" for rows, or "left" for columns, and positive numbers count "down" and "right".
Technically, OFFSET can take 2 other values too (for 5 parameters in total) but in this case, you can ignore the last 2. (They are to specify height and width, irrelevant unless you get into working with array formulas.)
So, the function in D1 is offsetting from cell D1 (itself), by zero rows (so neither up nor down), and "2 columns to the left".
→ And now, if you drag D1 and E1 elsewhere (or insert columns/rows, etc) those 2 cells will automatically count the offset from the new location.
Just make sure you keep them as relative references (so D1, not $D$1) or else it will count from D1 regardless of where you move the formula.
...clear as mud? :-)
As an afterthought, here are a couple of other ways to visualize how OFFSET counts the cells to offset:
Use a lookup table (Insert > Table from the ribbon, or alternatively create an ordinary range and use cell references instead of the table syntax in the formula below).
Value
Abbreviation
Newbie
N
Promise
P
Waning promise
Wp
...
...
From this, the value in your Wk2-Wk3 column can be calculated with a VLOOKUP(), XLOOKUP() or INDEX(MATCH()). For example,
=VLOOKUP(C3,LookupTable[[Value]:[Abbreviation]],2,FALSE)&"-"&VLOOKUP(D3,LookupTable[[Value]:[Abbreviation]],2,FALSE)
where LookupTable is the name of the table (you can set this on the "Table Design" ribbon tab).
Well, I did it like this, using text functions:
IF(IFERROR(FIND(" ",C3,1),0),LEFT(C3,1)&LOWER(MID(C3,FIND(" ",C3,1)+1,1)),LEFT(C3,1))&"-"&IF(IFERROR(FIND(" ",D3,1),0),LEFT(D3,1)&LOWER(MID(D3,FIND(" ",D3,1)+1,1)),LEFT(D3,1))
And, if you add the new week data columns appropriately then all you need to do is drag right...
Within Excel, is there an array formula or something else that could shorten the formula below? This is only an example going through 12 rows. The actual formula would have thousands of rows, which is why I'd like to find a way to write this formula much shorter. I've considered and tried SUMIF and SUMPRODUCT in addition to what's below, but I haven't found a way for it to check for a specified value in multiple columns, and then doing that for many rows, like a FOR loop would do. The below formula is in Cell J3. I have attached an image of the spreadsheet example.
=SUM(
IF(ISNUMBER(MATCH($I$3,$B3:$E3,0)),$F3,0),
IF(ISNUMBER(MATCH($I$3,$B4:$E4,0)),$F4,0),
IF(ISNUMBER(MATCH($I$3,$B5:$E5,0)),$F5,0),
IF(ISNUMBER(MATCH($I$3,$B6:$E6,0)),$F6,0),
IF(ISNUMBER(MATCH($I$3,$B7:$E7,0)),$F7,0),
IF(ISNUMBER(MATCH($I$3,$B8:$E8,0)),$F8,0),
IF(ISNUMBER(MATCH($I$3,$B9:$E9,0)),$F9,0),
IF(ISNUMBER(MATCH($I$3,$B10:$E10,0)),$F10,0),
IF(ISNUMBER(MATCH($I$3,$B11:$E11,0)),$F11,0),
IF(ISNUMBER(MATCH($I$3,$B12:$E12,0)),$F12,0),
IF(ISNUMBER(MATCH($I$3,$B13:$E13,0)),$F13,0),
IF(ISNUMBER(MATCH($I$3,$B14:$E14,0)),$F14,0))
Use SUMPRODUCT like this:
=SUMPRODUCT($F$3:$F$14*(MMULT(N($B$3:$E$14=I3),TRANSPOSE(COLUMN($B$3:$E$14)^0))>0))
This is an array formula and it needs to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
Improved solution
=SUMPRODUCT($F$3:$F$14,CEILING(((($B$3:$B$14=$I3)+($C$3:$C$14=$I3)+($D$3:$D$14=$I3)+($E$3:$E$14=$I3))/4),1))
Idea : 4 is the number of rows/week. Use ceiling() to normalize value instead of int() n sqrt().
Expansion note : just add another row condition & adjust 4 to the number of rows.
Past solution (for reference)
=SUMPRODUCT($F$3:$F$14,INT(SQRT(INT(SQRT(($B$3:$B$14=$I3)+($C$3:$C$14=$I3)+($D$3:$D$14=$I3)))+($E$3:$E$14=$I3))))
should do.
idea : while $B$3:$B$14=$I3 part is creating an array of 0 n 1, the INT() n SQRT() function 'force' the '+' sum to become 1 even if there is more than 1 match in the same month.
please share if it works/not. (:
p/s : (note for expansion) Upon dissecting the int() and sqrt() function, you can see that sqrt(3)=1.73205 , sqrt(2)1.414213, sqrt(1)=1 and its int() results the same value (1) . So let say you want to add more rows, just use 'bundle' 3 rows together in one int(sqrt(__)) function, recursively.
I have two sheets in my excel file.
The first sheet contains values and dates, on the second I want to calculate values between a date range.
for instance:
50 20/9/2014
1600 16/10/2014
254 21/10/2014
1547 03/11/2014
(Belgian time)
I tried this with the following formula:
=SUMIFS(INPUT!$G$3:$G$100; INPUT!$H$3:$H$27; ">=01/10/2014"; INPUT!$H$3:$H$27; "<=31/10/2014")
This gives me an error in the formula and I notice that inside the formula values the date has become a random value:
Anyone has an idea how to do this correctly?
I couldn't find it at first either, but it seems your ranges need to be the same size (sum_range and criteria).
So change your sum_range to G3:G37 or your criteria range to H3:H100 and it will solve your problem. :)
Reference: http://www.excelforum.com/excel-formulas-and-functions/731875-sumifs-returns-value.html
I am analysing library statistics relating to loans made by particular user categories. The loan data forms the named range LoansToApril2013. Excel 2007 is quite happy for me to use an index range as the sum range in a SUMIF:
=SUMIF(INDEX(LoansToApril2013,0,3),10,INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6))
Here 10 indicates a specific user category, and this sums loans made to that group from three columns. By "index range" I'm referring to the
INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6)
sum_range value.
However, if I switch to using a SUMIFS to add further criteria, Excel returns a #VALUE error if an index range is used. It will only accept a single index.
=SUMIFS(INDEX(LoansToApril2013,0,4),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
works fine
=SUMIFS(INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
returns #value, and I'm not sure why.
Interestingly,
=SUMIFS(INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,4),INDEX(LoansToApril2013,0,3),1,INDEX(LoansToApril2013,0,1),"PTFBL")
is also accepted and returns the same as the first one with a single index.
I haven't been able to find any documentation or comments relating to this. Does anyone know if there is an alternative structure that would allow SUMIFS to conditionally sum index values from three columns? I'd rather not use three separate formulae and add them together, though it's possible.
The sumifs formula is modelled after an array formula and comparisons in the sumifs need to be the same size, the last one mimics a single column in the LoansToApril2013 array column 4:4 is column 4.
The second to bottom one is 3 columns wide and the comparison columns are 1 column wide causing the error.
sumifs can't do that, but sumproduct can
Example:
X 1 1 1
Y 2 2 2
Z 3 3 3
starting in A1
the formula =SUMPRODUCT((A1:A3="X")*B1:D3) gives the answer 3, and altering the value X in the formula to Y or Z changes the returned value to the appropriate sum of the lines.
Note that this will not work if you have text in the area - it will return #VALUE!
If you can't avoid the text, then you need an array formula. Using the same example, the formula would be =SUM(IF(A1:A3="X",B1:D3)), and to enter it as an array formula, you need to use CTRL+SHIFT+ENTER to enter the formula - you should notice that excel puts { } around the formula. It treats any text as zero, so it will successfully add up the numbers it finds even if you have text in one of the boxes (e.g. change one of the 1's in the example to be blah and the total will be 2 - the formula will add the two remaining 1s in the line)
The two answers above and a bit of searching allowed me to find a formula that worked. I'll put it here for posterity, because questions with no final outcome are a pain for future readers.
=SUMPRODUCT( (INDEX(LoansToApril2013,0,3)=C4) * (INDEX(LoansToApril2013,0,1)="PTFBL") * INDEX(LoansToApril2013,0,4):INDEX(LoansToApril2013,0,6))
This totals up values in columns 4-6 of the LoansToApril2013 range, where the value in column 3 equals the value in C4 (a.k.a. "the cell to the left of this one with the formula") AND the value in column 1 is "PTFBL".
Despite appearances, it isn't multiplying anything by anything else. I found an explanation on this page, but basically the asterisks are adding criteria to the function. Note that criteria are enclosed in their own brackets, while the range isn't.
If you want to use names ranges you need to use INDIRECT for the Index commands.
I used that formula to check for conditions in two columns, and then SUM the results in a table which has 12 columns for the months (the column is chosen by a helper cell which is 1 to 12 [L4]).
So you can do if:
Dept (1 column name range [C6]) = Sales [D6];
Region (1 column name range [C3]) = USA [D3];
SUM figures in the 12 column monthly named range table [E7] for that 1 single month [L4] for those people/products/line item
Just copy the formula across your report page which has columns 1-12 for the months and you get a monthly summary report with 2 conditions.
=SUMPRODUCT( (INDEX(INDIRECT($C$6),0,1)=$D$6) * (INDEX(INDIRECT($C$3),0,1)=$D$3) * INDEX(INDIRECT($E7),0,L$4))
I have two columns of numbers. Both are 1 to 5. I want to count all the cells where the left column value equals the right column value AND the left column value equals a certain value.
I tried this:
=SUM(IF(W2:W13=X2:X13 AND W2:W13=4,1,0))
I've tried pressing Ctrl+Shift+Enter and it adds {} around the formula but that didn't help either.
I think it's the W2:W13 = 4 part that doesn't work
=COUNTIFS(W2:W13,"=4", X2:X13, "=4")
You can use the sumif() function:
SumIf( range, criteria, sum_range )
it will apply the criteria for each row in the range.
Edit: to count the matches, you can use sum_range = 1 or use the Countif() function suggested by Ben in his answer
Have you considered a third column (C) with the formula IF(A1=B1,1,0) and then summing that third column?
I'm not much of an Excel Expert, but didn't they craeted the COUNTIF(range, criteria) function for this?
Add a third column eg Z2:Z13 with this formula: IF(AND(W2=X2; W2=4); 1; 0)
Then sum that one.
I don't have Excel 2007. So here's how you can do it in Excel 2003:
=COUNT(IF((W2:W14=4)*(X2:X14=4),Y2:Y14))
Since you are looking for a specific value and the column next to it to be the same value, you can just compare both columns to the same value.
The trick to get this to work is after entering the formula you need to hit F2 to go into edit mode and then hit CTRL-SHIFT-ENTER which makes this formula an array formula. This will put {} around the entire formula. Without making this an array formula this formula won't work.
I found this information in the Excel help document titled Count how often a value occurs