Count most frequent test based on criteria - excel-formula

I am trying to pull back the most frequent country based on a sector.
Here is a brief look at the data
Here is my code:
=INDEX(Transactions!$U$2:$U$318,MODE(IF(B5=Transactions!$S$2:$S$318,MATCH(Transactions!$U$2:$U$318,Transactions!$U$2:$U$318,0))))
Unfortunately, it appears that since there are multiple countries and multiple sectors in excel, the values pulled back in the Match don't line up with the values in the IF (i.e. IF={1,1,8,5}, MATCH={1,1,289,9})
Can anyone help out with this excel code?

Another solution would be: (Confirm with CTRL + SHIFT + ENTER)
=INDEX(B:B, 10000*MOD(MAX(COUNTIFS(A1:A7,C1,B1:B7,B1:B7)+(ROW(B1:B7)/10000)),1), 1)
The formula is based on the one from this site. I just changed COUNTIF to COUNTIFS to account for the criteria.

Related

Excel - Increment a number based on values from two other columns

I have 2 columns one is a period and another is a cycle. I need to create a 3rd column where I create a cycle identifier. Where the Letter changes on the cycle but resets every period.
I seem to have it with the following formula
IF(A1<>A2,1,IF(B1<>B2,C1+1,C1)). Which will give results of 1, 2 or 3. Then to get the numbers into letter form by using a switch SWITCH(C1,1,"A",2,"B",3,"C") in an adjacent cell. However I was curious if there is a more efficient or better way to accomplish this perhaps in all in one formula.
Any suggestions would be greatly appreciated.
Period & Cycle
Copy this formula to cell C2 and copy it down.
=IF(B2<>B1,IF(A2<>A1,CHAR(65),CHAR(CODE(C1)+1)),C1)
In Excel 365, you could use a spill formula like this:
=CHAR(B2:B15-XLOOKUP(A2:A15,A2:A15,B2:B15)+65)
You could argue that this is less efficient because it uses a lookup so there could be a speed hit with large amounts of data. On the other hand, it could be considered more efficient because it is a single formula and doesn't need to be pulled down.
If you were worried about the speed, you could set the binary search option in xlookup:
=CHAR(B2:B15-XLOOKUP(A2:A15,A2:A15,B2:B15,,2)+65)
(Column A has to be sorted ascending for this to work - I'm fairly sure that where there are duplicates this will still give the first match. However Microsoft are quoted as saying that there is only a slight benefit of using binary search according to this and other articles)
You could make the formula more dynamic:
=CHAR(B2:INDEX(B:B,COUNTA(B:B))-XLOOKUP(A2:INDEX(A:A,COUNTA(A:A)),A2:INDEX(A:A,COUNTA(A:A)),B2:INDEX(B:B,COUNTA(B:B)))+65)
Or using Let
=LET(Period,A2:INDEX(A:A,COUNTA(A:A)),
Cycle,B2:INDEX(B:B,COUNTA(B:B)),
CHAR(Cycle-XLOOKUP(Period,Period,Cycle)+65))

Issue converting SUMPRODUCT formula to INDEX+MATCH

I have a spreadsheet that shows the actual dollars produced and the expected dollars produced for each employee, where each row is one day and each employee has two columns. I would like to count the number of times an employee came within 10% of their production goal without referencing specific columns. Please look at the example spreadsheet.
I want for the formulas in cells Sheet2!E4:E7 to do what the formulas in Sheet2!E10:E13 do.
I am trying to avoid using direct column references because it causes me to have to go in and update the columns in the formulas every time a new employee is added to the sheet.
I'm no stranger to using INDEX+MATCH, or to converting SUMPRODUCT formulas to INDEX+MATCH. What I can't get past are the formulas in cells Sheet2!E4:E7. If you evaluate the formula you can watch the INDEX+MATCH section on the left side of the formula work correctly, and then a nearly identical INDEX+MATCH section on the right side evaluate to 0 for no apparent reason.
The formula I'm having trouble with is in cell Sheet2!E4:
COUNTIF(INDEX(Sheet1!$A$2:$AZ$314,,MATCH($B4,Sheet1!$A$1:$AZ$1,0)),">"&(INDEX(Sheet1!$A$2:$AZ$314,,MATCH($B4,Sheet1!$A$1:$AZ$1,0)+1)))
The section to the left of the ">" will match with the correct range (Sheet1!B2:B314), however the section to the right of the ">" will evaluate to 0 instead of Sheet1!C2:C314.
This is strange to me because the only real difference between the two sections is the '+1' on the end of the MATCH function, and adding the '+1' to the section to the left of the ">" produces the expected result for the MATCH function (Sheet1!C2:C314) as seen by evaluating the formula in cell Sheet2!E5.
The formula that produces the correct result is in cell Sheet2!E10:
SUMPRODUCT(--(ISNUMBER(Sheet1!$B$2:$B$313)),--(Sheet1!$B$2:$B$313<Sheet1!$C$2:$C$313),--(Sheet1!$B$2:$B$313>=Sheet1!$C$2:$C$313*0.9))
I understand that if I exclude the '$' before the column references, any future additions/subtractions to the columns on Sheet1 will adjust the references accordingly. This solution is not ideal, because there are multiple data sheets (one for each year) where the columns are all different (Dan is column F for 2019, G for 2018, M for 2017, etc.), and the tables using these sheets are laid out in a way that would prevent me from easily being able to auto-update the formulas, so a solution that locates the correct column using the employee's name is preferred.
The correct result should be 2 for Allen and 3 for Torres, but I can only seem to get the INDEX+MATCH formulas to return 0, 12, 15, 16, or 17 (depending on what tweaks I make to the formula).
Any help with this would be greatly appreciated.
You can try this: ARRAY FORMULA CTRL + SHIFT + ENTER
=SUM(IF(ISNUMBER(INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0))),INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0))<INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)+1):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0)+1)*(INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0))>=INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)+1):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0)+1)*0.9))*1)
and adapt the ranges to your needs. It is entered on the same sheet as the data.
Basically this approach uses ARRAY FORMULA CTRL + SHIFT + ENTER
INDEX($A$1:$E$30,2,MATCH(G9,$A$1:$E$1,0)):INDEX($A$1:$E$30,30,MATCH(G9,$A$1:$E$1,0)))
to get the needed ranges. MATCH looks for the right column. the row number can be set as needed, here its is from 2 to30 adapted to your provided data. My search term in this case is in G9 with Allen as content. The result of the formula is $B$2:$B$30. A +1 after Match gives the other range $C$2:$C$30. Both ranges can then be evaluated with the needed conditions
Pull it down and provide Torres in G10. Then the ranges will be adapted to $D$2:$D$30 and $E$2:$E$30.

