简体   繁体   中英

Extract high and lows on monthly basis from Excel dataset

I am trying to extract the highs and lows for each month from a set of daily high, low, close data on a stock price in Excel over several years. How would I go about doing it, i have attached an image of a spreadsheet showing my attempt.

在此处输入图片说明

why not just use a pivottable and change the pivot field aggregate method to maximum or minimum for High and Low fields.

Here is an example image (forgive the use of a Mac!) of adding the fields 添加字段的图像

And result

结果字段示例

You don't have to double up on Max and Min for each field. I just wanted to show it was possible.

The formula you came up with looks like you are on the right track, but I would change to this:

= MAX(IF((MONTH($B$2:$B$100)=J2)*(YEAR($B$2:$B$100)=I2),$C$2:$C$100))

And then drag this formula down as far as necessary.

(Similarly, to find the minimum for each month, just replace MAX in the above equation with MIN .)

Note this is an array formula, so you must press Ctrl + Shift + Enter after typing this formula rather than just pressing Enter .

Also note you should include your entire data in this formula, not just to the end of January. In this example I took it down to the 100th row, but you will need to adjust that based on where ever your data ends.

Or if you are using Pivot Tables, @QHarr's solution will work also.


EDIT

As requested, here is what is going on in this formula:

Both (MONTH($B$2:$B$100)=J2) and (YEAR($B$2:$B$100)=I2) return vertical arrays (of the same size) of TRUE 's and FALSE 's. When you multiply them together, you get an array of 1's and 0's. (The indices of this output array will equal 1 where the corresponding indices of the input array were both equal to TRUE .) You are not actually multiplying the MONTH and YEAR together. This would be pointless because for example for February of 2017 you would get a value of 4034 which is meaningless.

Simple example of multiplying two logical arrays together: {TRUE;TRUE;FALSE}*{TRUE;FALSE;TRUE} would return {1;0;0} . You might expect that it should return {TRUE;FALSE;FALSE} but when multiplying two logical arrays, it automatically converts to an array of 1's and 0's. However, this isn't an issue because the IF statement will handle an array of 1's and 0's the same way it would handle an array of TRUE 's and FALSE 's so there is no need to convert it back to logical values.

The reason AND doesn't work is because AND always returns a single result (not an array). For example, AND({TRUE;TRUE;FALSE},{TRUE;FALSE;TRUE}) would simply return FALSE . Because of this, AND is typically not very useful in array formulas since it, well, doesn't return arrays haha.


As far as returning +1, -1, or 0 depending on how it compared to the previous month, this is fairly easy to accomplish.

Since the output is in K column, I'll base it off of that.

In cell L2 , type the formula (and drag down as necessary):

= IF(K2>K1,"+1",IF(K2<K1,"-1","0"))

This formula works fine unless you insert rows, then it may screw up the formula, which is why I usually prefer to do something like this below. (It adds a bit more to the formula, but is "safer"):

= IF(K2>INDEX(K:K,ROW()-1),"+1",IF(K2<INDEX(K:K,ROW()-1),"-1","0"))

You'll notice that these formulas are identical except that in the formula above, K1 was replaced with INDEX(K:K,ROW()-1) . The issue with just typing K1 is that, if a row is inserted between the first and second row, the reference to K1 won't change, even though, you want it to actually increase to K2 . This is solved with INDEX(K:K,ROW()-1) which always just references the cell in the K column directly above it, even if rows are inserted in the sheet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM