Google Sheets: Repeat values as rows based on associated numbers - google-sheets

I have a table in Sheets that has 2 columns: (1) Station (2) Number of people at Station:
Station Number of People at Station
A 1
B 2
C 1
D 4
I want to have a new column in excel populate with Station names repeating in it's own row based on number of people at station. The expected output would be below:
Station Allocation
A
B
B
C
D
D
D
D
Is there a way to dynamically have this final table change based on the number of people at station? So if someone was to use a google sheet and they change Station A to 2 people, then there will now be 2 rows with A instead of 1.

Try:
=arrayformula({"Values";query(flatten(if(B2:B<>"",split(rept(A2:A&"|",B2:B),"|"),)),"where Col1 is not null",1)})
Replace "|" with a character that is not used in your dataset.

Related

Calculations Dependent On Multiple Drop-down Selections

I have the following spreadsheet calculating my household outgoings with a number of categories to choose from multiple drop-down lists (Cols B, C).
In Column H, with the header Outgoings, I would like to sum the columns D and E in two rows based on the selections made in the drop-down lists in J2, J3 which act as filters for said calculation. These drop-down lists reflect the Bill Type in Column B, and the Category in Column C.
As an example, if I selected from the drop-downs in Column J: Personal, Credit/Debit then I would like the two rows in Column H (Outgoings) to be populated with the total sum of the rows D and E, respectively, for where the Bill Type and Category in B and C match Personal and Credit/Debit.
Along with the above conditions, if there is no selection in either of the Column J drop-downs, I would like the rows in Column H (Outgoings) to simply provide the calculation of the incomes in D, E (Row 2) minus the remaining sum of each column (total outgoings, i.e. D2:D).
I'm a little new with the syntax required to perform more complex calculations and was wondering if this was achievable in spreadsheets, specifically, in Google Sheets.
Try this in cell H2:
=if(J2&J3="",
sum(filter(D:D,C:C="Income"))-sum(filter(D:D,C:C<>"Income")),
sum(filter(D:D,B:B=J2))+sum(filter(D:D,C:C=J3))
)
This in H3:
=if(J2&J3="",
sum(filter(E:E,C:C="Income"))-sum(filter(E:E,C:C<>"Income")),
sum(filter(E:E,B:B=J2))+sum(filter(E:E,C:C=J3))
)

Populate a new Google Sheets sheet, with a random sample, with at least X samples of a given attribute

I have a data set which I would like to take a random sample from and place in to a new sheet. I have one extra constraint / stratification: I would like X examples of each of a given attribute.
For example, if COL A has 5 rows of Apples, 5 rows of Bananas etc., I would like a random sample which includes 2 Apple rows, 2 Banana rows and so on for as many values of COL A as there are.
I am halfway there having got a formula to populate a new sheet with a random sample:
A1: =ArrayFormula(FILTER( SORT('My list of 100000 rows'!A:A ;RANDBETWEEN( 0+ROW('My list of 100000 rows'!A:A) ; ROWS('My list of 100000 rows'!A:A)); TRUE); ROW('My list of 100000 rows'!A:A)<=100))
but this doesn't give me the ability to select a minimum or exact number of instances of each unique attribute.
Any advice is appreciated!
I would like a random sample which includes 2 Apple rows, 2 Banana rows and so on for as many values of COL A as there are.
Insert two columns to the left of your data and in A1:
=choose(randbetween(1,10),"12","13","14","15","23","24","25","34","35","45")
in B1 and copied down to suit:
=countif(C$1:C1,C1)
then :
=query(A:D,"select C,D where B contains '"&left(A1)&"' or B contains '"&right(A1)&"' ")

Google Sheets query to extract the top three instances according to criteria

I have a Google Spreadsheet with two sheets.
In sheet "Source" I have a series of countries, cities and landmarks - these are,respectively, in columns A, B and C.
In sheet "Sheet for Query", there are two columns: (A) Country, which has a list of unique country names; and (B) Top 3 cities by Landmark. In column B, I would like to have a Query which gives me, for each country, the top three cities by number of landmark, i.e., the query just has to count the number of instances each city in each country appears and return, for each country, the names of the three cities that come up the most times
This is a sample sheet that I've created in order to demonstrate what I mean: https://docs.google.com/spreadsheets/d/1IPwtAHjwjV1A03o9URws-AtDKw3h9QS9UTT0P1PeVN0/edit?usp=sharing.
Thank you!
I've given this some thought and to 'just' count the number of instances and return the top 3 in each country is surprisingly difficult.
The grouping is straightforward with a query like this
=query(A:C," select A,B,count(C) where A<>'' group by A,B order by A,count(C) desc label A 'Country',B 'City', Count(C) 'Landmarks'",1)
But I don't know of a way of getting the top 3 for each group without going through 2 further steps
(1) Number the results in each group (various ways of doing it but here is one)
=(E1=E2)*D1+1
where the country names after grouping are in column E.
(2) Filter the result for the number in column D being less than 4
=filter(E:G,D:D<4)
You don't specify what qualifies as top (so assuming those are the first listed - higher up the sheet), and you don't clarify number of landmark where there are no numbers in your sheet, but perhaps:
=textjoin(", ",,query(Source!A:C,"select B where A='"&A2&"' limit 3"))
in B2 of sheet for Query, copied down to suit.

