Combine VLOOKUP and SUMIF in one step - excel-formula

What I want to do (if possible) is sum each metric of each transaction with a common customer_id in ONE STEP. The output should look like the "DESIRED OUTPUT" sheet. I know some sort of lookup will need to be used to reference the "CUSTOMER LOOKUP" table.
I can see how to accomplish this using VLOOKUP then SUMIF (or the other way around), but I would like to accomplish it using a single formula if possible.
TRANSACTIONS
transaction customer_id metric_1 metric_2
1 1 0 564
2 1 0 762
3 1 5.305 8367
4 2 0 150
5 2 3.125 4109
6 2 6.18 1853
7 3 0 61
8 3 0 1
9 3 0 4
CUSTOMER LOOKUP
customer_id customer_name
1 XXX
2 YYY
3 ZZZ
DESIRED OUTPUT
customer_name metric_1 metric_2
XXX 5.305 9693
YYY 9.305 6112
ZZZ 0 66

I doubt it can be done with one formula. This is a very common scenario and is typically solved by adding a Vlookup column to the first table to show the customer name.
Then you can build a pivot table with a few clicks.
I guess, since using a pivot table does not involve formulas, the only formula you need is the Vlookup. So, yes, it can be done with one formula ;-)

Related

Formula to put 1 in the first value and 0 for the rest. Duplicate dataset

