How to fill cells dynamically based on the value of a cell? How to segregate values based on a particular cell? - excel

Suppose, I have secured Marks for a particular subject which have 5 course Outcomes (each containing 2Marks--Total 10 marks for each subject).
I have the total marks obtained for all the students, but not the marks of each CO.
I want to fill the COs(Column B-F) based on the Column H(Total).
Example- A Student having total of 8 should have the value- 2,2,2,1,1 or 2,2,1,2,1, etc
Conditions-
All the cells should be filled in case of marks with >5
Each cell should have the value between 1 and 2. 0 in case when total is less than 5. As we don't have enough values to fill:
Previously, I used the randbetween(1,2) in Columns B to E (to get random values between 1 and 2) and for Column F, I used the formula =10-SUM(B6:E6)
It works fine, but doesn't work well if the difference is more than 2(which violates our condition)

With 2007 we need to refer to the cell in column A, so change that to an empty cell or text. Put this in B2 and copy over and down:
=IF(SUM($A2:A2)+6-COLUMN(A$1)>=$H2,1,IF($H2/(6-COLUMN(A$1))>=2,2,RANDBETWEEN(1,2)))
with Office 365 we do not need Column A to be blank or text as we no longer need to refer to it:
=LET(
sm,H2,
rng,B2:F2,
clm,COLUMN(rng),
{1,1,1,1,1}+ISNUMBER(MATCH(clm,TAKE(UNIQUE(RANDARRAY(1,100,MIN(clm),MAX(clm),TRUE),TRUE),,sm-5),0)))

Related

How to create a dynamic formula to find the average of a set of values for a given vector

I am trying to create a formula that gives me the average of the last 12 entries in a given dataset depending on the associated vector.
Let's make an example:
I have in column F2,G2,H2 and I2 dates, Company1, Company2 and Company3 respectively. Then from row3 to row 33 I have months dates starting from May 2016.
Date Company1 Company2 Company3
May-16 2,453,845
Jun-16 13,099,823
Jul-16 14,159,037
Aug-16 38,589,050 8,866,101
Sep-16 63,290,285 13,242,522
Oct-16 94,005,364 14,841,793
Nov-16 123,774,792 7,903,600 41,489,883
Dec-16 93,355,037 12,449,604 69,117,105
Jan-17 47,869,982 13,830,712 83,913,764
Feb-17 77,109,905 10,361,555 68,176,643
The goal is to create a formula that, when I drag it down, correctly calculates the average of the last 12 values for a given company.
So for example i would have, say in table "B2:C5":
Company1 76,856,345
Company2 11,120,859
Company3 65,674,349
And, if a new Company4 is added to the list, then I just have to drag it down the formula, to calculate the average of the last 12 months for Company4.
Until now, I have came up with this formula:
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(G:G),ROW(G:G)),ROW(INDIRECT("1:"&MIN(12,COUNT(G:G))))),ROW(G:G),G:G ))
This formula correctly calculates the average of a given column, considering only the last 12 values. The last step would be to come up with a formula that includes all the columns and then calculates the average for the given company.
Thanks!
I recommend that you use a named range to define your data in columns G:I. When a company is added, just modify the named range's specs. I used the name Target. Of course, you can replace it with $G:$I if you feel so inclined but I would rather recommend reducing the number of rows in the range, which is easier to manage when it is named.
Use the formula below to extract the company names from the first row of Target into the first column of your averages table. This is to ensure that the names are spelled identically in both locations.
=INDEX(Target,1,ROW()-2)
The number 2 indicates the number of rows above the row containing the formula. it is copied here from cell M3. There, ROW()-2 creates the number 1, counting sequentially as the formula is copied down.
Now I have the formula below in my cell N3 and copied down.
=SUM(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))
The formula simply sums up the columns G, H, and I in 3 consecutive rows.
In the final step I inserted the range definition established above, meaning excluding the SUM() function, into your existing formula.
=AVERAGE(LOOKUP(LARGE(IF(ISNUMBER(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0)))),ROW(INDIRECT("1:"&MIN(12,COUNT(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))))),ROW(INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))),INDEX(Target,0,MATCH($M3,INDEX(Target,1,0),0))))

All combinations of 4 out of 7 columns with totals using excel