Excel calculate SUM of differences when cell addresses are not fixed

In Excel I have a list of employees (see image) and I want to store their salary update history. For each salary update, I want to calculate the difference in their total salary. I'm unable to create a formula to calculate this and SUM() the updates per period for me.
The image below demonstrates my spreadsheet and the desired salary update total (I did calculations manually, bet need a formula for this). In reality, there will be a few dozens of employees, so the requirement for the formula is to be dynamic and not contain manual "+" for each row.
NOTE: For some people their salary can stay unchanged and there are not any values in some cells. It's the main challenge, since we have to find the value of the "previous salary" for an employee, because it's not necessary stored in the previous cell. Otherwise, I could have used a simple array formula, like:
{=SUM(G3:G6-H3:H6)}. But unfortunately, it does not work in this case (there can be no value in the H column, so we have to find where it actually is).
Also, the formula should work correctly with filtering: is some row is filtered out (person is inactive, see picture), this row/cell should be ignored.
Can anybody help me with this formula that I'm struggling with?
This will do what you ask, not my my best work.Column "I"is still =SUM. Remember to press Ctrl + shift + enter. Then drag the formula where you want it to apply.
{=IFERROR(IF(E3="",E3,E3-INDEX(F3:$I$3,MATCH(FALSE,ISBLANK(F3:$I$3),0)))
+IF(E4="",E4,E4-INDEX(F4:$I$4,MATCH(FALSE,ISBLANK(F4:$I$4),0)))
+IF(E5="",E5,E5-INDEX(F5:$I$5,MATCH(FALSE,ISBLANK(F5:$I$5),0)))
+IF(E6="",E6,E6-INDEX(F6:$I$6,MATCH(FALSE,ISBLANK(F6:$I$6),0)));SUM(E3:E6))}
This formula does give the same answers as yours on the test data as below, BUT it is making a very big assumption - that the last recorded salary is always the largest one.
=SUM(IF(D3:D6="",0,D3:D6-SUBTOTAL(4,OFFSET(E3,ROW(D3:D6)-ROW(D3),0,1,COLUMNS(E3:$J3)))))
Must be entered as an array formula using CtrlShiftEnter
If you want to ignore hidden rows, plz use the form of Subtotal that ignores hidden cells
=SUM(IF(D3:D6="",0,SUBTOTAL(104,OFFSET(D3,ROW(D3:D6)-ROW(D3),0,1,1))-SUBTOTAL(104,OFFSET(E3,ROW(D3:D6)-ROW(D3),0,1,COLUMNS(E3:$J3)))))