I'm looking for a smart way to indicate only one row with duplicated values. I need it to help my pivot table a little.
Id Description Estimation(h) Formula
1 Desc1 20 1
1 Desc2 20 0
1 Desc3 20 0
2 Desc4 30 1
2 Desc5 30 0
2 Desc6 30 0
This looks quite simple: I just put the value 1 as the first value, and the rest of the column is based on the following formula:
=IF(A2=A3;0;1)
(I've put this formula in cell D3)
This option will work even if the Id is not sorted:
=--(COUNTIF($A$2:A2,A2)=1)

Subtotals grouped by value using COUNTIF to create ranges for SUMIF, but in a single formula

End-goal: A column with the subtotals of groups (defined in the below table as all foods listed above Zucchini, incl Zucchini).
Current attempt: create a column to define groups using COUNTIF('count all 'zucchini' thus far'). Then use SUMIF to get the total cost for the current group.
Problem: I don't know how to do this without the COUNTIF column (since SUMIF needs range C:C to be resolved first). I'd like to have it in a single formula. I looked into array formulas but not sure if/how to apply that here.
FOOD COST COUNTIF(A2:A$2;"Zucchini") SUMIF(C:C;C2;B:B)
Apple 3 0 12
Pecan 7 0 12
Zucchini 2 0 12
Apple 4 1 23
Olive 8 1 23
Pecan 6 1 23
Zucchini 5 1 23
Apple 4 2 16
Olive 9 2 16
Zucchini 3 2 16
Any ideas on how to solve either the current problem or the end-goal problem? Thanks!
Put this in C2 and copy down:
=IF(A2="Zucchini",SUM($B$1:B2)-SUM($C$1:C1),"")
It basically sums everything to the row and subtracts what is already accounted for.

Excel using SUMIF to calculate totals of multiple columns

I'm trying to use Excle's SUMIF to calculate totals of Col1 to Col5 for dates that are similar.
My formula is as follows =SUMIF($A2:$A7,A10,$B2:$F7), but this only gives me the total of a single column.
How can I get the Totals of all the columns based on the date like I've shown in my results.
Date Col 1 Col 2 Col 3 Col 4 Col 5
1/5/2017 1 2 2
1/5/2017 5 3 1
1/5/2017 9 5 5
2/5/2017 10 5 3
2/5/2017 20 10 3
2/5/2017 6 8 1 5
Desired Results
1/5/2017 15 7 7 3 1
2/5/2017 30 11 11 11 8
use below formula in cell B11
=SUMIF($A$2:$A$7,$A11,B$2:B$7)
Per the example you provided, One solution is to use SUMPRODUCT
Multiplies corresponding components in the given arrays, and returns the sum of those products
Microsoft Docs give a thorough example, but per SO etiquette, here is an example in case of link-rot: [FYI, I used absolute reference for easier filling across, arbitrary how you get it done though]
Forumlas shown:
Formula is kind of hard to see without clicking on image:
=SUMPRODUCT(($B$3:$B$8=$B$11)*C3:C8)
This basically breaks down like this, it searches the B:B column for a match, and it will naturally return a true or false for the match, or 0/1 counterparts, and multiplys that by the number found in the column to the right (C3:C8), so it will either be 1 * # = # or 0 * # = 0

Find Largest Value in one cell and then display a different cell in the same row

This was a fairly difficult question to put to words but hopefully the example data and my attempt will help
I have the following
Apple 17 1 0 0 0 0 0 0 0 0 0 0
Orange 14 1 15 1 6.67 1 6.67 1 6.67 2 13.33 10
Banana 15 3 5 0 0 0 0 0 0 0 0 1
Cherry 13 1 12 2 16.67 2 16.67 2 16.67 2 16.67 2
Peach 16 4 12 1 8.33 1 8.33 2 16.67 2 16.67 8
Strawberry 12 5 6 1 16.67 1 16.67 1 16.67 1 16.67 7
I am trying to find the max value in M and then display A from the same row. So in this example the max value would be 10 and "Orange" should be displayed.
It should be noted that I am using 2 sheets, "Data" has my output and "Raw Data" has... the raw data
=VLOOKUP(MAX('Raw Data'!M1:M6), A1:M6, 1, FALSE)
This produces #N/A and I think it might have something to do with my data either not being formatted into a table (though doing so seems to fail with a different error) or running from one sheet to the other.
Any input would be greatly appreciated
VLOOKUP requires that the value to find is in the first column.
Use INDEX/MATCH:
=INDEX(A1:A6,MATCH(MAX(M1:M6),M1:M6,0))
Try this =INDIRECT(ADDRESS(MATCH(MAX(M1:M6),M1:M6,0),1))
I recomend using Index Match as Scott Craner has shown.
But if you are set on using vlookup:
I will first shortly explain vlookup as how I have understood it.
First a limitation, vlookup can only return values to the left of your search range, i.e you cant use negative numbers i.e,
=vlookup(Max('Raw Data'!M1:M6), A1:M6, **"-1"**, False)
Secondly, vlookup is structured like this:
=vlookup(The value you want to search for, the range you want to work with, which column you want to return a value from (1 means the first column).
vlookup will allways search for the vale you want to search for in the first column.
So in your data:
You are telling vlookup to search for the maximum value that exists in the range M1:M6. In this case 10 right?
You are telling vlookup to search for this value in column A which contains the fruits. This is where it goes wrong.
My solution to this would be to move all the fruits names to the N column (to the left of you values you want to search).
In code:
In the N Column:
Put = A and the row number
Now do a vlookup like this:
=vlookup(Max('Raw Data'!M1:M6), M1:N6, 2, False)
I hope this can solve your problem.
Best regards,

Multiple criteria lookup in categorical data

I've got some raw data like the one shown below and I want to search the value under, Category B, sub-category 3. This is something like a pivot table in compact display form.
I cannot simply use MATCH function since the name of the category is not displayed on each row. I reckon I could probably use an extra lookup column and add some mappings but that seems rather fiddly.
Category Sub-Category Value
A 1 431
2 543
3 13421
4 754
5 43
B 1 456
2 35
3 906
C 1 562
2 13
3 1243
4 765
5 35
6 12
You need to use INDEX/MATCH function pairs to reshape the ranges searched through with the lookups.
        
The standard formula in G2 is,
=INDEX(INDEX(C$2:C$999, MATCH(E2, A$2:A$999, 0)):C$999, MATCH(F2, INDEX(B$2:B$999, MATCH(E2, A$2:A$999, 0)):B$999, 0))
Fill down as necessary.

Resources