-1

Dear StackOverflow Participants,

My post was ambiguous before, so here's a second attempt.

I have the following table of data:

Raw chart data

And hand-drawn examples of what I'd like to produce:

I'd like to produce a chart that displays calculated values for all codes (left-hand column of the Raw Chart Data pic) that have the same first four digits. I would like to display a data point for each date that sums all values under the "A" column. I would do the same with the "B" column. The "P" data point is problematic though, because it would use a calculation using each code that begins with the same first four digits. I'm referring to the "A", "B" and "P" that are under each date along the top of my spreadsheet as in the Raw Chart Data pic.

"P" is a % value, and has a corresponding "A" and "B" numeric value for a given, single code. I would multiply P with B for each single code with the same first four digits, and then sum each of those results. Then I would divide that sum into the sum of all the "B" values for those same individual codes.

I want to create a single 3-line chart for each unique group of codes that begin with the same four digits. As suggested in the comments, it makes sense to have the "P" values as a secondary axis, and use the primary axes for "A" and "B".

These codes will change, and new dates and associated values will be added every week, so I'm looking to have this chart be dynamic as the raw data gets added to/altered.

Is there any way to perform calculations like this for a chart in Excel? I think I'm familiar enough with VBA and Excel's formulae to do this, but I guess I'll find out.

Thank you All,

Angus

QHarr
  • 83,427
  • 12
  • 54
  • 101
Angus Ryer
  • 114
  • 2
  • 10
  • I would even have settled for an embedded hand drawing of the chart on this one. I am a little confused. – QHarr Feb 25 '18 at 18:24
  • For a given date, for example, you have two B columns (I assume you are referring to the letter under the date). The only thing distinguishing these is apparently the colour (blue/yellow) and the order they appear. You make no reference to which of these would be used for the calculation of P and do not show values for A that would enable me to work it out for myself. Please see [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). I think this question needs clarifying before a decent answer can be given either way. – QHarr Feb 25 '18 at 18:32
  • FWIW...You would likely plot P on a secondary axis due to it being a percentage and of a different scale to A & B. Which you seem to be getting at later in the question but before that you have the confusing statement, IMHO, of ...The vertical left axis would have "B" at the top, then "A", then "P". I'm thinking perhaps you mean a "side ways/horizontal bar chart" - in which case you would need separate charts and fake them being part of the same chart. – QHarr Feb 25 '18 at 18:37
  • Sorry - a macro I ran changed the column names so that there were two "B" columns - edited. You're right, the way I worded this is confusing, now that I read your comment. I'm going to hand draw the chart that I'm envisioning and post that, as you suggest - haha, that's a good idea. – Angus Ryer Feb 26 '18 at 01:54
  • Thanks QHarr - I've edited my post and -hopefully - clarified a few things. – Angus Ryer Feb 26 '18 at 03:59
  • Is p a fixed percentage? Why then plot on the chart? A worked example of "P" is a % value ...........to show how you get to the values you actually want to plot on the chart % series would help. – QHarr Feb 26 '18 at 11:14
  • Is the first p value on your chart 25.35%? – QHarr Feb 26 '18 at 11:32
  • Exactly. I'll work out an example using the two "P" values for the code beginning with "0905": 30% x 7777 + 20% x 6750 = 2333.1 + 1350 = 3683.1 Then: 3683.1 / (7777 + 6750) = 25.35% What I'm charting here is a measured percentage of progress made on a task against the number of hours it took to complete that percentage of progress. So, I'd like to compare these calculated percentage values against the percentage of hours/budget ("A" values / "B" values). The "P" values _are_ fixed, but they're a measured value, so I need to retain their relative weight. – Angus Ryer Feb 26 '18 at 13:04
  • Yeah. I worked it out! I sort of have a solution I am tinkering with but not the prettiest. The main problem is you actually have to flip your data around. The formulas bit isn't a problem. Are there a fixed number of columns or do you just keep adding? Are you allowed to change the layout format? – QHarr Feb 26 '18 at 13:07
  • Thanks for the help QHarr! I am allowed to change anything to make it work. It's possible that on a project the dates would get extended (as with most projects it seems), but I set up a formula that automatically populates the columns with the dates, so if they get added to, I would have months to figure it out, haha. For flipping the data: I'm guessing I should have a "P", "A" and "B" row for each date along the left, and the codes along the top? Actually... that's starting to make more sense already... – Angus Ryer Feb 26 '18 at 14:00
  • Well, the solution I am about to post is using the format you currently have. Don't cry! But it does include many of the elements you will need going forward. – QHarr Feb 26 '18 at 14:23

1 Answers1

2

2 sheet set-up.

1) Sheet1 is called DataSheet and mirrors your data input sheet.

2) Sheet2 is called ChartData and has both the transformed dataset for plotting your chart and the chart itself. This is how you actually want your dataset for charting. So if you can change the format of your reporting then always try and make a 'flat-file' table arrangement as shown.

Here is what is looks like in the "after" state:

2 sheets

DataSheet sheet:

Cell B2 in dataSheet you can input the 4 digit code to plot the chart for.

Note the leading ' to preserve the 0 at the start.

4 digit code selection

