Vlookup and sum all instances of the matching lookup - excel

this is probably an extremely basic problem but I can't figure a way to do this within Excel.
I have a list of values IDs from a regional sales manager,
he wants to see if his figures have been inputted into the monthly report and are reporting correctly.
However, as he sells by region and we report by product we have multiple instances of his ID by Product
so our monthly report.. for example
ID Value
1 16,999
1 22,521
3 400
3 221
5 71
6 22,000
So he has provided me with a list of IDs
1
2
3
4
5
6
and wants a list with its total revenue not just the first match up.
Is an excel limitation or is there a way to do this?

You can use a SUMIF formula. Basically you give it the column to check the value of, then you give it the expected value and finally the colum to sum.
Option 1 (whole range)
=SUMIF(A:A, 1, B:B)
Option 2 (defined range)
=SUMIF(A1:A7, 1, B1:B7)
Option 3 (Using excel table)
=SUMIF([Id], 1, [Value])
For more details please refer to: https://support.office.com/en-ie/article/sumif-function-169b8c99-c05c-4483-a712-1697a653039b

Is this what you're looking for?
=SUMPRODUCT((A1:A5=1)*B1:B5)
Taken from here: Sum values from multiple rows using vlookup or index/match functions
How it works:
An array formula is used to create an array of numbers that gets passed to the SUMPRODUCT formula.
The array formula processes A1 and B1 together, then A2 and B2 and so on... if A1 is equal to 1, B1 is added to the array.

Related

Single formula with Multiple Countifs and where data table contains duplicate values in more than one column