How to use QUERY in Sheets to find most recent grade for each student?

I have a spreadsheet containing a list of assessments for students. It is comprised of four columns:
A B C D
Student Standard Date Score
On another sheet I have a table containing a list of students in column A and a list of standards in Row 1.
I want to query the spreadsheet of assessments to return the score of the most recent assessment for the current student on the current standard. Currently I have this formula in cell K3:
=Query(Assessments!A:D,"select D where(A="&$A3&" and B="&K$1&") order by max(C) limit 1")
but it gives me this error:
Unable to parse query string for Function QUERY parameter 2: AGG_IN_ORDER_NOT_IN_SELECTMAX(C)
Edit:
I've gotten some great answers, but I guess I asked poorly. What I really need is the score from the most recent assessment. The results from JPV and pnuts have both given the date of the most recent assessment. Here is a stripped down version of the actual file: spreadsheet.
In the Students sheet I'm needing a formula in the Green cells that results in a number from 0 to 4 based on the data in the Assessments sheet.
This is why in the query I was trying to select D, but order by max(C).
I tweaked one of these formulas and got:
=iferror(query(A3:D20, "Select D where A = '"&G20&"' and B = '"&I20&"' order by C desc limit 1",0), "no data found")
I addition to the fine solution provided by pnuts (+1 for the sample data), here are some posibilities using query (check the green cells in this spreadsheet.
=query(A2:D20, "select A, MAX(C) GROUP BY A pivot B",1)
or
=query(A2:D20, "select A, B, MAX(C) GROUP BY A, B",1)
should create a table with the latest date per student, per standard.
In case you want to use cell references (where a cell holds a student name and another cell holds the standard), try:
=iferror(query(A3:D20, "Select A, B, C where A = '"&G20&"' and B = '"&I20&"' order by C desc limit 1",0), "no data found")
where G20 is student name and I20 is standard (change range to suit).
Does not use the query function but seems to "query the spreadsheet of assessments to return the most recent assessment for the current student on the current standard":
=iferror(max(filter(Assessments!$A$1:$D$99,Assessments!$A$1:$A$99=$A3,Assessments!$B$1:$B$99=K$1)),"")
Constructed data as example:
Student Standard Date Score
Bod5 C 2/2/2015 9
Bod6 B 1/1/2015 8
Bod7 C 7/7/2015 7
Bod8 A 9/9/2015 6
Bod1 B 3/3/2015 5
Bod2 C 4/4/2015 43
Bod3 B 6/6/2015 2
Bod4 C 1/1/2015 1
Bod1 A 1/1/2016 8
Bod1 A 2/2/2017 7
Bod1 A 1/1/2013 6
Bod1 A 1/1/2011 5
Bod9 A 9/9/2009 9
Bod9 B 1/1/2011 3
Bod9 C 3/3/2013 2
Bod9 A 10/10/2010 4
Bod9 B 11/1/2001 2
Bod9 C 4/4/2014 1
Output:

Sum values if any cell in corresponding row matches text

I have the following Google Sheets data:
Name1 Name2 Name3 Value
A B C 20
B A C 30
C D F 40
What I'm trying to do is see if a specific name appears in any of the three name columns. If it does, then sum all the values in the "Value" column for all matching rows.
For example, I want to sum all of the values for name "A". That name only appears on the first two rows, so it should do 20+30 to give me 50. If I change the searched name to "C", that appears in all three rows so it should sum all of the numbers: 20+30+40. The algorithm needs to adjust and search appropriately.
=DSum will work
With the example you give use
=dsum(A1:D4,D1,{A1;"A"})+dsum(A1:D4,D1,{B1;"A"})+dsum(A1:D4,D1,{C1;"A"})
You can swap the "A" for a Cell reference
see https://drive.google.com/previewtemplate?id=0As3tAuweYU9QdEVHdTFHNzloSTY4LVYxdW9LdHRHbEE&mode=public#

Resources