Test for change in month from list of dates - vba

I want to code something in VBA that could identify each time there's a change in month, for example from January to February.
In the below example you can see I want to take the input from column B, and output to column C. The output should be:
Test when a month change occurs.
Train when the month is the same as before.
Example data:
A B C
1 29/12/2006 Train
2 01/01/2007 Test
3 02/01/2007 Train
4 03/01/2007 Train
5 04/01/2007 Train
6 05/01/2007 Train
..
100 01/07/2007 Test

Here's another way. A1-Day(A1) will always return the last day of the preceding month. So:
B2: =IF((A1-DAY(A1))=(A2-DAY(A2)),"Train","Test")

Something like this would probably do the job:
=IF(And(Month(A4)=Month(A3);Year(A4)=Year(A3));"Train";"Test")
It compares months for A4 and A3 and the years.

Related

Create chart where the columns of the data set are the categories - Report Builder 3.0

I have a very simple chart that I am wanting to add but I can't for the life of me figure it out. The chart is referencing a dataset that returns data like this. It is calculating the sum of each Location and then using Rollup to produce a Total Count for each Week Column
Location CurrentWeek PreviousWeek 2WeeksAgo
======== =========== =========== ===========
North 5 6 3
South 4 3 1
East 8 2 3
West 2 7 0
Total 19 18 7
What I am wanting to do is have the X Axis (horizontal) represented by the CurrentWeek, PreviousWeek, 2WeeksAgo columns and plot the "Total" values from each respective column.
Adding Snip...
Sample Chart
Thanks for adding the image.
So we have a few steps to get to where we need to be - first, we need to transform the data into a format that's easier and more scalable to work with (if we ever add a "3 weeks ago" column, we don't want to have to rework everything). The desired format is:
Date Amount
Current Week 19
1 week ago 18
2 weeks ago 17
Personally - instead of naming stuff "current week", "1 week ago" etc., I would have a WeeksPrior column where 0 would mean the current week, 1 would mean a week ago and so on.
Anyways, to get from your sample table to the more standardized input, we have to use an unpivot (these always hurt my brain, but the docs have some good examples you can use).
SELECT
*
, ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) AS Ordinal
--this is hacky, but ordering by (select null) allows us to assign a row number by the default order
FROM (SELECT 'Total' AS Location, 19 AS CurrentWeek, 18 AS PreviousWeek, 7 AS [2WeeksAgo]) x
--This is the test data, replace this with your actual query
UNPIVOT (Value FOR Date IN ([CurrentWeek], [PreviousWeek], [2WeeksAgo])) y
--This unpivots the test data, converting the separate columns into a single [Date] column, and assigning the values to the [Value] column.
This will spit out the following:
Location Value Date Ordinal
Total 19 CurrentWeek 1
Total 18 PreviousWeek 2
Total 7 2WeeksAgo 3
From here, we add the data to the chart. This is pretty straighforward, but there are a few "gotchas" to be wary of.
First, we'll add the Value column as a chart value, and the Ordinal column as a category group.
Let's see what the chart looks like right now by running the report.
Well, it's getting there, but we want our labels on the bottom. To do this, we go into the Ordinal category group's properties and switch the label to the date column. Make sure you're still sorting by Ordinal, since SSRS doesn't know what "1 week ago" means relative to "Current Week", and will sort alphabetically or randomly if you don't tell it to sort by ordinal.
We can also clean up the chart a bit by removing the legend and changing the major tick mark line style to "solid" on the horizontal axis., leaving us something that looks like this:
Adding a label to the vertical axis would probably also help readability, as would adding hover-text to the points on the chart.

Calculate average VBA from dynamic table

