Excel array query - excel-formula

I'm struggling to understand the mechanics of a particular array formula. I have a row of data ranging from January 2015 to December 2016. Let's assume the data is populated up to October 2016 and the sum in October is £1,000. When data is entered into November 2016 say £1,250, the formula below automatically calculates the delta between the two months. How did the formula do that. Could someone help provide a simple explanation of the below, in particular how it knew to deduct the latest month from the prior month.
=(INDEX(60:60,MAX(IF(M60:AV60<>"",COLUMN(M60:AV60)))))-(INDEX(60:60,MAX(IF(M60:AV60<>"",COLUMN(M60:AV60)-1))))
Thanks for your help,
Miles

It's a little complex, but let's break it down a piece at a time.
This looks to be an array formula, which means that rather than dealing with a single cell, it can deal with a whole set of cells at once.
M60:AV60<>"" This segment produces an array (list) of TRUE and FALSE values, looking at each cell between M60 and AV60. Wherever the cell contains a value - ie is not blank - it returns TRUE. Wherever the cell does not contain a value, it returns FALSE. This list exists only in the program's working memory, and it isn't recorded anywhere in the sheet. So we have something like this:
TRUE
TRUE
TRUE
TRUE
TRUE
TRUE
FALSE
FALSE
FALSE
FALSE
FALSE
COLUMN(M60:AV60) This segment produces another array, the same size as the TRUE/FALSE array above, that simply contains the column numbers of every cell from M60 to AV60. We now have two lists - one containing TRUE/FALSE, and one containing numbers, both the same length.
TRUE | 1
TRUE | 2
TRUE | 3
TRUE | 4
TRUE | 5
TRUE | 6
FALSE | 7
FALSE | 8
FALSE | 9
FALSE | 10
FALSE | 11
IF(M60:AV60<>"",COLUMN(M60:AV60)) This IF statement combines the TRUE/FALSE array with the column numbers array to get something more useful. Wherever there is a TRUE in the first array, it is replaced with the corresponding number from the second array; wherever there is a FALSE in the first array, nothing is changed, and the value stays at FALSE. This way, we end up with a list of numbers, representing the columns of each non-blank cell. It's the equivalent of running the IF formula on all the members of the array.
IF | TRUE |THEN| 1 = 1
IF | TRUE |THEN| 2 = 2
IF | TRUE |THEN| 3 = 3
IF | TRUE |THEN| 4 = 4
IF | TRUE |THEN| 5 = 5
IF | TRUE |THEN| 6 = 6
IF | FALSE |THEN| 7 = 0
IF | FALSE |THEN| 8 = 0
IF | FALSE |THEN| 9 = 0
IF | FALSE |THEN| 10 = 0
IF | FALSE |THEN| 11 = 0
The last column, after the =, is what is passed to the MAX function.
MAX(IF(M60:AV60<>"",COLUMN(M60:AV60))) This segment cuts down the list of numbers to just one number, the Max or highest number in the list. Thus we end up with a single result, which represents the last column that contains a value.
INDEX(60:60,MAX(IF(M60:AV60<>"",COLUMN(M60:AV60))))) The INDEX function looks at all of row 60, and returns a value from a specified column in that row. That being the column returned by the previous segments - the last column that contains a value.
The second half of the formula with the second INDEX function does exactly the same thing, but it subtracts 1 from the column number returned - that is, it gets the second-to-last column that has a value.
The end result is subtracting the second-to-last value from the last value, to get the difference between them.

Related

What does this formula mean in excel? (A cell equals a range)