Excel Formula - Compare two tables (If match is found in name, calculate difference in charges.)

I am trying to figure out a formula to find and calculate the difference in charges from two tables which has matching facility names. If facility name does not have a match then leave blank or if NA in calculation cell.
Looking at your screenshot I'm assuming the first row after the header is on row 7. You can try to paste this to cell J7:
=IFNA(VLOOKUP(CONCATENATE(RIGHT(F7,2)," - ", G7),B:D,2,FALSE)-H7,"")
What it does is using vlookup function to get the data from the other table. But since they don't have the common value, I use CONCATENATE(RIGHT(F7,2)," - ", G7) to generate common value assuming column B has a unique value.
If I understand your question correctly, this is what you are trying to do:
This is the formula to enter from cell J6 and you can drag it to K6 as well:
=IFERROR(INDEX(C$6:C$12,MATCH("*"&$G6&"*",$B$6:$B$12,0))-H6,"")
Basically, the trick is to use wildcard * to perform this search. Try and let me know if this is what you are looking for.

Excel countif does not look up a different sheet

I am hoping someone can help with this Excel 2016 issue. I have done extensive searching on the net and believe I have found the formula to address my requirements. Before sharing the formula, let me explain my need.
I am creating a budget spreadsheet. There is currently two sheets in the spreadsheet. sheet1 contains data for categories and keywords. Column A contains different categories like Car Fule, Salary, Bank Fee, etc. Column B contains keywords to be search for within the transaction description. An example could be:
car fule | Mobile
Sheet2 contains all the transaction data. All the columns other then column c isn't relevant for this issue. An example of a transaction description could be: "EXP mobile Hobbitville 2221 MiddleEarth".
I am trying to create a formula that does a search in the defined cell for column c, against the keywords found in sheet` column b. Then once a match is found, perform a indirect function call to give the category of the transaction. I cannot get the initial search to work.
Formula that should work is:
=countif(c2,"*"&sheet1!$b$1:$b$85&"*")
This always gives a value of 0 saying it hasn't been found. I am using ctrl shift enter and ctrl enter to make the an array formula. If I press f9, I get all the values defined from sheet 1 as {0, 0, ...} (or something like that) which is what I expect. In that array, I should get at least one element as a '1' (true value). If I can fix this part of the formula or find a better method, then I can build the rest of the formula. Any one with ideas?
This is the COUNTIF syntax,
COUNTIF(range, criteria)
It should be the other way around,
=COUNTIF(C:C,"*"&Sheet1!$B1&"*")
Let me know if its not working

Resources