I'm very new to Access.
I'd like to make a column which has two conditions to result in "1" or "0".
The two conditions are:
If the session ID is smaller than 154 AND their level average is
bigger than the current level, "1" is assigned, otherwise, "0".
AND
If the session ID is equal to or bigger than 154 AND the exam average
is more than 2, "1" is assigned, otherwise, "0".
The syntax that I wrote is below, but it didn't work - returns 0 for every cell.
IIf([Session_ID]<154 And [Level_Avg]>[Current_Lvl],"1","0") And IIf([Session_ID]>=154 And [Exam_Avg]>=2,"1","0")
Could anyone take a look? Any comments are very appreciated. Thank you!
Most likely you need an OR operator between the two conditions as either one should return the 1 option, otherwise return 0.
IIf(([Session_ID]<154 And [Level_Avg]>[Current_Lvl]) Or ([Session_ID]>=154 And [Exam_Avg]>=2), 1, 0)
Related
I'm with a non-profit. We divide the state up into 16 regions for administrative purposes. I'm trying to find out how many members live in each region. We have 4 different types of members too. There's a table that lists all the members. There is also a table in which we have listed all the towns in the state and put in which of our regions the town is in. For the first member type the record has the value "1" in the TYPE field. I'm trying to write an expression that basically says this. If the TYPE field has a "1" in it, then the condition is true, return 1. Else return 0. Then sum up all the ones.
I didn't build the database or the query. This is the expression that was already there: INDIVIDUAL: Sum(IIf([Membership.TYPE]=1,1,0)) The sum that it comes up with just doesn't make sense.
Edit: I hope I did this right. Here is a table showing what information the query returns except for the 502 and the 96 which I put there as a check. The values in that one column add up to 502 but they should add up to 674. The 96 should be 113.
enter image description here
I need to get the first value within a large data-set, based on a row group where the condition holds true.
I.e: Get the First Value Where Deal Name is "ABC" and Type = "main" within a row group (scope) of a entire dataset.
I tried the following:
=IIF(Fields!DealName.Value="ABC" AND Fields!Type.Value="Main", First(Fields!DealValue.Value, "Deal"), NOTHING)
There are 3 records with distinct values for Deal Values.
In this scenario, it is picking up 0, when it should have picked up 4946.
Can I have the entire if statement in a scope?
Help would be immensely appreciated.
If I understand it right, you want to get first value based on if condition. Then you can use LookUp
Lookup(1, IIF(Fields!DealName.Value="ABC" AND Fields!Type.Value="Main", 1, NOTHING), Fields!DealValue.Value, "DateSetName")
I am trying to write an Access query (Access 2013) that uses an IIF statement to first check the value of an Option Frame and then, if the value is "1", use a date range or, if the value is anything else, find all records (only values "1" and "2" are available). My query criteria is...
Like IIf([Forms]![MaintenanceDueList]![OptionFrame]=1,>[Forms]![MaintenanceDueList]![FromDateText] And <[Forms]![MaintenanceDueList]![ToDateText],"*")
When my Option Frame Value is set to "2" I get all records returned as expected, but when my Option Frame value is set to "1" I get no results at all, despite there being plenty of records available with dates between the two dates provided in the FromDateText and ToDateText TextBox fields.
This one really has ne stumped, so any help would be gratefully received. Thanks, Mort640.
IIF returns a value but you seem to be trying to return some SQL logic - the true/false parts are evaluated then returned.
You can instead:
WHERE
[Forms]![MaintenanceDueList]![OptionFrame] = 2
OR
XXX > [Forms]![MaintenanceDueList]![FromDateText] And XXX < [Forms]![MaintenanceDueList]![ToDateText]
or to be inclusive of XXX as opposed to greater-than
WHERE
[Forms]![MaintenanceDueList]![OptionFrame] = 2
OR
XXX BETWEEN [Forms]![MaintenanceDueList]![FromDateText] And [Forms]![MaintenanceDueList]![ToDateText]
This should be easy, but I am stuck.
I have a table listing some figures about Qualifications - to achieve which a dataset that is essentially a row per Student is being grouped on Qualification with a Parent Grouping on "Measure" (which is just a bucket of qualifications).
One of the columns is trying to work out the number of students (well, more properly the number of students with a value in a particular field, weighted by another field) in each Measure/Qualification. In the screenshot below, it's the "Pred. Avg" column on the right hand side.
So for the Qualification Row Grouping, that column is calculated by:
=CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual") * Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP")
This works fine - the values of 35 and 11.5 in that rightmost column are correct for those rows. What the top row should be doing is simply adding up the values in the other rows to give me the number of students in this Measure, in this case to give 46.5. To do that the expression I am using is:
=Sum(CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual") * Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP"), "Measure")
However as you can see in the screenshot, this returns 2917 instead.
So my question is; Why doesn't that work, and given that it doesn't work how can I, within a parent group, aggregate the results of aggregates inside a child group?
EDIT:
OK so, I have determined that the following works correctly:
=Sum(CountDistinct(Iif(IsNothing(Fields!AVG_PTS.Value) = False, Fields!Learner_ID.Value, Nothing), "Qual"), "Measure")
The problem there is that the Qual row that returns 11.5 is weighted to 0.5. I.E. it actually returns 23, and the Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP") is for that row returning 0.5 and altering it to 11.5...so the question becomes; "how do I force that ...*Lookup(Fields!Qual_Code.Value, Fields!Qual_Code.Value, Fields!size.Value, "DS_KS5Nationals_LKP") into the "Qual" scope, like the CountDistinct() is already in?
The issue here is that you're trying to aggregate values using that Lookup function which only returns one value. There are a couple ways you could go about doing this. One option would be to use the LookupSet function to get the applicable weightings. An even better option is to combine the data in your dataset so that the weighting is available without using a lookup. That way the function can recalculate an any grouping level without you having to force a scope on it. Also, CountDistinct ignores "Nothing" so you can do without the extra IIf statement. Hope that helps.
Currently I am trying to average a set of numbers, but with certain conditions.
Is it possible to use an iif() within an avg() and return the correct result?
Furthermore, as of now my computations return a decimal returned to a power (8.9267....E -05).
I tried circumventing the AVG function by conditionally summing and then dividing by a conditional count but it gives me the same results.
Can someone explain why this is returned and offer help?
Currently I have:
=avg(iif((This_case) AND (That_case) AND (This_conditional)
, Fields!ResponseRate.Value
, 0))
Essentially I want the average ResponseRate if certain conditions are met.
The sum function works fine for the conditions but the average doesn't.
You can definitely use IIf within Avg and get the correct results.
Do you want to exclude the False values from the calculation entirely?
In your example you're still including them, just setting them to 0 and hence still including them in the calculation. This might explain your unexpected results.
If you want to exclude them entirely use Nothing instead of 0.
Edit after comment
You can nest an expression in another IIf statement and check for NULL values using IsNothing.
Say your condition average expression is:
=Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing))
You can return 0 for NULL values with something like:
=IIf(IsNothing(Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
, 0.0
, Avg(IIf(Fields!ID.Value > 5, Fields!value.Value, Nothing)))
I'd like to add my two cents to this one, a little late, but also valuable. I was able to use the above code to Average out a data set if another record appears X number of times.
=AVG(IIF(Count(Fields!AcctNo.Value, "AcctNo1") = 2, Fields!Limit.Value, Nothing))
So, if the acctno field appears 1 time, avg the limit field for that row group. Ian's example was very helpful