I see the following formula in a Excel spread sheet and can not understand... Can anyone explain what the test condition "N5=N4:N741" mean?
=MIN(IF(N5=N4:N741,K4:K741))
I made some experiments and still cannot get a clue...
I'm assuming this is an array formula.
What this does is takes the minimum of the range K4:K741 where the value in N4:N741 equals the value in N5.
Let's look at a smaller example. K4:N9 is shown below.
K L M N
----------
4 | 4 2
5 | 8 7
6 | 3 4
7 | 2 1
8 | 7 9
9 | 1 7
The expression N5=N4:N9 is true in row 5 and row 9 since both of those match N5 (value = 7), giving the array {False,True,False,False,False,True} Thus IF(N5=N4:N9,K4:K9) will return {False,8,False,False,False,1} since the True values are replaced by the corresponding row in column K. The MIN() function will then ignore the False parts and return the minimum of the corresponding values in column K (the value 1 since 1 < 8).
I believe it returns an array of true and false values. I also believe the true shows up for the 3 because it is the third item in the array. but that is a guess on my part.
{false, false, true, false,false}
If you change your 5 in E1 to a 1, it will return a true.
Research all of many things excel

Excel: Consider only cells with given value - Recursive formula

I'm trying to make a formula that lets me easily extrapolate a quality within a subser
Let's say I have the following set of data:
Week Name Accepted? Accept Week?
1 a TRUE
1 b TRUE
1 c TRUE
2 d FALSE
2 e TRUE
2 f TRUE
3 g FALSE
3 h FALSE
3 i FALSE
Three weeks, three entries each
I'm trying to make a formula that fills Column 4:
Week 1 would be TRUE because all three entries (B2:B4) are accepted week TRUE
Week 2 has a non accepted entry, therefore all three entries (B5:B7) are FALSE
Week 3 is false as well in Accept Week (B8:B10)
I would appreciate any tip you can give to me.
Use this formula:
=COUNTIFS(A:A,A2,C:C,TRUE) = COUNTIF(A:A,A2)

Subsets, within Excel formula

