I'm trying to create a calculated field that displays the growth rate in % from one of the numeric columns in my pivot table.
How can I reference a row above the current cell to calculate the growth rate?
=(Current cell - (Current cell - 1 Row)) / (Current cell - 1 Row)
Pivot table
Year
Count
Calculated Field (YoY Growth)
2019
100
2020
300
100%
2021
600
50%
based on instructions:
=(Current cell - (Current cell - 1 Row)) / Current cell
={""; ARRAYFORMULA(IFERROR(B2:B-(B2:B-B1:B999)/B2:B))}
I agree with player0, that seeing your sheet, data, data types and layout would facilitate helping you.
However, I'll venture a solution, sight unseen.
There are actually many ways to address what I think you're asking. This is just one of them.
Suppose that your source numbers are in range A2:A. Place the following formula in some other open column, in the cell parallel to the top number in your source range:
=ArrayFormula(ARRAY_CONSTRAIN({"";FILTER(({A3:A;""}-A2:A)/{A3:A;""},A2:A<>"")},COUNTA(A2:A),1))
I've added in more "parts" than are probably necessary. But I tried to include controls, seeing as I don't know what your data actually looks like.
An alternative is to place the following simpler formula into the cell one below where the top number in your source range is:
=ArrayFormula(IF(A3:A="",,(A3:A - A2:A)/A3:A))
In either case, you'll want to format the column/range where the results will be as percent (Format > Number > Percent).
Obviously, if your source numbers are in a range other than A2:A, you'll need to adjust the ranges in either formula accordingly.
Related
This is more a design question so please bear with me.
I have a system that stores locations consisting of the ID, Longitude and Latitude.
I need to compare the distance between my current location and the locations in the database a only choose ones that are within a certain distance.
I have the formula that calculates the distance between 2 locations based on the long/lat and that works great.
My issue is I may have 10 of thousands of locations in the database and don't want to loop through them all every time I need a list of locations close by.
Not sure what other datapoint I can store with the location to make it so I only have to compare a smaller subset.
Thanks.
As was mentioned in the comments, SQL Server has had support for geospatial since (iirc) SQL 2008. And I know that there is support within .NET for that as well so you should be able to define the data and query it from within your application.
Since the datatype is index-able, k nearest neighbor queries are pretty efficient. There's even a topic in the documentation for that use case. Doing a lift and shift from that page:
DECLARE #g geography = 'POINT(-121.626 47.8315)';
SELECT TOP(7) SpatialLocation.ToString(), City
FROM Person.Address
WHERE SpatialLocation.STDistance(#g) IS NOT NULL
ORDER BY SpatialLocation.STDistance(#g);
If you need all the points within that radius, omit the top clause and change the predicate on STDistance() to something like SpatialLocation.STDistance(#g) < 1000 (the SRID I typically use has meters as the unit of measure, so this would say 'within 1 km').
https://gis.stackexchange.com/ is a good place for in-depth advice on this topic.
A classic approach to quickly locating "nearby" values, is to "grid" the area of interest:
Associate each location with a "grid cell", where each cell is a convenient size. Pick a cell-edge-length such that most cells will hold a small number of values and/or that is similar to the distance range you typically query.
If cell edge is 1 km, and you need locations within 2 km, then get data from 5x5 cells centered at the "target" location.
This is guaranteed to include all data +- 2 km from any location within the central cell.
Apply distance formula to each returned location; some will be beyond 2 km.
I've only done this in memory, not from a DB. I think you add two columns, one for X cell number, other for Y cell number.
With indexes on both of those. So can efficiently get a range of Xs by a range of Ys.
Not sure if a combined "X,Y" index helps or not.
This is hard to explain so my title sucks, and is just my best guess at how I might be able to approach this. I have a Google Sheet of sales data for cases of various bottle sizes of kombucha. Column E is the sale date, Column G contains the item code, and column J is the quantity sold of said cases. See my (vastly simplified) sample data:
https://docs.google.com/spreadsheets/d/17-LzGrNJtBr-FwOZtdaoCws3ayeGOHu_TdtGOfXj4cA/edit?usp=sharing
See my current test code below (also present in the Formula tab of the linked spreadsheet). It successfully gives me the combined number of cases sold of half-liter bottles and Growlers. The values in E4 and E5 are cells containing my start and end dates, respectively, so I'm constraining the results only to those which fall within a certain date range.
This code works, but now I need to figure out a way to sum the total number of bottles sold instead of # of cases. The data set is already massive and pushing the limits of google sheets, so adding a column to the source data sheet with # of bottles per case is not an option. Half liter cases hold 13 bottles, and growlers hold 5. Is there any way to do this with my current approach, using another array perhaps? Or any other approach that keeps the formula as simple as possible?
FYI the current formula is a proof of concept and I will be adding many additional types of cases to the existing formula, each containing a different number of bottles per case, and using it as part of a larger dynamic formula that allows you to switch between showing # cases vs # bottles vs # of actual liters sold, so this is why I am hoping to find an array-based approach that will let me do this without needing to resort to an absurdly long and complex formula of nested IF statements.
=SUMPRODUCT(--((XeroInvoiceData!$E$3:$E>=B4)*(XeroInvoiceData!$E$3:$E<=B5)), (--(ISNUMBER(MATCH(XeroInvoiceData!$G$3:$G, {"HalfLiterCase","GrowlerCase"}, 0)))), XeroInvoiceData!$J$3:$J)
I would be eternally grateful for any assistance.
Here is my solution:
https://docs.google.com/spreadsheets/d/1ig0krumJu4Lj9-nIKJyRfPLTYbU-mzOL0JokRUDEqNc/edit?usp=sharing
My idea was to filter your table on date and sum by the type of container.
I wanted also to allow new types of containers that contain smaller units (bottles or liters).
I divided this job into 3 stages.
First we have to filter this table according to selected dates and container types.
I prepared a list that may be extended (all you need is to extend the filter range).
Then I have to vlookup values of units in each container and I try to do it inside the same formula.
General idea is
={[query results],arrayformula(ifna(vlookup([first column of query],$C$21:$D$26,2,0)*[second column of query])}
I divide it into 2 stages.
First stage referrs to query results in adjacent table:
Second stage uses indexes of query so formula is quite long:
Tell me if it solves your problem.
I have a spreadsheet document that I want to make as efficient as possible. It is used to calculate the quantity-based discounts of a product. The more you buy, the cheaper the unit is.
https://docs.google.com/spreadsheets/d/1JxrvA3WPANQeIXqLt-7NFFgyxxKbqr_4rW_Q5ptAvKA/edit?usp=sharing
In the first red cell, call "tiers" I will input the quantities at which the discounts will be applied.
I want to use this array in the table directly underneath, one item per row until we run out of numbers.
I will then enter the percentage values in the right hand side and that will then populate the rest of the fields.
So all I will need to do is enter the cost price, the tiers, and the percentages and this calculator does the rest.
My question is, how do I create new rows based on this array in the correct place?
paste in B10 cell and drag down:
=B$9-B$9*C10
paste in B15 cell:
=ARRAYFORMULA(IFERROR(A15:A*VLOOKUP(A15:A, A9:B12, 2)))
spreadsheet demo
Referring to the snippet of a pivot table below in the image, there are 6,000 J####### models (i.e. J2253993, J2254008, J2254014 ... etc).
How can the difference between the last Odometer reading and the first Odometer reading for each model be calculated? There is no consistency in the number of recorded months for each model and there is no consistency between the first and last timestamps for each model.
i.e.
For model J2253993:
Desired answer is: 378
Because 2501 minus 2123
For model J2254008:
Desired answer is: 178
Because 1231 minus 1053
... And so on for the remaining 6,000 models
Would a dynamic array be needed?
Messy SUM/INDIRECT Solution
EDIT: A similar formula for Max-Min in column B (my first idea):
=INDEX(INDIRECT("B"&MATCH(E4,A$1:A$50000,0)+1&":B50000"),MATCH("",INDIRECT("B"&MATCH(E4,A$1:A$50000,0)+1&":B50000"),0)-1)-INDEX(B$1:B$50000,MATCH(E4,A$1:A$50000,0)+1)
I abandoned it because the image wasn't showing any empty cells.
EDIT-END
The formula is calculating the C column sums. A drawback is that you have to insert ="" in all the empty cells of column C unless you know a way how the MATCH function returns an empty cell. In the E column write the ID-s starting from the 4th row and in F4 write the formula:
=SUM(INDIRECT("C"&MATCH(E4,A$1:A$50000,0)+2&":C"&MATCH("",INDIRECT("C"&MATCH(E4,A$1:A$50000,0)+2&":C44"),0)-1+MATCH(E4,A$1:A$50000,0)+2))
Copy/Paste down.
If I am understanding you correctly, it looks like you just need to add a sum of the "Odometer Reading Change" column in your pivot table. When I sum them for J2253993 I get 378 like you say.
Pivot table will total all of the rows by model based on the way you have built it, no matter how many rows are there.
I have a pivot table
Year and Week are Rows. Orders and Return Loaded are calculations based on sum(OrderSum) and sum(Reutilized) respectively
What I need is a third calculation based on the division of Return Loaded and Orders. This is to show what % Reutilized represent of the total orders. In the first case would be 14.04% (8 represent 14% of 57).
That type of calculation is not possible within the pivot itself. There are two general workarounds.
You can setup the calculation in the underlying data in a way that lets you pull the value through to the pivot. I would need to know how that is laid out to explain further.
You can create an additional table where you read the values from your pivot using GetPivot() formulas and then do your calculation there. You would manually add new rows here for new weeks.