Forum Discussion
Help with Formula
=IF(OR(RIGHT(B2,4)="1101",C2="Salary"),INDEX($B$2:$B$9,MATCH(1,($A$2:$A$9=A2)*($C$2:$C$9="Salary"),0)),B2)
You can try this formula. Enter the formula with ctrl+shift+enter if you don't work with Office 365 or Excel for the web or Excel 2021.
Thank you so much for your help with this.
I have used this formula and it looked like it had worked. I copied the formula down to include all the rows (2170) and it seemed to stop working further down the rows. I copied out a sample for one person and the results of the formula:
The column R shows the results when the data was included in the full listing. The Column U is when I tried applying the formula to the data independently in this cut of it.
Here's the formula for the first row above used in the main data:
=IF(OR(RIGHT(C106,4)="1101",G106="Salary"),INDEX($C$1:$C$2170,MATCH(1,($B$1:$B$2170=B106)*($G$1:$G$2170="Salary"),0)),C106)
Here's the formula used in Column U above:
=IF(OR(RIGHT(C1,4)="1101",G1="Salary"),INDEX($C$1:$C$9,MATCH(1,($B$1:$B$9=B1)*($G$1:$G$9="Salary"),0)),C1)
I'm not sure why there's a difference.
- djclementsNov 25, 2023Silver Contributor
Peter_Browning The MATCH function will return the first match found where the criteria evaluates to TRUE. Since the results you're getting for a particular employee are not what you expected, it means the criteria is not unique to that employee. The most likely explanation, based on the data you've shared, is the same reference number is being used for more than one employee. Try using additional criteria to return the first match where the reference number, first name and last name are equal to the values in the current row. For example, the formula you used in row 106 can be re-written as follows:
=IF(RIGHT(C106, 4)="1101", INDEX($C$1:$C$2170, MATCH(1, ($B$1:$B$2170=B106)*($D$1:$D$2170=D106)*($E$1:$E$2170=E106)*($G$1:$G$2170="Salary"), 0)), C106)Or, with the XLOOKUP function (requires Excel 2021 or later):
=IF(RIGHT(C106, 4)="1101", XLOOKUP(1, ($B$1:$B$2170=B106)*($D$1:$D$2170=D106)*($E$1:$E$2170=E106)*($G$1:$G$2170="Salary"), $C$1:$C$2170, C106), C106) - OliverScheurichNov 24, 2023Gold Contributor
You are welcome. The formula in column R refers to 2170 rows and the formula in column U refers to 9 rows. The screenshot shows 9 rows therefore it's impossible to tell why there are different results. I could only guess why the results are different. Can you attach your file without sensitive data so we can have a look at this?
- Peter_BrowningNov 29, 2023Copper Contributor
I am sorry for the delay in replying. Things have been a bit crazy here just at the moment.
I am attaching the data anonymised as requested. I have also found out that the original logic I gave isn't quite correct. Originally I said I wanted to show the code for each employee ending in "1101" to match that of the record for that employee where the Type is "Salary". I now learn that an employee can have either a "Salary" or a "Basic" so either of these we need to look for. It is also possible that an employee could have two "basic" Types, but I am informed in these cases both Basic types will have the same Code.
I hope that makes sense. Seeing the actual data should help.
- OliverScheurichNov 29, 2023Gold Contributor
In the Test Payroll file there isn't a column with unique Reference numbers for each employee. @djclements has explained the issues of this scenario.
=COUNTIF($G$1:G1,$G$1)
I've used this formula to return a unique Reference number in column A for each employee. I simply returns the count of "Employers" from column G up to each row.
Formula in cell R1:
=IF(OR(RIGHT(C1,4)="1101",G1="Salary",G1="Basic"),INDEX($C$1:$C$2169,MATCH(1,($A$1:$A$2169=A1)*(($G$1:$G$2169="Salary")+($G$1:$G$2169="Basic")),0)),C1)
Then this formula returns the Code for Salary or Basic for each Reference if the Type is either Salary or Basic or the Code ends in 1101. As you can see in column R of the sample file the formula returns #NA in e.g. cell R20 since there isn't a Code for Salary or Basic for Reference 4. Reference 4 is the result of the COUNTIF in cell A20 for "Employers" from cell G20.
Formula in cell S1:
=IFERROR(IF(OR(RIGHT(C1,4)="1101",G1="Salary",G1="Basic"),INDEX($C$1:$C$2169,MATCH(1,($A$1:$A$2169=A1)*(($G$1:$G$2169="Salary")+($G$1:$G$2169="Basic")),0)),C1),C1)
You can wrap the formula into IFERROR in order to return the Code of the unique employee if the formula in column R returns #NA. If the formula in column S works as intended the formula from column R isn't needed anymore. Actually i added the formula from column R in order to discuss the potential #NA issue.
- djclementsNov 24, 2023Silver Contributor
OliverScheurich It's most likely because the reference number is not unique by employee, but rather represents the pay period cutoff date, or something like that, where it will be the same reference number for all employees for the pay period. As you've stated, this is just a guess, but I'd be willing to bet on it, lol. Additional criteria for the first and last name columns is likely needed to find the unique "Salary" record for each employee...