I would like to learn how to make Excel formulae understand my subsets. An Example:
Week Value Number Accept? First
1 a 11 TRUE a
1 b 12 TRUE FALSE
1 c 13 FALSE FALSE
2 d 13 FALSE f
2 e 12 FALSE FALSE
2 f 12 TRUE FALSE
3 g 12 FALSE #N/A
3 h 13 FALSE FALSE
3 i 13 FALSE FALSE
4 j 14 FALSE k
4 k 14 TRUE FALSE
4 l 12 TRUE FALSE
Column A (Week) defines subsets, separated for clarity.
"First" uses the formula
=IF(A1<>A2,INDEX($B$1:$B$100,MATCH(A2&TRUE,$A$1:$A$100&$C$1:$C$100,0))
Which allows me to select the first value which satisfies condition Accept?, within subsets (It seems rather inefficient, so if anyone has a better idea I'd gladly test it)
Now imagine the following data:
Value Time Price Trigger?
x 14 500
a 11 490
b 12 480
c 13 320
d 14 560
e 15 570 e
f 16 490
g 17 520 g
X is a comparison value. It has 2 parameters: Time & Price
"Trigger" searches for values in the list that have greater time and price than X (Row 2)
The formula for Trigger would be something like:
=IF(AND(B3>$B$2;C3>$C$2);B1;"")
But, how do I make this same thing work, within weeks ("subsets")?
=IF(IFERROR(INDEX(INDEX(A:A,MATCH(A2,A:A,0)):INDEX(D:D,COUNTIF(A:A,"<="&A2)+1),MATCH(TRUE,INDEX(D:D,MATCH(A2,A:A,0)):INDEX(D:D,COUNTIF(A:A,"<="&A2)+1),0),2)=B2,FALSE),B2,FALSE)
Here is the explanations:
INDEX(A:A,MATCH(A2,A:A,0)):INDEX(D:D,COUNTIF(A:A,"<="&A2)+1): This is to determine the range/subset that corresponds to the Week, so you will get $A$2:$D$4, $A$5:$D$7, $A$8:$D$10, $A$11:$D$13 as outcome.
MATCH(TRUE,INDEX(D:D,MATCH(A2,A:A,0)):INDEX(D:D,COUNTIF(A:A,"<="&A2)+1),0): You probably know this already. This is to satisfy the condition on Accept column. But I have to add another range by using INDEX again just for column Accept.
Then I used an IF function to determine if the result is equal to Value column, otherwise will output FALSE. You don't need this if you don't mind the same outcome for every cell on First column.
Lastly, I personally don't like those error code so used IFERROR to output the error message I prefer. You can remove that for your preference.
Hope this helps.

SUMPRODUCT with a conditional with two ranges to calculate

To calculate a margin (JAN) I need to calculate:
sales(loja1)*margin(loja1)+sales(loja2)*margin(loja2)+sales(loja3)*margin(loja3)
/
(SUM(sales(loja1);sales(loja2);sales(loja3))
but I need to make this using a SUMPRODUCT. I tried:
=SUMPRODUCT((B3:B11="sales")*(C3:C11);(B3:B11="margin")*C3:C11))/SUMPRODUCT((B3:B11="sales")*(C3:C11))
but gave error!
When SUMPRODUCT is used to select cells within a range with text, the result for each evaluation will either be TRUE or FALSE. You will need to convert this to 1's or 0's by using '--' before the function so that when you multiply it by another range of cells, you will get the expected value
SUMPRODUCT Example: Sum of column B where column A is equal to 'Sales"
A B
1 | Sales 5
2 | Sales 6
3 | Margin 3
4 | Margin 2
Resulting Formula =SUMPRODUCT(--(A1:A4 = "Sales"),B1:B4)
How SUMPRODUCT works:
First, an array is returned that has True for each value in A1:A4 that equals "Sales", and False for each value that doesn't
Sales TRUE
Sales -> TRUE
Margin FALSE
Margin FALSE
Then the double negative converts TRUE to 1 and False to 0
1
1
0
0
Next, the first array (now the one with 1's and 0's) is multiplied by your second array (B1:B4) to get a new array
1st 2nd New Array
1 * 5 = 5
1 * 6 = 6
0 * 3 = 0
0 * 2 = 0
Finally all the values in the new array are summed to get your result (5+6+0+0 = 11)
Step 1:
For your scenario, you're going to need find the sales amount for each Location and multiply it by the margin for the corresponding location
location 1: sales * margin
=SUMPRODUCT(--(A3:A11="loja1"),--(B3:B11="venda"),(C3:C11)) * SUMPRODUCT(--(A3:A11="loja1"),--(B3:B11="margem"),(C3:C11))
You can do a similar formula for location 2 and 3 and then sum them all together.
Step: 2
To sum the sales for all locations, you can do a similar formula, again using the double negative, i.e. "--"
SUMPRODUCT(--(B3:B11="sales"),(C3:C11))
The resulting formula will be a bit long, but when you divide Step 1 by Step 2, you'll get the desired result

How to get cells value from previous visible row in excel formulas

Is it possible to write a formulas in Excel,to calculate value based on the previous visible row?
By applying a Filter on the columns, the previous visible row changes but the usual formals does not consider visibility of the previous row, so the result does not change by applying filters. For example:
Let's original values of the spreadsheet cells be:
A | B | C | D
1: 5 3 1
2: 9 1 1
3: 2 3 0
4: 7 8 1 =A3-B4 equals 2-8=-6
Now assume that we make a filter on C column to hide the third row so we have
A | B | C | D
1: 5 3 1
2: 9 1 1
4: 7 8 1 =A3-B4 is still equals -6 but I want to get: 9-8=1
Is it possible to get such a formulas? Thank you very much.
Try this formula
=LOOKUP(2,1/SUBTOTAL(3,OFFSET(A$1,ROW(A$1:A3)-ROW(A$1),0)),A$1:A3)-B4
The SUBTOTAL/OFFSET part returns a 1 or zero for each value in column A depending on whether it's visible or not, LOOKUP finds the last 1 (equivalent to the last visible value) and gives that value.

Resources