I ve a table on Excel with the week number and the weight (in Kg.)
Somebody can insert his weight every day during a week or just once or not at all. I can't manage that.
Then my table can literally change. from zero to 7 even more lines a week (like the yellow side of the image).
What I wanna do is to calculate the weight average per week. and then I will have one line for each week, when i got at least one weight (sometimes I won't have any line). We can have week without any weight so then I don't want this line at all. We can also easily have a weight for the week 2 but between the weeks 5 and 6 in the yellow table. That would happen if someone insert his weight after others.
How can I say this two weeks are similar, so we calculate the average for this two weight ?
I hope it's enough clear with this picture
Use formula below in Column C to calculate average(assume Week in column A and Weight in column B)
=AVERAGEIF(A:A,A2,B:B)
Average Column Copy->PasteSpecial value only,
then Remove Duplicates base on Week and the new Average Column

Calculation for month number in time series data

The data I am working with is oil and gas production data. The production table uniquely identifies each well and contains a time series of production values. I want to be able to calculate a column that contains the month number occurrence of production for every well in the production table. This needs to be a calculation, so I can graph the production for various wells based on the production month, not the calendar month. (I want to compare well performance across wells over the life of wells.) Also note that there could be gaps in the production data so you can't depend on having twelve months of sequential production for each well.
I tried using the answer in this postRankValues but the calculation would never finish. I have over 4 million rows of production data.
In the table shown below, the values shown in ProdMonth is what I need to calculate based on their time occurrence shown in ProdDate. This needs to be performed as a row calculation for each unique WellId
Thanks.
WellID ProdDate ProdMonth
1 12/1/2011 1
1 1/1/2012 2
1 2/1/2012 3
1 3/1/2012 4
… … …
1 11/1/2012 12
2 3/1/2014 1
2 4/1/2014 2
2 5/1/2014 3
2 6/1/2014 4
2 7/1/2014 5
… … …
2 2/1/2014 12
I would create a new date table that has a row for each day (the granularity of your data). I would then add to that table the ProdMonth column. This will ensure you have dates for all days (even if there are gaps in the well reporting data). Then you can use a relationship between the well production data and the Date table on the ProdDate field. Then if you pull in the ProdMonth from the date table, you'll have a list of all of the ProdMonths (hint: you may need to select 'show values with no data' on the field right click menu in the fields well). Then if you add to the same visualization WellID you should be able to see which wells were active in which ProdMonth. If WellID is a number, you might need do use the 'do not summarize' feature on the WellID to get the result you desire.
I posted this question on the PowerPivotPro and Tom Allan provided the DAX formula I needed. First step was to calculate a field that concatenated Year and Month (YearMonth). Then utilized the RANKXX function as such:
= RANKX ( FILTER ( Data, [WellID] = EARLIER ( [WellID] ) ), [YearMonth], , 1, DENSE )
That did the trick and performed fairly quickly on 12mm rows.

Excel logic - matching columns

My situation is the following:
The 'Day' column is 1 to 365, and each day is repeated 24 times (for the 24 hours). Each day and hour together have a Degree value, represented by the B column. I need to multiple the B column with the E column everytime A column matches D (eg. B2*E2, B3*E2, B4*E2, B7*E3).
How can I do that? I'm really confused here.
Thank you
Using your shown example, in cell C2 and copied down:
=B2*VLOOKUP(A2,$D$2:$E$366,2,FALSE)
Two ways:
VLOOKUP(A2,$D$2:$E$366,2,FALSE)*B2
SUMPRODUCT(B2*($D$2:$D$366=A2)*$E$2:$E366)

SQL change over time query

I have created 2 tables. one table has 4 fields. a unique name, a date and 3 figures. The second table contains the same fields but records the output of a merge function. therefore has a date at which time the update or insert function happened. what I want to do is retrieve a sum of either the difference between 2 days or alternatively the totals of the 2 days to work out how much the value has changed over the day. The merge function only updates if a value has changed or it needs to insert a new value.
so far I have this
select sum(Change_Table_1.Disk_Space) as total,
Change_Table_1.Date_Updated
from VM_Info
left join Change_Table_1
on VM_Info.VM_Unique = Change_Table_1.VM_Unique
where VM_Info.Agency = 'test'
group by Change_Table_1.Date_Updated
but this would just return the sum of that days updated total rather than the difference between the two days. One answer to this question would be to to add all new records to the table but this would contain a number of duplicates. So in my head what I want it to do is loop over the current figures for the day then loop over the next day but also to include all values that haven't updated. sorry if I haven't explained this well. so what I want to achieve is to get some sort of change of the total over time. If its poor design im in a position to accept that also.
Any help is much appreciated.
maybe this would explain it better. show me total for day 1, if the value hasn't changed then show me the same value for day 2 if it has changed show me new value. and so on...
ok to further elaborate.
the Change_Table looks like
vm date created action value_Field1 value_field_2 Disk_Space
abc 14/10/2013 insert 5 5 30
def 14/10/2013 insert 5 5 75
abc 15/10/2013 update 5 5 75
so the out put I want is for the 14th the total for the last column is 105. On the 15th abc has changed from 30 to 75 but def hasn't changed but still neds to be included giving 150
so the output would look like
date disk_Space
14/10/2013 105
15/10/2013 150
Does this help? If not, can you provide a few rows of sample data, and an example of the desired result?
select
(VM_Info.Disk_Space - Change_Table_1.Disk_Space) as DiskSpaceChange,
Change_Table_1.Date_Updated
from
VM_Info
left join Change_Table_1 on VM_Info.VM_Unique = Change_Table_1.VM_Unique and VM_Info.Date = Change_Table_1.Date_Updated
where
VM_Info.Agency = 'test'