Excel formulae with three different conditons

%3CLINGO-SUB%20id%3D%22lingo-sub-2083436%22%20slang%3D%22en-US%22%3EExcel%20formulae%20with%20three%20different%20conditons%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2083436%22%20slang%3D%22en-US%22%3ECan%20someone%20assist%20in%20getting%20this%20formulae%20in%20one%20cell%3F%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2083436%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2083662%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formulae%20with%20three%20different%20conditons%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2083662%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F937900%22%20target%3D%22_blank%22%3E%40MickeyMouse%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%2C%20Mickey%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20going%20to%20give%20you%20more%20than%20you%20asked%20for.%20Probably%20quite%20a%20bit%20more.%20As%20it%20happens%2C%20I%20was%20working%20on%20a%20tax%20estimating%20spreadsheet%20for%20the%20US%20tax%20tables%20applying%20to%202020%20and%202021.%20I'm%20attaching%20the%20result.%20There%20are%20really%20only%20two%20distinct%20formulas%20on%20this%20(with%20variations%20to%20refer%20to%20the%20tables%20that%20apply%20to%20different%20filing%20statuses).%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20to%20do%20the%20kind%20of%20thing%20you%20are%20asking%2C%20although%20a%20deep%20IF%20or%20IFS%20function%20is%20possible%2C%20the%20tax%20calculations%20are%20done%20using%20the%20%3CSTRONG%3EXLOOKUP%3C%2FSTRONG%3E%20function%20and%20referring%20to%20%3CU%3Etax%20tables%3C%2FU%3E%20to%20get%20those%20different%20cut-off%20levels.%20This%20is%20far%20better%20than%20hard-coding%20the%20values%20in%20the%20formulas%2C%20because%20you%20can%20basically%20update%20the%20tables%20(which%20happens%20every%20year)%20and%20check%20and%20validate%20those%20static%20tables%20against%20the%20official%20IRS%20tables...and%20leave%20the%20formulas%20themselves%20unchanged.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2085729%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formulae%20with%20three%20different%20conditons%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2085729%22%20slang%3D%22en-US%22%3EI%20got%20the%20solution%20using%20IF.%20See%20below%20the%20attachment%20of%20the%20formulae.%3CBR%20%2F%3E%3CBR%20%2F%3ELete%20also%20try%20the%20Xlookup.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2085835%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formulae%20with%20three%20different%20conditons%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2085835%22%20slang%3D%22en-US%22%3EOne%20other%20option%2C%20but%20if%20it%20grows%20to%20more%20conditions%20I%20would%20definitely%20look%20at%20setting%20up%20a%20lookup%20table.%3CBR%20%2F%3E%3CBR%20%2F%3E%3DMEDIAN(0%2C8233%2CG2-24000)%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2087570%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20formulae%20with%20three%20different%20conditons%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2087570%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F425987%22%20target%3D%22_blank%22%3E%40mathetes%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20do%20not%20know%20whether%20this%20is%20of%20interest%20to%20you.%26nbsp%3B%20Basically%2C%20I%20have%20used%20your%20workbook%20as%20a%20test%20case%20for%20developing%20strategies%20for%20using%20the%20Lambda%20function%20TAXDUE.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3D%20%20LAMBDA(year%2Cstatus%2Ctaxable%2C%0A%20%20%20%20%20LET(%0A%20%20%20%20%20%20%20Tbl%2C%20TAXTABLE(year%2C%20status)%2C%0A%20%20%20%20%20%20%20Thresholds%2C%20INDEX(Tbl%2C%20%7B1%3B2%3B3%3B4%3B5%3B6%3B7%7D%2C%201)%2C%0A%20%20%20%20%20%20%20RowNum%2C%20MATCH(taxable%2C%20Thresholds%2C%201)%2C%0A%20%20%20%20%20%20%20threshold%2C%20INDEX(Thresholds%2C%20RowNum)%2C%0A%20%20%20%20%20%20%20accumulated%2C%20INDEX(Tbl%2C%20RowNum%2C%202)%2C%0A%20%20%20%20%20%20%20taxband%2C%20INDEX(Tbl%2C%20RowNum%2C%203)%2C%0A%20%20%20%20%20%20%20accumulated%20%2B%20taxband*(taxable-threshold)%0A%20%20%20)%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Ewhich%2C%20in%20turn%2C%20calls%20a%20Lambda%20TAXTABLE%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3D%20LAMBDA(Year%2CStatus%2C%0A%20%20%20%20LET(%0A%20%20%20%20%20%20StatusOptions%2C%20%7B%22Single%22%2C%22Married%20Separate%22%2C%22Married%20Joint%22%2C%22Surviving%20Spouse%22%2C%22Head%20of%20Household%22%7D%2C%0A%20%20%20%20%20%20TableNumber%2C%20QUOTIENT(%201%2BXMATCH(FileStatus%2C%20StatusOptions)%2C%202%20)%2C%0A%20%20%20%20%20%20Offset%2C%205*(TableNumber-1)%2C%0A%20%20%20%20%20%20YearTaxTable%2C%20SWITCH(Year%2C%202020%2C%20Tables2020%2C%202021%2C%20Tables2021%2C%20NA()%20)%2C%0A%20%20%20%20%20%20INDEX(YearTaxTable%2C%20%7B1%3B2%3B3%3B4%3B5%3B6%3B7%7D%2C%20%7B1%2C2%2C3%7D%2BOffset))%0A%20%20%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor
Can someone assist in getting this formulae in one cell?
7 Replies

