Numbers - Complex If And Then Statement Using Checkboxes In One Formula

So I have this conundrum I am dealing with. I am trying to get one formula to work properly in one field, so the information is displayed in only that one field. This formula I need help with involves multiple check boxes and prices. This seems simple, but it is more complex than it seems. I know I am very close, but yet still not quite there yet. Here is a link to my Google Drive of an example showing exactly what I am trying to do: Checkboxes & Prices Formula. The formula in question is in the fields filled with Yellow.


Here is the break down of conditions I am trying to fulfill in one formula (I believe I got all the conditions down):


If Checkbox 1 is checked, then display Price 1

Then if Checkbox 1 is NOT checked, but Checkbox 2 is checked, then display Price 2

Then if Checkbox 1 is NOT checked, & Checkbox 2 is NOT checked, but Checkbox 3 is checked, then display Price 3

Then if Checkbox 1 & Checkbox 2 is checked, then (Price 1 + Price 2) /2, displaying the average of Price 1 & Price 2

Then if Checkbox 1, 2, & 3 is checked, then (Price 1 + Price 2 + Price 3) /3, displaying the average of Price 1, Price 2, & Price 3

Then if Checkbox 1, 2, or 3 is NOT checked, then 0


As you can see, this can get VERY complex as more check boxes and prices are added. Is there any way to get this to work right, and possibly an easy way to add to the formula as more customers and prices are added?


I have come up with 3 different formulas, all of course don't work (2 don't work at all), but the first seems VERY close. Each uses a completely different way of looking/calculating this situation. With all three formulas, I only got as far as having two checkboxes and two prices. I have not factored in a 3rd check box or price just yet, since it's getting pretty complex, and I've been focusing on getting a solid formulas with 2 first.


I greatly appreciate anyone's and everyone's help. Thank you in advance!

iMac 27" 5K, macOS 10.14

Posted on Apr 24, 2019 5:17 PM

Reply
16 replies

Apr 25, 2019 11:06 PM in response to SGIII

Hi SG,


I note your screenshot doesn't include an example with none of the boxes checked. As written, I think your formula will return a division by zero error in that case, as mine did before I replaces zero with "" in the Aux table formula, and added the IF wrapper to the formula in column D of Table 1.


A quick fix to prevent your formul from throwing this error is shown below:


D2: (IF(A2,A$1,0)+IF(B2,B$1,0)+IF(C2,C$1,0))/MAX(1,COUNTIFS($A2:$C2,TRUE))


Regards,

Barry


Apr 24, 2019 9:51 PM in response to briangmetal

Rephrasing your description, I get:


I have three checkboxes.

Each is assigned a different 'price'

If one or more box is checked, I want to display th AVERAGE of the price(s) assigned to the checked boxes.

If no box is checked, display 0.00.


Here is a solution using an auxiliary table.


I'm on a slow internet connection, so screen shots will have to wait. Here are descriptions of the main table (Table 1) and the Auxilliary table (Aux).



Table 1:

Row 1 is a header row.

Column A is a Header column.

Columns B, C and D contain checkboxes.

Column E contains the formula below, entered in D2, and filled down to D9:

D2: IF(COUNTIF(B2:D2,TRUE)<1,0,AVERAGE(Aux::A2:C2))


COUNTIF counts the number of checked boxes in 'tis row' of columns B to D.

IF the count is less than one, the formula places a 0 in the cell, and the data format of the cell displays this as 0.00.

If the count is not less than 1 (at least 1 box is checked), the formula places the AVERAGE of the prices assigned to the checked boxes (which it gets from 'this row' of the Aux table) in the cell.


Aux

Row 1 is a Header row.

Rows 2 through 9 are body rows.

Cells A1, B1, and C1 contain the prices assigned to the three checkboxes in columns B, C and D of Table 1.

The example uses 10.00, 20.00 and 40.00 in these columns.

The rest of the cells (A2 - C9) contain the formula below, entered in A2, then filled right and down to C9.

