Logical Functions in Excel for aggregate data - excel-formula

In MS Excel, I am trying to write an aggregate function that counts the number of entries in a data table that meets a set of requirements.
I have tried using the AND(), OR() operators, but they only return a single value even when I input an array.
AND(1={1,1},1={1,1}) returns TRUE, instead of {TRUE, TRUE}
I have also tried using * as AND and + as OR, but for some reason this is what I get.
1={1;1}*1={1;1} returns {FALSE;FALSE} when I am expecting a {TRUE;TRUE}
However, when I put it all together, it works except when (TRUE + TRUE) * TRUE, it evaluates into FALSE instead of TRUE. These are the functions I am using below and their expected results. (header is row 0)
count - {sum(IF( (A1:A5=1 + B1:B5=1)*C1:C5=1 , 1, 0))} = 2
sum - {sum(IF( (A1:A5=1 + B1:B5=1)*C1:C5=1 , D1:D5, 0))} = 7
min - {min(IF( (A1:A5=1 + B1:B5=1)*C1:C5=1 , D1:D5, 9999999))} = 3
max - {max(IF( (A1:A5=1 + B1:B5=1)*C1:C5=1 , D1:D5, 0))} = 4
A B C D
1 1 1 3
1 0 1 4
0 0 0 5
0 0 1 6
1 1 0 7

If you array enter then it does return an array, but you need to array enter over many cells.
For example: Highlight a range 2 high and 2 wide. In the formula box put:
=(1={1,0})*(1={0;1})
and hit Ctrl-Shift-Enter. You will return an array of values:
By adding the SUM to the outside we are returning that array to the sum:
=SUM((1={1,0})*(1={0;1}))
Hit Ctrl-Shift-Enter and you will get the sum of the array or 1
If you want to display the array, one must array enter the formula over many cells, otherwise the display will only be the upper left value of the array.

If our data is like:
So we want to count rows where either A or B or both are 1 and C is a one.
(the 12 yellow rows)
We can use:
=SUMPRODUCT(--((A1:A28)+(B1:B28)<>0)*(C1:C28))

Related

Subtract cells when the condition matches in a Row

How do I subtract horizontal multiple cells when the condition matches.
If match found then return subtracted value if not then return current value.
I tried the below formula but not able to do multiple matches
=IF(ROW(A3)=2,0,D3-D2)
Date Type Content Value Answer
1-Oct-18 Type 1 Content 1 7 7
1-Oct-18 Type 1 Content 1 7 0
1-Oct-18 Type 1 Content 1 9 2
2-Oct-18 Type 2 Content 1 8 8
2-Oct-18 Type 2 Content 2 10 10
2-Oct-18 Type 2 Content 2 3 -7
Put this in E2 and copy down:
=D2-SUMIFS($E$1:E1,$B$1:B1,B2,$C$1:C1,C2)
You do not need to check the row here as your current formula does. ROW(A3) will always return the row. Thus your test statement can be reduced to 3 = 2 which will always show TRUE
The equation you are looking for is =IF(A2=A1, D2-D1, 0)

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

CountifS + multiple criteria + distinct count