@MickeyMouse 

 

 

Hi, Mickey

 

I'm going to give you more than you asked for. Probably quite a bit more. As it happens, I was working on a tax estimating spreadsheet for the US tax tables applying to 2020 and 2021. I'm attaching the result. There are really only two distinct formulas on this (with variations to refer to the tables that apply to different filing statuses).

 

But to do the kind of thing you are asking, although a deep IF or IFS function is possible, the tax calculations are done using the XLOOKUP function and referring to tax tables to get those different cut-off levels. This is far better than hard-coding the values in the formulas, because you can basically update the tables (which happens every year) and check and validate those static tables against the official IRS tables...and leave the formulas themselves unchanged.

I got the solution using IF. See below the attachment of the formulae.

Lete also try the Xlookup.
One other option, but if it grows to more conditions I would definitely look at setting up a lookup table.

=MEDIAN(0,8233,G2-24000)

@mathetes 

I do not know whether this is of interest to you.  Basically, I have used your workbook as a test case for developing strategies for using the Lambda function TAXDUE.

=  LAMBDA(year,status,taxable,
     LET(
       Tbl, TAXTABLE(year, status),
       Thresholds, INDEX(Tbl, {1;2;3;4;5;6;7}, 1),
       RowNum, MATCH(taxable, Thresholds, 1),
       threshold, INDEX(Thresholds, RowNum),
       accumulated, INDEX(Tbl, RowNum, 2),
       taxband, INDEX(Tbl, RowNum, 3),
       accumulated + taxband*(taxable-threshold)
   ) )

which, in turn, calls a Lambda TAXTABLE

= LAMBDA(Year,Status,
    LET(
      StatusOptions, {"Single","Married Separate","Married Joint","Surviving Spouse","Head of Household"},
      TableNumber, QUOTIENT( 1+XMATCH(FileStatus, StatusOptions), 2 ),
      Offset, 5*(TableNumber-1),
      YearTaxTable, SWITCH(Year, 2020, Tables2020, 2021, Tables2021, NA() ),
      INDEX(YearTaxTable, {1;2;3;4;5;6;7}, {1,2,3}+Offset))
   )

 

@Peter Bartholomew 

 

Thank you very much. Prior to this, I've not even looked at the LAMBDA information. I've seen it here, of course, but have simply assumed it was for the truly advanced.

 

On the other hand, I did employ the LET function in my own solution, and was feeling proud of THAT as a test case for the original poster. It was only my second time using LET.

 

Lambda, here I come, thanks to you, @Peter Bartholomew!

@mathetes 

It was the fact that it was a well-structured workbook and that you had used LET that temped me to try the LAMBDA to see what it offered.  I think the good thing about the LAMBDA concept is the way it breaks the problem into manageable parts.

1) Can I identify and return the correct table

2) Given the table, can I calculate the tax due

Most of the finance modellers I know let out a howl of agony if they think a solution falls short of being a perfect 'white box'.  In this case the solution can be validated simply by typing

= TAXTABLE(2021, FileStatus)

How it is achieved should not matter that much to the end user.  I have used a bottom-up strategy of building arrays of row and column indices but, equally, I could have returned the table by intersecting row ranges for the year and column ranges for the filing status.

 

I am pleased you found the use of LAMBDA interesting.

@MickeyMouse 

 

It's definitely possible to do that with IF. But you don't really want to; it's not a good long term solution for a variety of reasons. First, you're only dealing with two levels of the tax tables, so it's limited that way; second, the longer the nested set of IF conditions gets, the harder it is to follow them; IFS is an option, but it too is prey to the next reason not to approach  it this way: third, it's really not good practice to hard code numbers into formulas when those numbers can change, as tax related numbers WILL do;