IF logical based on multiple conditions

mba_110

Board Regular
Joined
Nov 28, 2012
Messages
80
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I am trying to execute some calculation for employees in query with IF but unable to reach the target.

My expression in query
Code:
 IIf([EOSB_Entitle]="Yes" And [ContractStatus]="Resigned",IIf([TotalDays]<=729,IIf([TotalDays]<=1824,[AccrualDays]/3,IIf([TotalDays]<=1825,[AccrualDays]/3*2,IIf([TotalDays]>3650,[AccrualDays])))),0)

IF
[EOSB_Entitle] equal to "Yes" and [ContractStatus]="Resigned"

than

Total days is less than 730 the result should show 0
Total days is greater than 730 but less than 1825 then result should be [Accrualdays]/3
Total days is greater than 1825 but less than 3650 then result should be [Accrualdays]/3*2
Total days is greater than 3650 then [Accrualdays]

otherwise

0

Any help here with above is much appreciated.
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I am not partial to Iif I think it is harder to read as code when trying to change or fix issues.

that said...will this work?

Code:
    If [EOSB_Entitle] = "Yes" And [ContractStatus] = "Resigned" Then
        If [TotalDays] <= 729 Then
            lngval = 0
        ElseIf [TotalDays] >= 730 And [TotalDays] <= 1824 Then
            lngval = Sum([Accuraldays] / 3)
        ElseIf [TotalDays] >= 1825 And [TotalDays] <= 3650 Then
            lngval = Sum([Accuraldays] / 3) * 2
        ElseIf [TotalDays] > 3650 Then
            lngval = [AccrualDays]
        End If
    Else
        lngval = 0
    End If

Here is the IIF formula, I think you were just missing a sum.

Code:
IIf [EOSB_Entitle] = "Yes" And [ContractStatus] = "Resigned", _
    IIf([Totaldays] <= 729, 0, _
    IIf([Totaldays] >= 730 And [Totaldays] <= 1824, [Accuraldays] / 3, _
    IIf([Totaldays] >= 1825 And [Totaldays] <= 3650, Sum([Accuraldays] / 3) * 2, _
    IIf([Totaldays] > 3650, [Accrualdays], 0))))
 
Last edited:
Upvote 0
IIF formula is not working and i dont know where i can enter your code in query ?
 
Upvote 0
It will work in a query. But it should be all on one line. Remove the _ (underscore) characters from the formula, and any unneeded spaces. If it is meant to be used a a column in the query it also needs a column alias/name.
 
Upvote 0
Oops, is missing a set of parentheses:
Code:
IIf [EOSB_Entitle] = "Yes" And [ContractStatus] = "Resigned", _
    IIf([Totaldays] <= 729, 0, _
    IIf([Totaldays] >= 730 And [Totaldays] <= 1824, [Accuraldays] / 3, _
    IIf([Totaldays] >= 1825 And [Totaldays] <= 3650, Sum([Accuraldays] / 3) * 2, _
    IIf([Totaldays] > 3650, [Accrualdays], 0))))

Should be:
Code:
IIf ([EOSB_Entitle] = "Yes" And [ContractStatus] = "Resigned", _
    IIf([Totaldays] <= 729, 0, _
    IIf([Totaldays] >= 730 And [Totaldays] <= 1824, [Accuraldays] / 3, _
    IIf([Totaldays] >= 1825 And [Totaldays] <= 3650, Sum([Accuraldays] / 3) * 2, _
    IIf([Totaldays] > 3650, [Accrualdays], 0)))))

One liner:
Code:
IIf([EOSB_Entitle] = "Yes" And [ContractStatus] = "Resigned", IIf([Totaldays] <= 729, 0, IIf([Totaldays] >= 730 And [Totaldays] <= 1824, [Accuraldays] / 3, IIf([Totaldays] >= 1825 And [Totaldays] <= 3650, Sum([Accuraldays] / 3) * 2, IIf([Totaldays] > 3650, [Accrualdays], 0)))))
 
Upvote 0
I don't actually see why you have a sum() in the formula at all. If it's not working post some sample data.
 
Upvote 0
Might also need some further logic ... for instance, if Totaldays is 731 and Accuraldays is 1825 then I'm not sure you would get any result here.
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,351
Members
451,697
Latest member
pedroDH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top