How can I generate a random number (or Cell) within a given range? Witout using the same result twice? - excel-2010

I am trying to generate a random Cell from specific range:
I need to each cell Row to generate a random selection from a a specific column (range)
below is a picture of my set up and my failed attempts:

You can do this like this:
Add a random number next to your data set using =RAND(). I've used column B, but you can put it wherever you like.
Add this formula to cells C2 to H2
=INDEX($A$2:$A$21,RANK.EQ(INDEX($B$2:$B$21,COLUMN()-2),$B$2:$B$21))
How it works:
RAND() returns a random number in the range [0..1) This is used as a random sort order for your data
Breaking down the formula:
COLUMN()-2 returns a sequential number 1..6 for columns C to H
INDEX($B$2:$B$21, ... ) returns the 1st to 6th number from the random number list
RANK.EQ( ... ,$B$2:$B$21) returns the position of the random number in the sorted random number list, 1..20.
=INDEX($A$2:$A$21, ... ) returns an item from your data set, based on the random rank from above.
Note: This will return a new randon sample each time Excel recalculates.

The only way to make a random selection that does not repeat is to make an array of integers, than randomize it, and than take out one by one.
For example you start with:
1 2 3 4 5 6 7
you randomize it (swap elements randomly)
2 4 5 1 7 3 6
than you take elements out one by one. (keep an index of how much elements you used in some cell)

Ok here is one trick, add another column with random numbers, than select both random column and range column and click the sort button (picture is from libreoffice but there is a similar button in excel) This will randomize your range column. Than you simply assign values to "w-1" : "w-6" like this =B2, =B3, =B4, =B5, =B6, =B7
I tried this and it works like you wanted, the only problem is it will shuffle values in your range column.

Related

Using ArrayFormula with a Dynamic Number of Column Header Names

My goal is to use ArrayFormula with the SPLIT() function, and name the headers of each column.
My problem is that the formula below only works when the number of headers declared exactly matches the first row's number of elements to split ie. if there are 3 elements being split on the first row, the formula needs 3 headers named (g1, g2, g3), but if any rows have more than 3 elements to split, it gives an error.
Is there a way to make the column header names dynamic in number, so that the number of elements to split can be, say, from 0-10? The elements to be split will always be separated by a comma and no spaces.
=ArrayFormula({"g1", "g2", "g3";if(A2:A="","",split(A2:A,","))})
link to example: https://docs.google.com/spreadsheets/d/1c2pskSYsGs12Yjbn-5gORQ22mDSaC9cSnp1nWeULlf4/edit?usp=sharing
You can try:
=index(iferror({"g"&sequence(1,max(len(substitute(
transpose(query(transpose(if(iferror(split(A2:A,","))="",,"z")),,9^9)),
" ",))));split(A2:A,",")}))
If we can use the Orders column, it's as simple as:
=index(iferror({"g"&sequence(1,max(B:B));split(A2:A,",")}))
You can achieve it by combining the index function, the sequence function and the max function. Here is the thought process behind it:
The max function (you can read more about it here) will retrieve the maximum value of the orders column.
The sequence function (you can read more about it here) will generate a series starting at 1 and ending at the previous maximum value.
The index function (you can read more about it here) will distribute the elements of the sequence (with a "g" in front) across as many cells as elements are in the sequence.
If you combine those, you get:
=INDEX("g"&SEQUENCE(1,MAX(B:B)))

Automatically fill sum column

I have a table of items with its buying and selling rates. Against each transaction, I wanted to show the item qty currently available and the current cost. Here is a screenshot of the table.
For the columns of Qty and Rate, I have used the formulas as shown below:
=SUMPRODUCT(C$2:C-F$2:F,B$2:B=B2,A$2:A<=A2)
=SUMPRODUCT(I$2:I/J2,B$2:B=B2,A$2:A<=A2)
Is it possible to convert SUMPRODUCT formulas as an array formula (returning array) so that it automatically fills whenever a row is added? Something like filling a column using FILTER or QUERY?
Regards,
Pravin Kumar.
I am making an assumption that this is your goal:
To automatically fill the quantity remaining column with the result of the difference between the two values, if and only if, there are two values provided.
A solution to this objective is:
=arrayformula(if(and(C2:C<>””, F2:F<>””), C2:C-F2:F, “”))
This produces a formula that subtracts C from F only if both C and F have values, and for all rows that have values in both C and F. If one of the rows does not have a value, the result will yield “” (blank). This formula should be posted at the top of the column where you want these results to start. In your screenshot example this would be cell J2. NOTE: 0 is still a value, and also that an array formula will not overwrite manually input data, so once you paste that function in J2, you will have to clear the cells below in order for it to auto populate.