Formulas in row 4:

A) F4 for p:

=IFERROR(SUMPRODUCT(OFFSET($A$6,0,SUM(COLUMN(H1)-1),COUNTA(Codes),1),--(LEFT(Codes,4)=$B$2),OFFSET($A$6,0,SUM(COLUMN(F1)-1),COUNTA(Codes),1))/H4,"")

This applies the p calculation

B) G4 for A:

=SUMPRODUCT(OFFSET($A$6,0,SUM(COLUMN(G1)-1),COUNTA(Codes),1),--(LEFT(Codes,4)=$B$2))

This sums, for the given 4 digit start code, column G.

C) H4 for B:

=SUMPRODUCT(OFFSET($A$6,0,SUM(COLUMN(H1)-1),COUNTA(Codes),1),--(LEFT(Codes,4)=$B$2))

This sums, for the given 4 digit start code, column H.

You can copy paste these in blocks of 3 to set the formulas for future ranges i.e. select F4:H4 and copy to I4:K4 etc and formulas will work for new range. Sorry, I haven't yet adjusted these so can drag in some way.

Shout out to @Tom Sharpe and @shrivallabha.redij for solving the sumproduct riddle, here, which forms part of the solution.

ChartData Sheet:

A) Formula in B4:

=DataSheet!F2

This ensures that the start date is set to the first date in your range.

B) Formula in B5 to drag down column B:

=IF(ROWS($B$4:$B5) <=INT(COLUMNS(dates)/3),$B$4+ROWS($B$5:$B5)*7,NA())

This ensures the date increments weekly (i.e. plus 7 days) for the required number of weeks. As each date is repeated 3 times, the number of columns in the dynamic range dates, which covers all the dates in the row 2 of dataSheet, is divided by 3 to get the number of weeks which should actually be plotted. If this number is exceeded #N/A is outputted as this will not be plotted on the chart.

C) Formula in C4, which is dragged down column C, and across for as many columns as you are using i.e. to column E in the shown example:

 =IFNA(INDEX(SOquestions.xlsb!dataRange,MATCH($B4,dates,0)+COLUMNS($E:E)-1),NA())

This retrieves the p, A and B values for the column B date. dataRange is the dynamic range which holds your source calculations in dataSheet. If you open the Name Manager, select dataRange, and then put your cursor inside the Refers To range, the dynamic range it references is outlined by "marching ants":

dataRange

If the date column (B) is #N/A in the chartData sheet then the associated p,A and B default to #N/A so are not plotted.

Plotting the chart:

The chart itself is a combination chart that has 4 dynamic series.

ChartSeries

The series are added in the normal way but you reference the dynamic series preceded by Sheetname! ; as in image. You use pSeries,aSeries, bSeries and dateSeries for the plot.

The chart axis should be set up to handle weekly data points.

X axis

Combo-chart set-up:

Combo chart

Note p is plotted on the secondary x-axis and formatted as percentage.

Process for updating:

1) Add new rows in dataSheet

Adding new rows

2) Copy block of three columns to next column. Note that the first column of the block has a +7 to increment the week

Copying formula and dates block

3) Goto ChartData and drag formulas down in columns B:E

chartDataSheet update

4) Select the code you are interested in, in dataSheet cell B2 and everything updates.

Named ranges required:

Dynamic named ranges used. Ctrl & F3 will bring up Name Manager so you can add these.

Named ranges

Formulas for the dynamic named ranges:

Codes

=OFFSET(DataSheet!$A$6,0,0,COUNTA(DataSheet!$A$6:$A$1048576),1)

dataRange

=OFFSET(DataSheet!$F$4,0,0,1,COUNT(DataSheet!$F$2:$XFD$2))

dates

=OFFSET(DataSheet!$F$2,0,0,1,COUNT(DataSheet!$F$2:$XFD$2))

pSeries

=OFFSET(DataSheet!$P$11,0,0,COUNT(DataSheet!$B$11:$B$1048576),1)

aSeries

=OFFSET(ChartData!$D$4,0,0,COUNT(ChartData!$B$11:$B$1048576),1)

bSeries

=OFFSET(ChartData!$E$4,0,0,COUNT(ChartData!$B$11:$B$1048576),1)

dateSeries:

=OFFSET(ChartData!$B$4,0,0,COUNT(ChartData!$B$11:$B$1048576),1)

Note: For earlier versions of Excel end row and columns have to be adjusted

End Row 1048576 becomes 65536 End Column XFD becomes IV

Further version info:

http://www.excelfunctions.net/Excel-2003-vs-Excel-2007.html

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • I renamed the file for the upload as converted to xlsx so no longer has same name as shown in one of the answer images. – QHarr Feb 26 '18 at 15:52
  • Qharr - I am breathless. Your answer is so thorough and clear. I am both very impressed at your knowledge and skill, as well as _really_ thankful for the time you've spent to help me out. This definitely solves my issue. I've read through everything, and I'm pretty sure I can implement this. Thanks for demonstrating to me what a great answer is! – Angus Ryer Feb 26 '18 at 16:19
  • Was a pleasure as you took the time to modify your question and actually included some hand drawings :-) – QHarr Feb 26 '18 at 16:30