I have 7 columns to choose from and I need to pick 4 of those columns and generate a total for each row. I also need every combination of 4, which means I'll have 35 new columns with the totals for each of those combinations showing in each row. I need the code for this and if it can be done only using Excel. Here is an image of the columns and the grayed ones are the 7 columns I'm talking about. My knowledge of Excel is very limited. There are over 1,500 rows if that matters.
multi step approach that is going to use some helper rows. there may be a more elegant formula that will do this, and much slicker options in VBA, but this is a formula only approach.
Step 1 - Generate List of Column Combination
To generate the list 4 helper rows will need to be insert at the top of your data. either above or below you header row. These 4 rows will represent the column number you are going to pick. To keep the math simpler for me I just assumed the 1 for the first column and 7 for the last column. those numbers will get converted to later to account for column in between in your spreadsheet. For the sake of this example The first combination sum will occur in column AO and the first helper row will be row 1. The first combination will be hard coded and it will seed the pattern for the remainder of column combinations. Enter the following values in the corresponding cells:
AO1 = 1
AO2 = 2
AO3 = 3
AO4 = 4
In the adjacent column a formula will be placed and copied to the right. It will automatically augment the bottom value by 1 until it hits its maximum value at which point the value in the row above will increase by 1 and the the value of the current will be 1 more than the cell above. This will produce a pattern that covers all 35 combinations by the time column BW is reached. Place the formulas below in the appropriate cell and copy to the right:
AP1
=IF(AO2=5,AO1+1,AO1)
AP2
=IF(AO2=5,AP1+1,IF(AO3=6,AO2+1,AO2))
AP3
=IF(AO3=6,AP2+1,IF(AO4=7,AO3+1,AO3))
AP4
=IF(AO4=7,AP3+1,AO4+1)
Step2 - Sum The Appropriate Columns
I was hoping to use a some sort of array type operation to read through the column reference numbers above, but I could not get my head around it. Since it was just 4 entries to worry about I simply added each reference manually in a SUM function. Now the important thing to note is that we will be using the INDEX function over the 13 columns that cover the range of your columns so to convert the index number we figured out above, to something that will work to grab every second row, the number that was calculated will be multiplied by 2 and then 1 will be subtracted. That means 1,2,3,4 for the first column combination becomes 1,3,5,7. You can see this in the following formula. Place the following formula in the appropriate cell and copy down and to the right as needed.
AO5
=INDEX($AB5:$AN5,AO$1*2-1)+INDEX($AB5:$AN5,AO$2*2-1)+INDEX($AB5:$AN5,AO$3*2-1)+INDEX($AB5:$AN5,AO$4*2-1)
pay careful attention to the $ which will lock row or column reference and prevent them from changing as the formula is copied.
Now you may need to adjust the cell references to match your sheet.

Conditional Unique ID for each record excel

I have two values in different columns. Column A have Department name i.e. HR, Admin and Ops. and column be have date. I want Unique ID in column C based on Combination of Column A & B and Unique number at the end.
Unique ID: HR-Aug-16-1
Admin-Aug-16-1
this number will be repeat till the combination of Column A and B repeated 50 times after 50 times last value will be increased by +1. i.e.
HR-Aug-16-2
Admin-Aug-16-2
Right now I am using formula,
=A1&"-"&TEXT(B1,"mmm-yy")&"-1"
In C1 as a standard formula,
=A1&"-"&TEXT(B1,"mmm-yy")&CHAR(45)&INT((SUMPRODUCT(--(A$1:A1&TEXT(B$1:B1, "mmm-yy")=A1&TEXT(B1, "mmm-yy")))-1)/5)+1
I've set this to repeat after 5 for an example. I'll leave it to you to change the modifier to 50. Fill down as necessary.

To filter multiple columns with a condition on the results