How to print different values to column based on value in another column and other conditions met?

I have a spreadsheet with several columns and I want to return a different value based on the value in Column A and if any of the other columns show a true or 1.
For example
If column A has the value "A" and any column B-N is either TRUE or 1 then I want to return "Good" to column O
If column A has the value "B" and any column B-N is either TRUE or 1 then I want to return "Best" to column O
Link to spreadsheet: https://docs.google.com/spreadsheets/d/12k9usKsOgrOUhtW5WBvfY7WB5hbfnTMSPgXtrqcRFjM/edit?usp=sharing
Try this in cell O2 (where your sample sheets only has values of TRUE, FALSE, Y or N in B2:N):
=arrayformula((trim(transpose(query(transpose(iferror(regexreplace(regexreplace(text(B2:N,),"(FALSE)|(N)",),"(Y)|(TRUE)",if(A2:A="A","Good",if(A2:A="B","Best",))),)),,columns(B2:N))))))
Alternative where values in B:N are either 0 or 1:
=arrayformula((trim(transpose(query(transpose(iferror(substitute(substitute(B2:N,0,),1,ifs(A2:A="A","Good",A2:A="B","Best")),)),,columns(B2:N))))))
B2:N is the range of cells to process.
The inner SUBSTITUTE clears all 0 values.
The outer SUBSTITUTE swaps 1 values for a test to see if values in A contain "A" or "B".
IFS does the test and returns either "Good" or "Best".
IFERROR hides any #N/A values down the sheet where the rows are empty.
TRANSPOSE transposes the data for QUERY.
QUERY is used to collapse empty cells (vertically). columns(B2:N) is used in the header part of QUERY. This is a quirk of QUERY, where the header number >= the columns of data, QUERY does the collapse.
TRANSPOSE reverts the dataset to the previous orientation.
TRIM removes leading or trailing space.
ARRAYFORMULA allows the formula to automatically cascade down the sheet, rather than you needing to drag the formula down (like with =IF(AND({B2:N2}>0, A2="A"),"Good", "")).
So, you already have this partial solution. To get a second condition to be met before printing something to your cell, just add an AND() with a new COUNTIF(), comparing A column with A, and then at the else argument, repeat your original IF(), just changing A for B and the output for each case. I will look like this:
=IF(AND(countif(A2;"A");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Good";
IF(AND(countif(A2;"B");OR(countif(A2:N2;"Y");countif(A2:N2;TRUE)));"Best";"BAD"))
To use it on every row, just autofill the column O. The row numbers will change accordingly and work on its own.
If you need a new if() statement for a third or fourth case, just repeat it, nesting one IF() inside the other, leaving room for a default error message at the end.

Can change shape of range with ARRAYFORMULA() in Google Sheets?

My intention is to convert a single line of data into rows consist of a specific number of columns in Google Sheets.
For example, starting with the raw data:
A
B
C
D
E
F
1
id1
attr1-1
attr2-1
id2
attr2-1
attr2-2
And the expected result is:
(by dividing columns by three)
A
B
C
1
id1
attr1-1
attr1-2
2
id2
attr2-1
attr2-2
I already know that it's possible a bit manually, like:
=ARRAYFORMULA({A1:C1;D1:F1})
But I have to start over with it every time the target range is moved OR the subset size needs to be changed (in the case above it was three)!
So I guess there will be a much more graceful way (i.e. formula does not require manual update) to do the same thing and suspect ARRAYFORMULA() is the key.
Any help will be appreciated!
I added a new sheet ("Erik Help") where I reduced your manually entered parameters from two to one (leaving only # of columns to be entered in A2).
The formula that reshapes the grid:
=ArrayFormula(IFERROR(VLOOKUP(SEQUENCE(ROUNDUP(COUNTA(7:7)/A2),A2),{SEQUENCE(COUNTA(7:7),1),FLATTEN(FILTER(7:7,7:7<>""))},2,FALSE)))
SEQUENCE is used to shape the grid according to whatever is entered in A2. Rows would be the count of items in Row 7 divided by the number in A2 (rounded to the nearest whole number); and the columns would just be whatever number is entered in A2.
Example: If there are 11 items in Row 7 and you want 4 columns, ROUNDUP(11/4)=3 rows to the SEQUENCE and your requested 4 columns.
Then, each of those numbers in the grid is VLOOKUP'ed in a virtual array consisting of a vertical SEQUENCE of ordered numbers matching the number of data pieces in Row 7 (in Column 1) and a FLATTENed (vertical) version of the Row-7 data pieces themselves (in Column 2). Matches are filled into the original SEQUENCE grid, while non-matches are left blank by IFERROR
Though it's a bit messy, managed to get it done thanks to SEQUENCE() function anyway.
It constructs a grid by accepting number of rows/columns input, and that was exactly I was looking for.
For reference set up a sheet with the sample data here:
https://docs.google.com/spreadsheets/d/1p972tYlsPvC6nM39qLNjYRZZWGZYsUnGaA7kXyfJ8F4/edit#gid=0
Use a custom formula
Although you already solved this. If you are doing this kind of thing a lot, it could be beneficial to look into Apps Script and custom formulas.
In this case you could use something like:
function transposeSingleRow(range, size) {
// initialize new range
let newRange = []
// initialize counter to keep track
let count = 0;
// start while loop to go through row (range[0])
while (count < range[0].length){
// add a slice of the original range to the new range
newRange.push(
range[0].slice(count, count + size)
);
// increment counter
count += size;
}
return newRange;
}
Which works like this:
The nice thing about the formula here is that you select the range, and then you put in a number to represent its throw, or how many elements make up a complete row. So if instead of 3 attributes you had 4, instead of calling:
=transposeSingleRow(A7:L7, 3)
you could do:
=transposeSingleRow(A7:L7, 4)
Additionally, if you want this conversion to be permanent and not dependent on formula recalculation. Making it in run fully in Apps Script without using formulas would be neccesary.
Reference
Apps Script
Custom Functions

How to use INDEX() inside ARRAYFORMULA()?

I am trying to use the INDEX() formula inside an ARRAYFORMULA(). As a simple (non-sense) example, with 4 elements in column A, I expected that the following array formula entered in B1 would display all four elements from A in column B:
=ARRAYFORMULA(INDEX($A$1:$A$4,ROW($A$1:$A$4)))
However, this only fills field B1 with a the value found in A1.
When I enter
=ARRAYFORMULA(ROW($A$1:$A$4))
in B1, then I do see all numbers 1 to 4 appear in column B. Why does my first array formula not expand similar like the second one does?
The INDEX function is one that does not support "iteration" over an array if an array is used as one of its arguments. There is no documentation of this that I know of; it simply is what it is. So the second argument will always default to the first element of the array, which is ROW(A1).
One clumsy workaround to achieve what you require relies on a second adjacent column existing next to the source data* (although it is unimportant what values are actually in that second column):
=ArrayFormula(HLOOKUP(IF(ROW($A$1:$A$4);$A$1);$A$1:$B$4;ROW($A$1:$A$4);0))
or indeed something like:
=ArrayFormula(HLOOKUP(IF({3;2;4;1};$A$1);$A$1:$B$4;{3;2;4;1};0))
edit 2015-06-09
* This is no longer a requirement in the newest version of Sheets; the second argument in the HLOOKUP can just be $A$1:$A$4.
Here is a tip for using vlookup with an array, so that even if the columns are moved later on the formula will still work correctly....
In general, configure the vlookup so that it's reading only 2 columns and returning the second. This can be done by inputting only the 2 columns required, rather than a range and column index.
Example:
Replace the following formula which would fail if columns are moved
=arrayformula( vlookup(C:C, booking!$A:$E ,5 ,false) )
with this formula which will continue to work even if columns are moved
=arrayformula( vlookup(C:C, {booking!$A:$A,booking!$E:$E} ,2 ,false) )
Note, you can also simulate the index function using vlookup.
Example:
Column R:R contains the row index numbers for looking up data in column booking!$A:$A
=arrayformula(vlookup(R:R ,arrayformula({row(booking!$A:$A), booking!$A:$A}),2 , false))
It's a nested array, so it can be helpful to test in stages, eg just the inner part for one example, eg return entry in row 10:
=vlookup(10 ,arrayformula({row(booking!$A:$A), booking!$A:$A}),2 , false)

Resources