A2: IF(Table 1::B2,A$1,"")

The formula inserts the price in row 1 of 'this column' is the associated checkbox is checked ( true )

If the associated box is NOT checked ( false ), the formula inserts a null string ( "" ), a text value, that is ignored by AVERAGE.


Here is a list showing which boxes are checked, and the result:

A:          10.00

B:          20.00

C:          40.00

AB:        15.00

AC:        25.00

BC:        30.00

ABC:     23.33

- - -:        0.00


Images when I get to a faster connection.


Regards,

Barry





Apr 25, 2019 12:01 PM in response to Barry

Thank you so much for your help to both Steve and Barry. Out of the two suggestions, I do like Barry's formula. It is very simple and to the point. It is exactly what I am after. It is very close to working properly, but there are some faults I need help fixing.


With all three checkboxes checked, it does calculate the average of the three prices perfectly. But then, if only two check boxes are checked, it should only take the average of those two prices (dividing by 2). Or if one checkbox is checked, it should only take the average of itself (dividing by 1). Instead what it's doing is it is always dividing by 3 regardless of what check box is checked. Here is an updated spreadsheet to show Barry's formula in action, but still needs a little work that I can't quite figure out: Barry's Formula.

Apr 25, 2019 12:37 PM in response to Barry

You know what Barry, I got it. I was able to figure it out. All now works perfectly. I had to set it up much better to match the way you had it set up.


This whole formula you came up with is very simple, but for some reason I couldn't quite wrap my head around it and I made it more complicating. I was in more of the C++ If Then statements thinking mode. Thank you again very much for all of both of you guys' help!

Apr 26, 2019 11:44 AM in response to SGIII

Hi SG,


Or perhaps it depends on what Brian specified in his original post :


If Checkbox 1 is checked, then display Price 1

Then if Checkbox 1 is NOT checked, but Checkbox 2 is checked, then display Price 2

Then if Checkbox 1 is NOT checked, & Checkbox 2 is NOT checked, but Checkbox 3 is checked, then display Price 3

Then if Checkbox 1 & Checkbox 2 is checked, then (Price 1 + Price 2) /2, displaying the average of Price 1 & Price 2

Then if Checkbox 1, 2, & 3 is checked, then (Price 1 + Price 2 + Price 3) /3, displaying the average of Price 1, Price 2, & Price 3

Then if Checkbox 1, 2, or 3 is NOT checked, then 0


Regards,

Barry


May 2, 2019 5:26 PM in response to Barry

Hello to all of you guys yet again. I went ahead and replied to this thread still instead of creating a new one since this question is related to the initial formula. I am back at working on this project I have with Numbers, and have created a very lengthy and somewhat complex spreadsheet. Barry's solution has worked perfectly, and it is what I am using to tackle the creation of this spreadsheet. I however ran into another small snag that I just get figured out, and hoped you guys could help me once again.


I have created a simple spreadsheet that shows my dilemma, which I have uploaded on my Google Drive. It can be downloaded here: Customers' Average Prices. The includes another table below which is where the prices are pulled from.


The most simple I can explain with what I am trying to accomplish is: when any one of the customers is not checked, the Customer and Product field will be "Blank", and the "Average" field, and the "Average Of All" field calculates properly.


I have come up with 2 examples. They are both exactly the same except each formula where Customer and Product meet, will display a "blank" in "Example 1" and a "0" in "Example 2" if its respectable checkbox is not checked. Example 2 is the closest to working properly, except if any checkbox is not checked, it takes the average of all three averages even if values are "0". And also it displays a "0" instead of a "Blank" when there is no value where Customer and Product meet. The areas in which is filled in yellow, are the formulas that are having the issue. I believe it's really only the "Average" formula that is at fault and needs a tweak. But, who knows, it could be something even further.


Best Regards,


Brian Green

May 2, 2019 6:02 PM in response to stevehok

