Hi,
Hoping someone can help or point me in the right direction!
I have a complex formula which essentially takes 7 input values and performs a calculation.
The goal of the formula is to calcuclate full or partial salaries for a given month based on multiple criteria. I would like to provide the critera as variables, so far I've named the variables:
As the number of Employees will change I would like to make it dynamic to spill down for all employees and across for all dates in the current FY but I don't understand how to pass multiple ranges as named variables and have the formula process row by row.
A Simplified version of the source data looks like, with expected output in AA:AC:
Cell X11 shows the full expanded formula but includes the function CompForecast
Cell Y11 contains the condensed formula
the formulas for the named lambda functions are:
Hoping someone can help or point me in the right direction!
I have a complex formula which essentially takes 7 input values and performs a calculation.
The goal of the formula is to calcuclate full or partial salaries for a given month based on multiple criteria. I would like to provide the critera as variables, so far I've named the variables:
- EmpID - the Employee ID from Column C is passed to the CompForecast lambda function to Filter the Compensation table and determine the Comp amount for the given month
- cMnth - is the date of the current month
- StartDate - the Employee Starting date Column I is used to determine when the Employee started and determines Comp for the days
- EndDate - Column J same concept as StartDate, but calculates when the Employment ended
- Leave - indicates employee had a type of unpaid leave which should be excluded
- The LeaveStart and LeaveEnd dates work similar to Start/EndDates as above and determine the Comp for the period
As the number of Employees will change I would like to make it dynamic to spill down for all employees and across for all dates in the current FY but I don't understand how to pass multiple ranges as named variables and have the formula process row by row.
A Simplified version of the source data looks like, with expected output in AA:AC:
Cell X11 shows the full expanded formula but includes the function CompForecast
Cell Y11 contains the condensed formula
Dynamic Query.xlsx | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | AA | AB | AC | |||
3 | These are the correct values - I would like to make the formula dynamic to spill down and if possible also across | |||||||||||||||||||||||||||||
4 | ||||||||||||||||||||||||||||||
5 | Names/IDs vary over time - data is imported via PowerQuery | 01/05/2023 | ||||||||||||||||||||||||||||
6 | ||||||||||||||||||||||||||||||
7 | Variables: | EmpID | StartDate | EndDate | Leave | LeaveStart | LeaveEnd | cMnth | ||||||||||||||||||||||
8 | ||||||||||||||||||||||||||||||
9 | Employee Name | Employee No. | Yet to Start | Dept | Title | Activity | Start Date | End Date | LoS | Hours | P status | P Start | P End | Emp Cont | Co cont | Leave type | Leave Start | Leave End | Comp Date | Ann Salary | Full Monthly formula | Condensed Monthly formula | 31-May-23 | 30-Jun-23 | 31-Jul-23 | |||||
10 | ||||||||||||||||||||||||||||||
11 | Joe Bloggs | 103 | 07-Oct-19 | - | SAB | 05-May-23 | 15-Jul-23 | 01-Jan-23 | 29000 | 311.84 | 311.84 | 311.84 | 0.01 | 1,247.32 | ||||||||||||||||
12 | John Doe | 100 | 30-Mar-20 | - | UPL | 10-Jul-23 | 31-Aug-23 | 01-Jan-23 | 31000 | 2,583.33 | 2,583.33 | 750.01 | ||||||||||||||||||
13 | Jane Smith | 101 | 29-Aug-17 | 15-Jul-23 | - | - | 01-Jul-23 | 46000 | 3,333.33 | 3,333.33 | 1,916.67 | |||||||||||||||||||
14 | Richard Roe | 102 | Yet to start | 01-Jun-23 | - | - | - | 01-Jan-23 | 40000 | 0.00 | 3,333.33 | 3,333.33 | ||||||||||||||||||
Formula Help |
Cell Formulas | ||
---|---|---|
Range | Formula | |
X11 | X11 | =LET(EmpID, $C11,Empstart,$I11, EmpEnd, $J11,Leave,$R11,LeaveStart,$S11,LeaveEnd,$T11,cMnth, AA$9, Salary, CompForecast(EmpID,cMnth), x, IF(Leave<>"", IF(AND(EOMONTH(LeaveStart,0)=cMnth,EOMONTH(LeaveEnd,0)=cMnth), Salary * SUM(DAY(LeaveStart)-1,DAY(cMnth)-DAY(LeaveEnd))/DAY(cMnth)+0.01, IF(AND(EOMONTH(LeaveStart,0)<=cMnth,EOMONTH(LeaveEnd,0)>=cMnth), IF(EOMONTH(LeaveStart, 0) = EOMONTH(cMnth, 0), Salary * (DAY(LeaveStart) - 1) / DAY(cMnth) + 0.01, IF(EOMONTH(LeaveEnd, 0) = cMnth, Salary * (DAY(cMnth) - DAY(LeaveEnd)) / DAY(cMnth) + 0.01, 0.01) ), IF(AND(EOMONTH(Empstart,0)=cMnth, EOMONTH(EmpEnd,0)=cMnth), Salary * (DAY(EmpEnd)-(DAY(Empstart)-1))/DAY(cMnth), IF(EOMONTH(Empstart,0)=cMnth, Salary * ((DAY(Empstart)-1-DAY(EOMONTH(Empstart,0)))/-DAY(EOMONTH(Empstart,0))), IF(EOMONTH(EmpEnd,0)=cMnth, Salary * (DAY(EmpEnd)/(cMnth-(EOMONTH(cMnth,-1)+1))), IF(AND(Empstart<cMnth,OR(EmpEnd=0,EOMONTH(EmpEnd,0)>=cMnth)), Salary, 0) ) ) ) ) ), IF(AND(EOMONTH(Empstart,0)=cMnth, EOMONTH(EmpEnd,0)=cMnth), Salary * (DAY(EmpEnd)-(DAY(Empstart)-1))/DAY(cMnth), IF(EOMONTH(Empstart,0)=cMnth, Salary * ((DAY(Empstart)-1-DAY(EOMONTH(Empstart,0)))/-DAY(EOMONTH(Empstart,0))), IF(EOMONTH(EmpEnd,0)=cMnth, Salary * (DAY(EmpEnd)/(cMnth-(EOMONTH(cMnth,-1)+1))), IF(AND(Empstart<cMnth,OR(EmpEnd=0,EOMONTH(EmpEnd,0)>=cMnth)), Salary, 0) ) ) ) ),x) |
Y11 | Y11 | =LET(EmpID, $C11,Empstart,$I11, EmpEnd, $J11,Leave,$R11,LeaveStart,$S11,LeaveEnd,$T11,cMnth, AA$9, Salary,CompForecast(EmpID,cMnth), Output, IF(Leave<>"", IF(AND(EOMONTH(LeaveStart,0)=cMnth,EOMONTH(LeaveEnd,0)=cMnth), Salary * SUM(DAY(LeaveStart)-1,DAY(cMnth)-DAY(LeaveEnd))/DAY(cMnth)+0.01, IF(AND(EOMONTH(LeaveStart,0)<=cMnth,EOMONTH(LeaveEnd,0)>=cMnth), LeaveCalculator(LeaveStart,LeaveEnd,cMnth,Salary), MonthlySalary(Empstart,EmpEnd,cMnth,Salary) ) ), MonthlySalary(Empstart,EmpEnd,cMnth,Salary) ),Output) |
AA11:AC11,AA12:AB14 | AA11 | =LET(EmpID, $C11,Empstart,$I11, EmpEnd, $J11,Leave,$R11,LeaveStart,$S11,LeaveEnd,$T11,cMnth, AA$9, Salary,CompForecast(EmpID,cMnth), Output, IF(Leave<>"", IF(AND(EOMONTH(LeaveStart,0)=cMnth,EOMONTH(LeaveEnd,0)=cMnth), Salary * SUM(DAY(LeaveStart)-1,DAY(cMnth)-DAY(LeaveEnd))/DAY(cMnth)+0.01, IF(AND(EOMONTH(LeaveStart,0)<=cMnth,EOMONTH(LeaveEnd,0)>=cMnth), LeaveCalculator(LeaveStart,LeaveEnd,cMnth,Salary), MonthlySalary(Empstart,EmpEnd,cMnth,Salary) ) ), MonthlySalary(Empstart,EmpEnd,cMnth,Salary) ),Output) |
AC12:AC14 | AC12 | =LET(EmpID, $C12,Empstart,$I12, EmpEnd, $J12,Leave,$R12,LeaveStart,$S12,LeaveEnd,$T12,cMnth, AC$9, Salary, CompForecast(EmpID,cMnth), x, IF(Leave<>"", IF(AND(EOMONTH(LeaveStart,0)=cMnth,EOMONTH(LeaveEnd,0)=cMnth), Salary * SUM(DAY(LeaveStart)-1,DAY(cMnth)-DAY(LeaveEnd))/DAY(cMnth)+0.01, IF(AND(EOMONTH(LeaveStart,0)<=cMnth,EOMONTH(LeaveEnd,0)>=cMnth), IF(EOMONTH(LeaveStart, 0) = EOMONTH(cMnth, 0), Salary * (DAY(LeaveStart) - 1) / DAY(cMnth) + 0.01, IF(EOMONTH(LeaveEnd, 0) = cMnth, Salary * (DAY(cMnth) - DAY(LeaveEnd)) / DAY(cMnth) + 0.01, 0.01) ), IF(AND(EOMONTH(Empstart,0)=cMnth, EOMONTH(EmpEnd,0)=cMnth), Salary * (DAY(EmpEnd)-(DAY(Empstart)-1))/DAY(cMnth), IF(EOMONTH(Empstart,0)=cMnth, Salary * ((DAY(Empstart)-1-DAY(EOMONTH(Empstart,0)))/-DAY(EOMONTH(Empstart,0))), IF(EOMONTH(EmpEnd,0)=cMnth, Salary * (DAY(EmpEnd)/(cMnth-(EOMONTH(cMnth,-1)+1))), IF(AND(Empstart<cMnth,OR(EmpEnd=0,EOMONTH(EmpEnd,0)>=cMnth)), Salary, 0) ) ) ) ) ), IF(AND(EOMONTH(Empstart,0)=cMnth, EOMONTH(EmpEnd,0)=cMnth), Salary * (DAY(EmpEnd)-(DAY(Empstart)-1))/DAY(cMnth), IF(EOMONTH(Empstart,0)=cMnth, Salary * ((DAY(Empstart)-1-DAY(EOMONTH(Empstart,0)))/-DAY(EOMONTH(Empstart,0))), IF(EOMONTH(EmpEnd,0)=cMnth, Salary * (DAY(EmpEnd)/(cMnth-(EOMONTH(cMnth,-1)+1))), IF(AND(Empstart<cMnth,OR(EmpEnd=0,EOMONTH(EmpEnd,0)>=cMnth)), Salary, 0) ) ) ) ),x) |
the formulas for the named lambda functions are:
Dynamic Query.xlsx | ||||
---|---|---|---|---|
B | C | |||
19 | Lambda Named Functions | |||
20 | CompForecast | =LAMBDA(EmpID,cMnth, FILTER(Comp[Rate],(Comp[ID]=EmpID)*(Comp[Date]=MAXIFS(Comp[Date],Comp[ID],EmpID,Comp[Date],"<="&cMnth)),0)/12) | ||
21 | ||||
22 | LeaveCalculator | =LAMBDA(LeaveStart,LeaveEnd,cMnth,Salary,IF(EOMONTH(LeaveStart, 0) = EOMONTH(cMnth, 0), Salary * (DAY(LeaveStart) - 1) / DAY(cMnth) + 0.01, IF(EOMONTH(LeaveEnd, 0) = cMnth, Salary * (DAY(cMnth) - DAY(LeaveEnd)) / DAY(cMnth) + 0.01, 0.01))) | ||
23 | ||||
24 | MonthlySalary | =LAMBDA(EmpStart,EmpEnd,cMnth,Salary,IF(AND(EOMONTH(EmpStart, 0) = cMnth, EOMONTH(EmpEnd, 0) = cMnth), Salary * (DAY(EmpEnd) - (DAY(EmpStart) - 1)) / DAY(cMnth), IF(EOMONTH(EmpStart, 0) = cMnth, Salary * ((DAY(EmpStart) - 1 - DAY(EOMONTH(EmpStart, 0))) / -DAY(EOMONTH(EmpStart, 0))), IF(EOMONTH(EmpEnd, 0) = cMnth, Salary * (DAY(EmpEnd) / (cMnth - (EOMONTH(cMnth, -1) + 1))), IF(AND(EmpStart < cMnth, OR(EmpEnd = 0, EOMONTH(EmpEnd, 0) >= cMnth)), Salary, 0))))) | ||
Formula Help |