VBA code for algebraic calculation depending on the criteria - vba

I want macro for:
I have 5 columns where I want like
COL 1 COL 2 COL 3 COL 4 COL5
JAN 13 0 1
JAN 12 8 7
FEB 13 7 4
FEB 14 7 5
MAR 44 10 7
now i want to calculate on col 5 = (sum of col2 values ) / (sum of col 3 values- sum of values col 4) which belongs to JAN and i want this solution to print on 1 cell of COL5
similarly for other months output should print on first cell belongs to FEB cell in col 1
Please i am very poor in this macro coding part . can someone help me to decode this one.
For better understanding of my problem i attaching image also

Put this in E2 and copy/drag down.
=IF(A2<>A1,IFERROR(SUMIF(A:A,A2,B:B)/(SUMIF(A:A,A2,C:C)-SUMIF(A:A,A2,D:D)),0),"")
You will want to change the ,0 to what every you want in place of when it tries to divide by 0.
Jan resolves to 25/0 which is an error I put 0 in for the error.

Related

Can I reference a prior row's value and populate it in the current row in a new column?

I have the following data frame:
Month
Day
Year
Open
High
Low
Close
Week Close
Week
0
1
1
2003
46.593
46.656
46.405
46.468
45.593
1
1
1
2
2003
46.538
46.66
46.47
46.673
45.593
1
2
1
3
2003
46.717
46.781
46.53
46.750
45.593
1
3
1
4
2003
46.815
46.843
46.68
46.750
45.593
1
4
1
5
2003
46.935
47.000
46.56
46.593
45.593
1
...
...
...
...
...
...
...
...
...
7257
10
26
2022
381.619
387.5799
381.350
382.019
389.019
43
7258
10
27
2022
383.07
385.00
379.329
379.98
389.019
43
7259
10
28
2022
379.869
389.519
379.67
389.019
389.019
43
7260
10
31
2022
386.44
388.399
385.26
386.209
385.24
44
7261
11
1
2022
390.14
390.39
383.29
384.519
385.24
44
I want to create a new column titled 'Prior_Week_Close' which will reference the prior week's 'Week Close' value (and the last week of the prior year for the first week of every year). For example, row 7260's value for Prior_Week_Close should equal 389.019
I'm trying:
SPY['prior_week_close'] = np.where(SPY['Week'].shift(1) == (SPY['Week'] - 1), SPY['Week_Close'].shift(1), np.nan)
TypeError: boolean value of NA is ambiguous
I thought about just using shift and creating a new column but some weeks only have 4 days and that would lead to inaccurate values.
Any help is greatly appreciated!
I was able to solve this by creating a new column called 'Overall_Week' (the week number in the entire data set, not just the calendar year) and using the following code:
def fn(s):
result = SPY[SPY.Overall_Week == (s.iloc[0] - 1)]['Week_Close']
if result.shape[0] > 0:
return np.broadcast_to(result.iloc[0], s.shape)
else:
return np.broadcast_to(np.NaN, s.shape)
SPY['Prior_Week_Close'] = SPY.groupby('Overall_Week')['Overall_Week'].transform(fn)```

Calculate new column using relative row references

I would like to turn a data frame like this:
DF
Nrow
a
1
5
2
6
3
7
4
11
5
16
Into this:
DF
Nrow
a
b
1
5
NA
2
6
NA
3
7
2
4
11
5
5
16
9
Column 'b' is calculated as the value from column 'a' minus another value from column 'a', in [row-2]. For example b4 = a4-a2.
I have had no success so far with indexing or loops. Is there a tool or command for this or some obvious notation that I am missing? I need to do this continuously without splitting into groups.

SQL Insert data into multiple tables from data table that contains table name, column name and value column mapping can change in source

I am perplexed. This looks possible but unsure how to accomplish this.
I have a data table that looks like this. The data source will sometimes
swap columns in the source file.
Data
IN column table_sheet y w value
col1 Route Summary 2021 Week 1 a
col2 CNG/Diesel Summary 2021 Week 1 5
col3 Freq Summary 2021 Week 1 B
col4 Weekly Miles Summary 2021 Week 1 6
col1 CNG/Diesel Summary 2021 Week 2 1
col2 Freq Summary 2021 Week 3 1
col3 Weekly Miles Summary 2021 Week 4 1
col1 Load Days_ON 2021 Week 1 L210224-25048
col2 Load_id Days_ON 2021 Week 1 L210224-25048
col3 cost Days_ON 2021 Week 1 263.64
col1 Distance CCD 2021 Week 2 781.62
col2 Code CCD 2021 Week 2 CL
col3 Name CCD 2021 Week 2 Squre
col4 Cost CCD 2021 Week 2 1800
The source file is a spreadsheet with multiple sheets (Table name for the project) that is populated weekly and is incremental.
The source file column mapping can change as in table Summary, all columns(excel source file column index) from the source changed . The destination tables have all possible column names already in place. The SQL code must read the table name and map the data to the source table accordingly.
I want to populate the below tables using the data table above like this.
Summary Route CNG/Diesel Freq Weekly Miles y W
a 5 B 6 2021 Week 1
1 2021 Week 2
1 2021 Week 3
1 2021 Week 4
Days_ON
Load LoaD_id cost y W
L210224-25048 L210224-25048 263.64 2021 Week 1
CCD
Distance Code Name Cost y W
781.62 CL Squre 1800 2021 Week 1
ADDED-
The solution may work for one row but it is not working for multiple rows in same week.
Data
IN column table_sheet y w value h1_ind
col1 Route Summary 2021 Week 1 a 2
col2 CNG/Diesel Summary 2021 Week 1 5 3
col3 Freq Summary 2021 Week 1 B 4
col4 Weekly Miles Summary 2021 Week 1 6 5
col1 Route Summary 2021 Week 1 b 2
col2 CNG/Diesel Summary 2021 Week 1 1 3
col3 Freq Summary 2021 Week 1 1 4
col4 Weekly Miles Summary 2021 Week 1 1 5
col1 Route Summary 2021 Week 1 c 2
col2 CNG/Diesel Summary 2021 Week 1 5 3
col3 Freq Summary 2021 Week 1 B 4
col4 Weekly Miles Summary 2021 Week 1 6 5
col1 Route Summary 2021 Week 1 d 2
col2 CNG/Diesel Summary 2021 Week 1 1 3
col3 Freq Summary 2021 Week 1 1 4
col4 Weekly Miles Summary 2021 Week 1 1 5
Desired output:
table_sheet y w Route CNG/Diesel Freq Weekly Miles
Summary 2021 Week 1 a 5 B 6
Summary 2021 Week 1 b 1 1 1
Summary 2021 Week 1 c 5 B 6
Summary 2021 Week 1 d 1 1 1
If possible how could SQL unpivot provide this output? I would like to see this.
I would also like to see if we can get spoon to work.
Please assist.
You'll need to separate your rows by table_sheet and afterwards apply a Row denormaliser for each table_sheet, because your producing different tables:

Pivot on multiple fields and export from Access

I have built an access application for a manufacturing plant and have provided them with a report that lists different data points along a process. I have a way to generate a report that looks like the following.
Batch Zone Value1 Value 2 etc.
25 1 5 15
25 2 12 31
26 1 6 14
26 2 10 32
However, there is demand to view the data in a different format. They would like one line per batch, with all data horizontal. Like this...
Zone 1 Zone 2
Batch Value1 Value2 Value1 Value2
25 5 15 12 31
26 6 14 10 32
In all there will be 157 columns, if displayed as in the second example. There are 7 unique field names, but the rest are 14 different data types that are repeated. I can't get a query to display the data in the format the they want, do to the fact that the field names are the same, but it is not hard to do it the first way. I can use VBA to insert the data into a table, but I can't use duplicate field names, so when I go to export this to Excel the field names won't mean anything, and there can't be sections (like zone1, zone2, etc.) I can link a report to this, but the report width can only be 22", so I would have to export and then do some vba handling of the excel sheet on the other end to display in a legible way.
I can get the data into format #1, is there some way I can get the data to display in one long row based on batch number? Does anyone else have a great idea of how this is doable?
Open to any suggestions. Thanks!
In your question you say that
I have a way to generate a report that looks like the following
and then list the data as
Batch Zone Value1 Value2
----- ---- ------ ------
25 1 5 15
25 2 12 31
26 1 6 14
26 2 10 32
Now perhaps the data may already be in "un-pivoted" form somewhere (with different Values in separate rows), but if not then you would use something like the following query to achieve that
SELECT
[Batch],
"Zone" & [Zone] & "_" & "Value1" AS [ValueID],
[Value1] AS [ValueValue]
FROM BatchDataByZone
UNION ALL
SELECT
[Batch],
"Zone" & [Zone] & "_" & "Value2" AS [ValueID],
[Value2] AS [ValueValue]
FROM BatchDataByZone
...returning:
Batch ValueID ValueValue
----- ------------ ----------
25 Zone1_Value1 5
25 Zone2_Value1 12
26 Zone1_Value1 6
26 Zone2_Value1 10
25 Zone1_Value2 15
25 Zone2_Value2 31
26 Zone1_Value2 14
26 Zone2_Value2 32
However you get to that point, if you save that query as [BatchDataUnpivoted] then you could use a simple Crosstab Query to "string out" the values for each batch...
TRANSFORM Sum(BatchDataUnpivoted.[ValueValue]) AS SumOfValueValue
SELECT BatchDataUnpivoted.[Batch]
FROM BatchDataUnpivoted
GROUP BY BatchDataUnpivoted.[Batch]
PIVOT BatchDataUnpivoted.[ValueID];
...returning...
Batch Zone1_Value1 Zone1_Value2 Zone2_Value1 Zone2_Value2
----- ------------ ------------ ------------ ------------
25 5 15 12 31
26 6 14 10 32

excel formula to display right column

I need an excel formula for this to produce the values on the right column. simple theory. to show when right values has jump by a certain value.
10 0
10 1
11 0
11 1
12 0
12 0
12 0
12 1
13 0
13 0
If I understand correctly, you want 1 when the next value is different, and zero otherwise?
If so, assuming your values are in column A, starting with A1, try the following formula in B1: =IF(A1=A2;0;1) then copy B1 and paste in the whole column B.
Make that =IF(ISBLANK(A2);0;IF(A1=A2;0;1)) to also return 0 if the next cell is blank.