I am trying to find a way of highlighting a result with multiple conditions. I have no knowledge of pivot tables. I would rather use a formula or macros. The table is organised by Dealer.
Acc NAME Add Dealer Total
68687 Sara 11 Wood 111A 0
68687 Sara 11 Wood 111A 0
32187 Sara 11 Wood 111A 0
12345 Tom 10 Main 7878C 2
12345 Tom 10 Main 7878C 2
54321 Tom 10 Main 7878C 2
My table is similar to the one above. I want to select where the Total is greater than 0 & for each Dealer each unique Account number with the lowest Account number highlighted somehow.
So the results I want for the table above would be: Dealer 7878C, Accounts 12345, 54321.
12345 being the lower of the two, it is highlighted.
I don't mind copying the results onto another sheet, as I don't want to remove any data from the sheet. I started by just filtering the Totals for >0 and I was thinking of trying to filter for unique values in Account but its the next step that I am stuck on. A countifs formula?
The sheet is quite large and I'm just not sure which is the best way to try and do it.
Thanks for any help.
There's a nice but complicated way to do it.
With your original data:
With changed data:
As you can see I've placed your data in A1:E7.
I use two array formulas, one for the Dealer in G2:G5 and one for the Accounts H2:N5. The Dealer formula is vertical, and the Accounts formula is horizontal.
For the dealers put this array formula in G2 (press Ctrl+Shift+Enter to enter it):
=IFERROR(INDEX($D$2:$D$7,SMALL(IF(($E$2:$E$7>0)*(COUNTIF($G$1:$G1,$D$2:$D$7)=0),ROW($D$2:$D$7)-1),ROW($G$1:$G1))),"")
Now copy G2 down to G3:G5 to get the rest of the relevant dealers.
For the accounts put this array formula in H2:
=IFERROR(SMALL(IF(($D$2:$D$7=$G2)*(COUNTIF($G2:G2,$A$2:$A$7)=0),$A$2:$A$7),1),"")
Now copy H2 to the right, I2:N2, and down to H3:N5.
To make the first accounts bold I simply make the H column formatted as Bold.
You can copy these formulas farther as needed. Note that the locations are important. If you want to place the formulas elsewhere you'll need to change the references accordingly.
Formulas explained
What these formulas do is check for your conditions, and then get the smallest value that hasn't been retrieved yet, in the upper / left most cells.
The two formulas are mostly the same, apart from the fact that in the account numbers we can use the actual numbers, and with the dealer we use the row number instead.
The dealer formula from the inside out:
The conditions are set in the IF part of the formula, with a multiplier * as a logical AND (TRUE*TRUE=TRUE FALSE*TRUE=FALSE).
The first condition in IF(($E$2:$E$7>0)*(COUNTIF($G$1:$G1,$D$2:$D$7)=0),... checks for the row's Total value to be greater than zero, the second condition checks that the dealer is not already present in the G column. The second condition is irrelevant in the first cell, but in the second cell G3 it becomes COUNTIF($G$1:$G2,... which returns more than 0 if the dealer already exists, and evaluate to FALSE.
If the conditions are met the IF returns the dealer's index by using its row minus 1 ROW($D$2:$D$7)-1, which returns 1 for the first etc. as the starting row is 2. Otherwise it returns FALSE which is ignored.
The SMALL function returns the k-th smallest item. It ignores the FALSE items, and in our case returns the k-th smallest index that meets the conditions (Total>0 and not already present in the results). SMALL(...,ROW($G$1:$G1) in the first cell return the first item. ROW($G$1:$G2) in the second cell G3 evaluates to 2 and returns the second smallest item, and so forth.
The INDEX function simply returns the dealer from the data according to the index.
And finally, the IFERROR is there only to hide the errors when the end of the results is reached.
based on your sample data and assuming a header row in row 1 and the left column being column A.
=COUNTIF($A$2:A2,A2)
place that in F2 and copy down. Then do a filter on the helper column =1

Excel: Obtain a column by sorting anotr one values

I need to automatically obtain a sorted column of values from another given column values, like in the sample:
I have I need A unchanged, and also B obtained from A
A A B
-----------------
1 1 0
0 0 0
3 3 1
8 8 3
0 0 8
I mean if the values from A changes, the B should change accordignly...
Is that possible in MS Excel?
Here a sandbox and sample:
http://1drv.ms/1SkqMhS
If you put The formula =SMALL(A:A,ROW()) in B1 and copy down then the cells in B will be linked to the cells in A in such a way that the numbers in B will be the numbers in A in sorted order. This won't be efficient for larger ranges but will work fine for small to medium size ranges.
If you want the numbers to start in a lower row, say B2 because you have a header in B1, adjust ROW() to something like ROW()-1.
A word of warning: Use of ROW() can make a spreadsheet somewhat fragile in that formulas that involve it can change their meaning if rows are inserted or deleted or the block containing the formula is moved to somewhere else. Rather than using ROW(), there is something to be said for adding a helper column which numbers the data in A (which would then be in e.g. B) and referring to these numbers rather than small. For example, in:
If I put the formula
=SMALL($B$2:$B$5,A2)
In C1 and copy down, it works as intended. In response to a question you raised in the comments, I added still another column which gives an index where the corresponding value occurs. To do this I wrote in D2 (then copied) the formula
=MATCH(C2,$B$2:$B$5,0)
Of course. Highlight your range and in the Data tab, click "Sort", then you can choose how you want to sort your data:
If column B has information that is to be used with Column A (like next to A1 is "Car"), and you want to sort the whole table, based on Column A, then just select Columns A and B, then sort by column A.
Found the answer, thanks to John Coleman !
Just some minor details like cell value fixing (with $, like A$2)and the -1+ROW adjustment for the 1 header row!

Resources