Also one trick I use the give a blank cell instead of a “0” is to add a conditional format that if the value is zero make the text white or in this case yellow. The value is still there but hidden.

If the cells are blank the average function in the total will error. You can also add an off error function to correct that to “0”


IFERROR(COUNTIF(D19:F19,">0")÷SUM(D19:F19),0)

May 9, 2019 2:48 PM in response to stevehok

Steve,


Thank you very much for your suggestion/input. I went ahead and tried you suggestion, but came to (what I hope) is one last problem. Here is the updated Numbers file uploaded on my Google Drive: Customers' Average Prices. Customer 2 and Customer 3 happens to not have a price assigned for a product (because they don't offer that product for sale. If there is a case where only the Customer 2 checkbox is checked OR only the Customer 3 checkbox is checked, there is an error in the "Average" column.


The error basically is when only one of those customers is checked, it can't find the average of zero ("Numbers can't be divided as zero"). Again, I can't quite figure out how the condition should be written in the formula to keep it from throwing that error. With either only Customer 2 or Customer 3 checkboxes checked, instead of the error, the answer should just be zero. Everything else with the formula is working perfectly.

May 9, 2019 8:18 PM in response to briangmetal

Hey Brian,


i took a look look at the document. The error is caused when all values equal 0. The remedy this the easiest way is to wrap the whole function in an iferror function like this:

(this is the Customer 3 function)


IFERROR(IF(COUNTIF(C3:C5,TRUE)<1,0,SUM(D9:F9)÷COUNTIF(D9:F9,">0")),0)


the “,0)” at the end would represent what you want the value to be if the function runs into an error. I don’t recommend using this too extensively but in this situation it would be the simplest solution.


You could do do this for each cell calculating an avg.


Hope this helps.


Steve

Apr 24, 2019 6:56 PM in response to briangmetal

I think you were over complicating it a bit. I was able to get it working using 2 different tables and filters to show or hide the prices I want.

I used an IF,AND Function to set each combination of checkboxes and set it to place either “show” or “hide” in column B of table 2.

Then I set a filter to only show the row that had “show”

I set the price values as $5, $10 and $20. I then set functions to get the avg of price 1 and 2 then price 2 and 3 then 1,2 and 3.


Here is the function for checkbox 3 or Table 1 C2:

IF(AND(Table 1::$A$3,Table 1::$B$3,Table 1::$C$3=FALSE),"show","hide")


Here are a few screen shots: (sorry about the size)

Apr 25, 2019 10:12 PM in response to briangmetal

If you don't want to set up an extra table, you can easily do this with just one formula, like this:



The formula in D2, filled down:


=(IF(A2,A$1,0)+IF(B2,B$1,0)+IF(C2,C$1,0))/COUNTIFS($A2:$C2,TRUE)


All this does is add the value if its corresponding box is checked, otherwise put zero, and then divide the sum by the number of boxes that were checked.


SG

May 2, 2019 5:53 PM in response to briangmetal

Hey Brian,


If I understand what you are having an issue with is the average of only the prices that are showing rather than all 3 if one is blank.


This is solved it in example 2.


SUM(G19:G21)÷COUNTIF(G19:G21,">1")


rather than using the average function that uses the same array each time I tested to see how many numbers were greater than zero first then divided by that number instead.


Hope this helps.


Steve

May 9, 2019 8:01 PM in response to briangmetal

Responding to your original post, not your latest with the additional question (because I did not see it before posting)...


Starting with Barry's rephrasing of your problem and his description of the table with the checkboxes and his inclusion of an AUX table to conatin the prices, a formula for cell Table 1::E2 would be:


=IFERROR(AVERAGEIF(B2:D2,TRUE,AUX::A2:C2),0)


While not recommended, if you would rather have the prices in the formula (not in a table), the formula would be like this one (the prices in this formula are 10, 20, and 30):


=IFERROR(AVERAGEIF(B2:D2,TRUE,{10,20,30}),0)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Numbers - Complex If And Then Statement Using Checkboxes In One Formula

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.