The excel file contains a structure table. (which I find difficult to work with but I can't alter the file structure, I am just adding a summary tab with various counts from the data table.)
Item
Month
1
2
2
2
2
2
2
3
3
3
3
4
The item number itself can repeat and be repeated within the same month.
I was able to get a count of total number of unique items using this formula.
=SUMPRODUCT((Item_tab[Item]<>"")/COUNTIF(Item_tab[Item],Item_tab[Item]&""))
Looking for how to count the number of unique items within a specific month.?
Example of Desired output.
Edit/Set formula for Month 2 , output should be 2
Edit/Set formula for Month 3 , output should be 2
Edit/Set formula for Month 4 , output should be 1
I cannot figure out how to apply countifs to something that needs to avoid duplicates in multiple columns.
Tried this but returns #VALUE!
=SUMPRODUCT((Item_tab[Item]<>"")/COUNTIFS(Item_tab[Item],Item_tab[Item]&"",'Item'!M:M,6))
The month column was added later and I don't know how why or how to get that column to have the structured table column Header like everything else.
The sheet name is Item
The Table Name is Item_tab
I can find where to rename the table but not the columns. Not sure the variance is causing an issue. The month column is absolutely part of the table in every way that I can see.
You can try the following in cell D2:
=LET(A, A2:A7, B, B2:B7, uxB, UNIQUE(B), cnts, BYROW(uxB, LAMBDA(u,
ROWS(UNIQUE(FILTER(A, B=u))))), HSTACK(uxB, cnts))
Here is the output:
If you want to get the result repeated instead of consolidating the result by unique months, you can try the following:
=LET(A, A2:A7, B, B2:B7, uxB, UNIQUE(B), BYROW(B, LAMBDA(u,
ROWS(UNIQUE(FILTER(A, B=u))))))
If you don't have such function available, you can try the following formula in D2, it produces the same result as the first formula:
=CHOOSE({1,2}, UNIQUE(B2:B7),
MMULT(N(MMULT(TRANSPOSE(N(B2:B7=TRANSPOSE(UNIQUE(B2:B7)))),
N(A2:A7=TRANSPOSE(UNIQUE(A2:A7))))>0), SEQUENCE(ROWS(UNIQUE(A2:A7)),,1,0)))
or using LET for a better reading and maintenance:
=LET(A, A2:A7, B, B2:B7, CHOOSE({1,2}, UNIQUE(B),
MMULT(N(MMULT(TRANSPOSE(N(B=TRANSPOSE(UNIQUE(B)))),
N(A=TRANSPOSE(UNIQUE(A))))>0), SEQUENCE(ROWS(UNIQUE(A)),,1,0))))

How to create a dynamic formula to find the average of a set of values for a given vector

I am trying to create a formula that gives me the average of the last 12 entries in a given dataset depending on the associated vector.
Let's make an example:
I have in column F2,G2,H2 and I2 dates, Company1, Company2 and Company3 respectively. Then from row3 to row 33 I have months dates starting from May 2016.
Date Company1 Company2 Company3
May-16 2,453,845
Jun-16 13,099,823
Jul-16 14,159,037
Aug-16 38,589,050 8,866,101
Sep-16 63,290,285 13,242,522
Oct-16 94,005,364 14,841,793
Nov-16 123,774,792 7,903,600 41,489,883
Dec-16 93,355,037 12,449,604 69,117,105
Jan-17 47,869,982 13,830,712 83,913,764
Feb-17 77,109,905 10,361,555 68,176,643
The goal is to create a formula that, when I drag it down, correctly calculates the average of the last 12 values for a given company.
So for example i would have, say in table "B2:C5":
Company1 76,856,345
Company2 11,120,859
Company3 65,674,349
And, if a new Company4 is added to the list, then I just have to drag it down the formula, to calculate the average of the last 12 months for Company4.
Until now, I have came up with this formula:
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(G:G),ROW(G:G)),ROW(INDIRECT("1:"&MIN(12,COUNT(G:G))))),ROW(G:G),G:G ))
This formula correctly calculates the average of a given column, considering only the last 12 values. The last step would be to come up with a formula that includes all the columns and then calculates the average for the given company.
Thanks!
I recommend that you use a named range to define your data in columns G:I. When a company is added, just modify the named range's specs. I used the name Target. Of course, you can replace it with $G:$I if you feel so inclined but I would rather recommend reducing the number of rows in the range, which is easier to manage when it is named.
Use the formula below to extract the company names from the first row of Target into the first column of your averages table. This is to ensure that the names are spelled identically in both locations.
=INDEX(Target,1,ROW()-2)
The number 2 indicates the number of rows above the row containing the formula. it is copied here from cell M3. There, ROW()-2 creates the number 1, counting sequentially as the formula is copied down.
Now I have the formula below in my cell N3 and copied down.
=SUM(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))
The formula simply sums up the columns G, H, and I in 3 consecutive rows.
In the final step I inserted the range definition established above, meaning excluding the SUM() function, into your existing formula.
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))),ROW(INDIRECT("1:"&MIN(12,COUNT(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))

Getting the next higher value with VLOOKUP or INDEX/MATCH

I have the following Excel spreadsheet:
A B C D
1 0 Product 1 7.500 Product 4
2 1.000 Product 2
3 5.000 Product 3
4 10.000 Product 4
5
In Cell C1 I type a random number (in this case 7.500). Now I want that in Cell D1 the corresponding Product is shown to the value in Cell C1. Since 7.500 does not exist in Column A the next higher value should be used. In this case 10.000 which belongs to Product 4.
I tried to go with the following formula in Cell D2 but instead of getting Product 4 I get #NV as a result.
=INDEX(A1:B4;MATCH(C1;A1:A4;-1);2)
The only solution I found so far was changing the values in Column A from ascending to descending. However, I would prefer to have a solution which does not require a change of the order in the list.
Do you have any idea how to solve this issue without changing the order in the list?
For unsorted data you can use below formula::
=INDEX(B1:B4,MATCH(SMALL($A$1:$A$4,COUNTIF($A$1:$A$4,"<"&C1)+1),A1:A4,0))
See image for reference

Getting the next lower value with VLOOKUP

I have the following Excel spreadsheet:
A B C D
1 15.000 Product 1 7.500 Product 2
2 5.000 Product 2
3 1.000 Product 3
4 0 Product 4
5
In Cell C1 I type a random number (in this case 7.500). Now I want that in Cell D1 the corresponding Product is shown to the value in Cell C1.
Since 7.500 does not exist in Column A the next lower value should be used.
In this case 5.000 which belongs to Product 2.
I tried to go with the following formulas in Cell D2 but instead of getting Product 2 I always get Product 4 as a result.
=VLOOKUP(F16,$A$1:$B$4,2,TRUE)
=VLOOKUP(F16,$A$1:$B$4,2)
Do you have any idea how to solve this issue?
For unsorted data you can use below formula:
=INDEX(B1:B4,MATCH(LARGE($A$1:$A$4,COUNTIF($A$1:$A$4,">"&C1)+1),A1:A4,0))
See image for reference
i will try it with addition columns, which indicates the difference between given 7.5 and col A like this: =IF($C$1-A1<0,999999,$C$1-A1) and then get the mininum value like =MIN(E1:E4). then get the according value in column B.
If the order is not guaranteed to be ascending then you can use an array formula (use ctrl+shift+enter instead of just enter to create an array formula)
=INDEX(B2:B5,MATCH((C2-MIN(IF((C2-A2:A5)<0,MAX(A2:A5),C2-A2:A5))),A2:A5,0))

To filter multiple columns with a condition on the results

I am trying to find a way of highlighting a result with multiple conditions. I have no knowledge of pivot tables. I would rather use a formula or macros. The table is organised by Dealer.
Acc NAME Add Dealer Total
68687 Sara 11 Wood 111A 0
68687 Sara 11 Wood 111A 0
32187 Sara 11 Wood 111A 0
12345 Tom 10 Main 7878C 2
12345 Tom 10 Main 7878C 2
54321 Tom 10 Main 7878C 2
My table is similar to the one above. I want to select where the Total is greater than 0 & for each Dealer each unique Account number with the lowest Account number highlighted somehow.
So the results I want for the table above would be: Dealer 7878C, Accounts 12345, 54321.
12345 being the lower of the two, it is highlighted.
I don't mind copying the results onto another sheet, as I don't want to remove any data from the sheet. I started by just filtering the Totals for >0 and I was thinking of trying to filter for unique values in Account but its the next step that I am stuck on. A countifs formula?
The sheet is quite large and I'm just not sure which is the best way to try and do it.
Thanks for any help.
There's a nice but complicated way to do it.
With your original data:
With changed data:
As you can see I've placed your data in A1:E7.
I use two array formulas, one for the Dealer in G2:G5 and one for the Accounts H2:N5. The Dealer formula is vertical, and the Accounts formula is horizontal.
For the dealers put this array formula in G2 (press Ctrl+Shift+Enter to enter it):
=IFERROR(INDEX($D$2:$D$7,SMALL(IF(($E$2:$E$7>0)*(COUNTIF($G$1:$G1,$D$2:$D$7)=0),ROW($D$2:$D$7)-1),ROW($G$1:$G1))),"")
Now copy G2 down to G3:G5 to get the rest of the relevant dealers.
For the accounts put this array formula in H2:
=IFERROR(SMALL(IF(($D$2:$D$7=$G2)*(COUNTIF($G2:G2,$A$2:$A$7)=0),$A$2:$A$7),1),"")
Now copy H2 to the right, I2:N2, and down to H3:N5.
To make the first accounts bold I simply make the H column formatted as Bold.
You can copy these formulas farther as needed. Note that the locations are important. If you want to place the formulas elsewhere you'll need to change the references accordingly.
Formulas explained
What these formulas do is check for your conditions, and then get the smallest value that hasn't been retrieved yet, in the upper / left most cells.
The two formulas are mostly the same, apart from the fact that in the account numbers we can use the actual numbers, and with the dealer we use the row number instead.
The dealer formula from the inside out:
The conditions are set in the IF part of the formula, with a multiplier * as a logical AND (TRUE*TRUE=TRUE FALSE*TRUE=FALSE).
The first condition in IF(($E$2:$E$7>0)*(COUNTIF($G$1:$G1,$D$2:$D$7)=0),... checks for the row's Total value to be greater than zero, the second condition checks that the dealer is not already present in the G column. The second condition is irrelevant in the first cell, but in the second cell G3 it becomes COUNTIF($G$1:$G2,... which returns more than 0 if the dealer already exists, and evaluate to FALSE.
If the conditions are met the IF returns the dealer's index by using its row minus 1 ROW($D$2:$D$7)-1, which returns 1 for the first etc. as the starting row is 2. Otherwise it returns FALSE which is ignored.
The SMALL function returns the k-th smallest item. It ignores the FALSE items, and in our case returns the k-th smallest index that meets the conditions (Total>0 and not already present in the results). SMALL(...,ROW($G$1:$G1) in the first cell return the first item. ROW($G$1:$G2) in the second cell G3 evaluates to 2 and returns the second smallest item, and so forth.
The INDEX function simply returns the dealer from the data according to the index.
And finally, the IFERROR is there only to hide the errors when the end of the results is reached.
based on your sample data and assuming a header row in row 1 and the left column being column A.
=COUNTIF($A$2:A2,A2)
place that in F2 and copy down. Then do a filter on the helper column =1

Resources