I'm looking for a formula calculating : distinct Count + multiple criteria
Countifs() does it but do not includes distinct count...
Here is an example.
I have a table on which I want to count the number of distinct items (column item) satisfying multiple conditions one column A and B : A>2 and B<5.
Image description here
Line Item ColA ColB
1 QQQ 3 4
2 QQQ 3 3
3 QQQ 5 4
4 TTT 4 4
5 TTT 2 3
6 TTT 0 1
7 XXX 1 2
8 XXX 5 3
9 zzz 1 9
Countifs works this way : COUNTIFS([ColumnA], criteria A, [ColumnB], criteria B)
COUNTIFS([ColumnA], > 2 , [ColumnB], < 5)
Returns : lines 1,2,4,5,8 => Count = 5
How can I add a distinct count function based on the Item Column ? :
lines 1,2 are on a unique item QQQ
lines 4,5 are on a unique item TTT
Line 8 is on a unique item XXX
Returns Count = 3
How can I count 3 ?!
Thanks
You can download the excel file # Excel file
Newer versions of Excel allow for this problem to be solved in a (relatively) more simple way. It certainly is easier to follow and understand, conceptually.
First, filter the table based on multiple criteria (join multiple with the *):
=FILTER(Table,(Table[Column A]>2)*(Table[Column B]<5))
Then, grab the "Item" column with INDEX:
=INDEX(FILTER(Table,(Table[Column A]>2)*(Table[Column B]<5)),,2)
Next, filter for unique entries:
=UNIQUE(INDEX(FILTER(Table,(Table[Column A]>2)*(Table[Column B]<5)),,2))
Finally, perform a count:
=COUNTA(UNIQUE(INDEX(FILTER(Table,(Table[Column A]>2)*(Table[Column B]<5)),,2)))
Ugly formula, but it works.
=SUM(((FREQUENCY(IF(C2:C10>2,1,0)*IF(D2:D10<5,1,0)*(COUNTIF(B2:B10,">"&B2:B10)+1),ROW(B2:B10)-ROW(B2)))*(ROW(B2:B11)-ROW(B2))>0)*1)
I'll start with the criteria IFS:
IF(C2:C10>2,1,0)*IF(D2:D10<5,1,0)
Gives an array of 1s and 0s for the rows that satisfy both criteria. ARRAY = {1;1;1;1;0;0;0;1;0} for your example.
Where B2:B10 is the Item column, the countif formula:
COUNTIF(B2:B10,">"&B2:B10)
returns {6;6;6;3;3;3;1;1;0} where the number equals the number of item values in B2:B10 alphabetically less than the tested item value.
QQQ goes to 6 [3"TTT", 2"XXX", 1"zzz"]
TTT goes to 3 [2"XXX", 1"zzz"]
XXX goes to 1 [1"zzz"]
zzz goes to 0 [0 less than "zzz"]
Need to add 1 to this array to make sure there are no 0 values:
{7;7;7;4;4;4;2;2;1}.
So when multiplying the criteria, and the countif statement:
(IF(C2:C10>2,1,0)*IF(D2:D10<5,1,0)*(COUNTIF(B2:B10,">"&B2:B10)+1)
You get ARRAY = {7;7;7;4;0;0;0;2;0}.
FREQUENCY(ARRAY,ROW(B2:B10)-ROW(B2))
ROW(B2:B10)-ROW(B2) sets the frequency bins to {0;1;2;3;4;5;6;7;8}. So the output of the frequency formula is {4;0;1;0;1;0;0;3;0;0} where the last 0 is for all values greater than 8.
((ROW(B2:B11)-ROW(B2)>0)*1) equals {0;1;1;1;1;1;1;1;1;1}. Multiplying ARRAY by this removes the 0 count at the start: ARRAY = {0;0;1;0;1;0;0;3;0;0}. [NOTE: B11 is lowest item column cell+1 because of the added array value from the frequency formula for values over 8]
(ARRAY)>0)*1 = {0;0;1;0;1;0;0;1;0;0}
SUM this = 3.
ctrl + shift + enter, because it's an array formula.
cmd + shift + enter for mac.
You could try this:
=SUMPRODUCT(1/COUNTIF(B2:B10,B2:B10))
Credit where credit due, however ... I found it over here:
https://exceljet.net/formula/count-unique-values-in-a-range-with-countif

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

What is wrong with formula: =IF(A1>B4:D4,1,0)?

Question 1
What is wrong with formula?
=IF(A1>B4:D4,1,0)
I want if cell A1 is greater than set of cells (B4:D4) then it returns 1.
Answered
Question 2:
How can i select/indentify two MAX values from set of cells? I want to count two max values.
For example:
(header) A B C D E
(row1) 1 5 4 1 3
It should return
(header) F G H I J
(row1) 0 1 1 0 0
For top 2 values I would use the LARGE function
=IF(A1>LARGE(B4:D4,2),1,0)
The LARGE function returns the nth largest value, so LARGE(B4:D4,1) would be equivalent to MAX(B4:D4), but LARGE(B4:D4,2) returns the 2nd largest value
I guess you mean bigger than the max of them:
=IF(A1>MAX(B4:D4),1,0)

Resources