SUMPRODUCT with a conditional with two ranges to calculate - excel

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

Related

Excel formula: For each instance of peak/bottom value in column, get range/distance to the second next peak/bottom

I am looking to solve the following problem in Excel:
ID Value Distance
1 1 3
2 0 0
3 -1 3
4 1 0
5 0 0
6 -1 0
7 0 0
Essential the distance column is what I want. It looks at peak/bottom values(1 and -1), then scrolling down to find the second next peak or bottom and compute the distance. For example, for ID 1, since it is peak, we looking for the second peak/bottom, ID 3 should be skipped since its the first, so we look at ID 4 and get distance = 4-1 = 3
Try following formula:
=IFERROR(AGGREGATE(15,6,A2:$A$18/ABS(B2:$B$18),3)/ABS(B2)-A2,0)
Explanation:
AGGREGATE function with first two parameters 15, 6 and last 3 returns the third smallest value in the array A2:$A$18/ABS(B2:$B$18) ignoring errors - in the first row after division the array looks like this [1, #DIV/0!, 3, 4, #DIV/0!, 6, #DIV/0!, ...] and returns 4.
Next, this value is divided by the absolute value of column B of the current row (if we divide by 0, then we get an error and the IFERROR function returns 0).
Then we subtract the value of column A of the current row from the obtained result (in the first row 1) and we get the desired distance - 3
To get the third and subsequent values, increase the last parameter of the AGGREGATE function accordingly.

Sum rows in one column between 2 time range

Is there a way to sum values in a column between two different time. The two different hrs are different for different start and end calculation. The formula i used is =SUMIFS(Period,time,">="&Time Range,time, Range,"<="&Time Range) , but the formula returns only the first hour value but the expected result should be between total of start calc and end cal value
time period Cal stat Time Range Expected Result
6:37:02 AM 1 start calc 6:37:02 AM 4
6:37:03 AM 1 no action 0
6:37:04 AM 2 End Calc 6:37:04 AM
6:37:06 AM 1 Start Calc 6:37:06 AM 6
6:37:07 AM 1 no action 0
6:37:08 AM 2 no action 0
6:37:10 AM 1 no action 0
6:37:11 AM 1 End Calc 6:37:11 AM
6:37:12 AM 2 Start Calc 6:37:12 AM 3
6:37:14 AM 1 End Calc 6:37:14 AM
6:37:15 AM 1 no action 0
6:37:16 AM 1 no action 0
6:37:17 AM 2 no action 0
6:37:19 AM 2 no action 0
I don't know about doing it with SUMIFS.
However, with INDEX(MATCH(…, you can construct a formula that will sum from the current row to the next "end Calc", and use an IF(… to only show results in the start calc rows.
E2: =IF(C2="start calc", SUM( INDEX(B2:$B$15,1): INDEX(B2:$B$15,MATCH(TRUE,C2:$C$15="end Calc",0))),"")
Fill the formula down as far as needed
The $15 row numbers in the formula can be changed to any row that is at least as large as your expected table, but be sure it remains an absolute address.
e.g: =IF(C2="start calc", SUM( INDEX(B2:$B$1500,1): INDEX(B2:$B$1500,MATCH(TRUE,C2:$C$1500="end Calc",0))),"")

Logical Functions in Excel for aggregate data

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))

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

Resources