how to group dates from ms access database as week of month using excel vba - vba

I am using MS access 2010 database and working with Excel VBA to connect to the database and make queries. Suppose I have a table named "MyTable" like this below:
----------------------
| Date | Count |
----------------------
|7/7/16 | 12 |
----------------------
|7/8/16 | 15 |
----------------------
|7/15/16 | 18 |
----------------------
|7/18/16 | 16 |
----------------------
|8/7/16 | 15 |
----------------------
|8/8/16 | 10 |
----------------------
|8/15/16 | 9 |
----------------------
|8/16/16 | 18 |
----------------------
Now I want to use query to get a table like this:
----------------------
|Week by Month | Sum |
----------------------
|July Week 2 | 27 |
----------------------
|July Week 3 | 18 |
----------------------
|July Week 4 | 16 |
----------------------
|Aug Week 2 | 25 |
----------------------
|Aug Week 3 | 27 |
----------------------

Use DatePart to get the week of the year, then subtract the week of the first day of the month (zero based week of the month) and then add 1 (to get to a one based week of the month:
Public Function WeekOfMonth(x As Date) As Integer
WeekOfMonth = DatePart("ww", x) - _
DatePart("ww", DateSerial(Year(x), Month(x), 1)) _
+ 1
End Function
Note that the Access SQL version should be idential to what's after the = sign.

I have solved this as below:
select weeknum, sum(count1) from (
select format(date1,'MMM') & " Week - " & int((datepart('d',date1,1,1) -1 ) / 7 + 1) as weeknum, count1 from MyTable)
group by weeknum

Show Week of Month where Week 1 is always the 1st Full Week of the Month starting in that month (First Sunday is 1 or 2 or 3 or 4 or 5 or 6 or 7), days of the month prior to the first Sunday are counted as week 4/5 of previous month.
After searching and failing to find EXACTLY the right answer for my situation - I modified ComIntern's solution as follows. This is used a CONTROL on a REPORT, where [StartDate] is a criteria on the form that calls/generates the report:
=IIf((DatePart("ww",[StartDate]-7)-DatePart("ww",DateSerial(Year([StartDate]-7),Month([StartDate]-7),1))+1)="5","1",DatePart("ww",[StartDate])-DatePart("ww",DateSerial(Year([StartDate]),Month([StartDate]),1))+0)
This results in showing the Week of Month based on FULL weeks - and accounts for when the previous month's week 5 included 1 or more days from this month.
For example - Week 5 of Oct 2017 is 29 OCT - 04 NOV. If I did not include the IIF statement to adjust the formula, 05-11 NOV is returned as Week 2, but for my reporting purposes it is Week 1 of NOV. I have tested this out and appears to ALWAYS work, if you need to see Week of Month, based on FULL weeks, this should work for you!

Related

Review scripts which are older than 3 months and in the last 30 days

I'm try to run a query that will allow me to see where we have scripts running that are older than 3 months old over the last 30 days delivery, so we know they need to be updated.
I have been able to build the query to show me all the scripts and their last regen dates (with specific dates put in) but can't work out;
How to look at only the last 30 days data.
How to see only the scripts where the date_regen column is older than 3 months from today's date - From the last 30 days data that I'm reviewing.
EXAMPLE TABLE
visit_datetime | client | script | date_regen |
2019/10/04 03:32:51 | 1 | script1 | 2019-09-17 13:12:01 |
2019/09/27 03:32:52 | 2 | script2 | 2019-07-18 09:44:02 |
2019/10/06 03:32:50 | 3 | script3 | 2019-03-18 14:08:02 |
2019/10/02 06:28:24 | 4 | script6 | 2019-09-11 10:02:01 |
2019/03/01 06:28:24 | 5 | script7 | 2019-02-11 10:02:01 |
The below examples haven't been able to get me what I need. My idea was that I would get the current date (using now()) and then knowing that, look at all data in the last 30 days.
After that I would then WHERE month,-3 (so date_regen 3 months+ old from the current date.
However I can't get it to work. I also looked at trying to do -days but that also had no success.
-- WHERE MONTH = MONTH(now()) AND YEAR = YEAR(now())
-- WHERE date_regen <= DATEADD(MONTH,-3,GETDATE())
-- WHERE DATEDIFF(MONTH, date_regen, GetDate()) >= 3
Code I am currently using to get the table
SELECT split_part(js,'#',1) AS script,
date_regen,
client
FROM table
WHERE YEAR=2019 AND MONTH=10 AND DAY = 01 (This where is irrelevant as I would need to use now() but I don't know what replaces "YEAR/MONTH/DAY ="
GROUP BY script,date_regen,client
ORDER BY client DESC;
END GOAL
I should only see client 3 as clients 1+2+4 have tags where the date_regen is in the last 3 months, and client 5 has a visit_datetime out of the 30 limit.
visit_datetime | client | script | date_regen |
2019/10/06 03:32:50 | 3 | script3 | 2019-03-18 14:08:02 |
I think you want simple filtering:
select t.*
from t
where visit_datetime >= current_timestamp - interval 30 day and
date_regen < add_months(current_timestamp, -3)

Left join with nested selects and aggregate functions

Problem
I have one table of generated dates (s) which I want to join with another table (d) which is a list of dates where a specific occurrence has happened.
table s
Wednesday 23rd August 2017
Thursday 24th August 2017
Friday 25th August 2017
Saturday 26th August 2017
table d
day_created -------------------------------- count
Thursday 24th August 2017 ---------------- 45
Saturday 26th August 2017 ---------------- 32
I want to show rows where the occurrence does not take place, which I cannot do if I just have table d.
I want something that looks like:
day_created -------------------------------- count
Wednesday 23rd August --------------------- 0
Thursday 24th August 2017 ---------------- 45
Friday 25th August 2017 ------------------ 0
Saturday 26th August 2017 ---------------- 32
I've tried joining with a left join as follows:
SELECT day_created, COUNT(d.day_created) as total_per_day
FROM
(SELECT date_trunc('day', task_1.created_at) as day_created
FROM task_1
)
d
LEFT JOIN (
SELECT (generate_series('2017-05-01', current_date, '1 day'::INTERVAL)) as standard_date
)
s
ON d.day_created=s.standard_date
GROUP BY d.day_created
ORDER BY day_created DESC;
I don't get an error however the join isn't working (i.e. it doesn't return dates where the count is null). What it returns is the dates from table d and the count, but not the dates in between where there are 0 occurrences.
I've been going round in circles and have understood that I need to make table s (I think!) the left table, but I'm getting confused as a newbie with the syntax.
This is all in PostgreSQL 9.5.8.
Basically, you had the LEFT JOIN backwards. This should work, with some other simplifications and performance optimizations:
SELECT s.standard_date, COUNT(d.day_created) AS total_per_day
FROM generate_series('2017-05-01', current_date, interval '1 day') s(standard_date)
LEFT JOIN task_1 d ON d.day_created >= s.standard_date
AND d.day_created < s.standard_date + interval '1 day'
GROUP BY 1
ORDER BY 1;
This counts rows in d, like you commented. Does not sum values.
Be aware that generate_series() still returns timestamp with time zone, even if you pass date values to it. You may want to cast to date or format with to_char() for display in the outer SELECT. (But rather group and order by the original timestamp value, not the formatted string.)
There may be corner cases depending on the current time zone setting depending on the actual undisclosed table definition.
Related:
How to avoid a subquery in FILTER clause?
I have one table of generated dates (s)
In real databases, we don't store a generated series. We just generate them when needed.
which I want to join with another table (d) which is a list of dates where a specific occurrence has happened. [...] I want to show rows where the occurrence does not take place, which I cannot do if I just have table d.
Nah, you can do it.
CREATE TABLE d(day_created, count) AS VALUES
('24 August 2017'::date, 45),
('26 August 2017'::date, 32);
SELECT day_created, coalesce(count,0)
FROM (
SELECT d::date
FROM generate_series(
'2017-08-01'::timestamp without time zone,
'2017-09-01'::timestamp without time zone,
'1 day'
) AS gs(d)
) AS gs(day_created)
LEFT OUTER JOIN d USING(day_created)
ORDER BY day_created;
day_created | coalesce
-------------+----------
2017-08-01 | 0
2017-08-02 | 0
2017-08-03 | 0
2017-08-04 | 0
2017-08-05 | 0
2017-08-06 | 0
2017-08-07 | 0
2017-08-08 | 0
2017-08-09 | 0
2017-08-10 | 0
2017-08-11 | 0
2017-08-12 | 0
2017-08-13 | 0
2017-08-14 | 0
2017-08-15 | 0
2017-08-16 | 0
2017-08-17 | 0
2017-08-18 | 0
2017-08-19 | 0
2017-08-20 | 0
2017-08-21 | 0
2017-08-22 | 0
2017-08-23 | 0
2017-08-24 | 45
2017-08-25 | 0
2017-08-26 | 32
2017-08-27 | 0
2017-08-28 | 0
2017-08-29 | 0
2017-08-30 | 0
2017-08-31 | 0
2017-09-01 | 0
(32 rows)

DAX SUMMARIZE() with filter - Powerpivot

Rephrasing a previous question after further research. I have a denormalised hierarchy of cases, each with an ID, a reference to their parent (or themselves) and a closure date.
Cases
ID | Client | ParentMatterName | MatterName | ClaimAmount | OpenDate | CloseDate
1 | Mr. Smith | ABC Ltd | ABC Ltd | $40,000 | 1 Jan 15 | 4 Aug 15
2 | Mr. Smith | ABC Ltd | John | $0 |20 Jan 15 | 7 Oct 15
3 | Mr. Smith | ABC Ltd | Jenny | $0 | 1 Jan 15 | 20 Jan 15
4 | Mrs Bow | JQ Public | JQ Public | $7,000 | 1 Jan 15 | 4 Aug 15
After the help of greggyb I also have another column, Cases[LastClosed], which will be true if the current row is closed, and is the last closed of the parent group.
There is also a second table of payments, related to Cases[ID]. These payments could be received in parent or child matters. I sum payments received as follows:
Recovery All Time:=CALCULATE([Recovery This Period], ALL(Date_Table[dateDate]))
I am looking for a new measure which will calculate the total recovered for a unique ParentMatterName, if the last closed matter in this group was closed in the Financial Year we are looking at - 30 June end date.
I am now looking at the SUMMARIZE() function to do the first part of this, but I don't know how to filter it. The layers of calculate are confusing. I've looked at This MSDN blog but it appears that this will filter to only show the total payments for that matter that was last closed (not adding the related children).
My current formula is:
Recovery on Closed This FY :=
CALCULATE (
SUMX (
SUMMARIZE (
MatterListView,
MatterListView[UniqueParentName],
"RecoveryAllTime", [Recovery All Time]
),
[RecoveryAllTime]
)
)
All help appreciated.
Again, your solution is much more easily solved with a model addition. Remember, storage is cheap, your end users are impatient.
Just store in your Cases table a column with the LastClosedDate of every parent matter, which indicates the date associated with the last closed child matter. Then it's a simple filter to return only those payments/matters that have LastClosedDate in the current fiscal year. Alternately, if you know for certain that you are only concerned with the year, you could store just LastClosedFiscalYear, to make your filter predicate a bit simpler.
If you need help with specific measures or how you might implement the additional field, let us know (I'd recommend adding these fields at the source, or deriving them in the source query rather than using calculated columns).

How to calculate the broadcast year and month out of the given date?

Is there a way to calculate the the broadcast year and month for a given gregorian date?
The advertising broadcast calendar differs from the regular calendar, in the way that every month needs to start on a Monday and end on a Sunday and have exactly 4 or 5 weeks. You can read about it here: http://en.wikipedia.org/wiki/Broadcast_calendar
This is a pretty common thing in TV advertising, so I guess there is a standard mathematical formula for it, that uses a combination of date functions (week(), month(), etc...).
Here is an example mapping between gregorian and broadcast dates:
| gregorian_date | broadcast_month | broadcast_year |
+----------------+-----------------+----------------+
| 2014-12-27 | 12 | 2014 |
| 2014-12-28 | 12 | 2014 |
| 2014-12-29 | 1 | 2015 |
| 2014-12-30 | 1 | 2015 |
| 2014-12-31 | 1 | 2015 |
| 2015-01-01 | 1 | 2015 |
| 2015-01-02 | 1 | 2015 |
Here is example how the broadcast calendar looks for 2015:
http://www.rab.com/public/reports/BroadcastCalendar_2015.pdf
As far as I can see, the pattern is that the first of the Gregorian month always falls within the first week of the Broadcast calendar, and any days from the previous month are pulled forward into that month to create full weeks. In Excel, you can use the following formula in cell B2 (first of your broadcast months above) to calculate the broadcast month:
=MONTH(A2+(7-WEEKDAY(A2,2)))
Similarly, in cell C2:
=IF(AND(MONTH(A2)=12,B2=1),YEAR(A2)+1,YEAR(A2))
This will return the broadcast month and year for any dates you put into your data set.
Hope that helps!
month,first,last
2018_1,2018-01-01,2018-01-28
2018_2,2018-01-29,2018-02-25
2018_3,2018-02-26,2018-03-25
2018_4,2018-03-26,2018-04-29
2018_5,2018-04-30,2018-05-27
2018_6,2018-05-28,2018-06-24
2018_7,2018-06-25,2018-07-29
2018_8,2018-07-30,2018-08-26
2018_9,2018-08-27,2018-09-30
2018_10,2018-10-01,2018-10-28
2018_11,2018-10-29,2018-11-25
2018_12,2018-11-26,2018-12-30
2019_1,2018-12-31,2019-01-27
2019_2,2019-01-28,2019-02-24
2019_3,2019-02-25,2019-03-31
2019_4,2019-04-01,2019-04-28
2019_5,2019-04-29,2019-05-26
2019_6,2019-05-27,2019-06-30
2019_7,2019-07-01,2019-07-28
2019_8,2019-07-29,2019-08-25
2019_9,2019-08-26,2019-09-29
2019_10,2019-09-30,2019-10-27
2019_11,2019-10-28,2019-11-24
2019_12,2019-11-25,2019-12-29
2020_1,2019-12-30,2020-01-26
2020_2,2020-01-27,2020-02-23
2020_3,2020-02-24,2020-03-29
2020_4,2020-03-30,2020-04-26
2020_5,2020-04-27,2020-05-31
2020_6,2020-06-01,2020-06-28
2020_7,2020-06-29,2020-07-26
2020_8,2020-07-27,2020-08-30
2020_9,2020-08-31,2020-09-27
2020_10,2020-09-28,2020-10-25
2020_11,2020-10-26,2020-11-29
2020_12,2020-11-30,2020-12-27
2021_1,2020-12-28,2021-01-31
2021_2,2021-02-01,2021-02-28
2021_3,2021-03-01,2021-03-28
2021_4,2021-03-29,2021-04-25
2021_5,2021-04-26,2021-05-30
2021_6,2021-05-31,2021-06-27
2021_7,2021-06-28,2021-07-25
2021_8,2021-07-26,2021-08-29
2021_9,2021-08-30,2021-09-26
2021_10,2021-09-27,2021-10-31
2021_11,2021-11-01,2021-11-28
2021_12,2021-11-29,2021-12-26
2022_1,2021-12-27,2022-01-30
2022_2,2022-01-31,2022-02-27
2022_3,2022-02-28,2022-03-27
2022_4,2022-03-28,2022-04-24
2022_5,2022-04-25,2022-05-29
2022_6,2022-05-30,2022-06-26
2022_7,2022-06-27,2022-07-31
2022_8,2022-08-01,2022-08-28
2022_9,2022-08-29,2022-09-25
2022_10,2022-09-26,2022-10-30
2022_11,2022-10-31,2022-11-27
2022_12,2022-11-28,2022-12-25
2023_1,2022-12-26,2023-01-29
2023_2,2023-01-30,2023-02-26
2023_3,2023-02-27,2023-03-26
2023_4,2023-03-27,2023-04-30
2023_5,2023-05-01,2023-05-28
2023_6,2023-05-29,2023-06-25
2023_7,2023-06-26,2023-07-30
2023_8,2023-07-31,2023-08-27
2023_9,2023-08-28,2023-09-24
2023_10,2023-09-25,2023-10-29
2023_11,2023-10-30,2023-11-26
2023_12,2023-11-27,2023-12-31
2024_1,2024-01-01,2024-01-28
2024_2,2024-01-29,2024-02-25
2024_3,2024-02-26,2024-03-31
2024_4,2024-04-01,2024-04-28
2024_5,2024-04-29,2024-05-26
2024_6,2024-05-27,2024-06-30
2024_7,2024-07-01,2024-07-28
2024_8,2024-07-29,2024-08-25
2024_9,2024-08-26,2024-09-29
2024_10,2024-09-30,2024-10-27
2024_11,2024-10-28,2024-11-24
2024_12,2024-11-25,2024-12-29
2025_1,2024-12-30,2025-01-26
2025_2,2025-01-27,2025-02-23
2025_3,2025-02-24,2025-03-30
2025_4,2025-03-31,2025-04-27
2025_5,2025-04-28,2025-05-25
2025_6,2025-05-26,2025-06-29
2025_7,2025-06-30,2025-07-27
2025_8,2025-07-28,2025-08-31
2025_9,2025-09-01,2025-09-28
2025_10,2025-09-29,2025-10-26
2025_11,2025-10-27,2025-11-30
2025_12,2025-12-01,2025-12-28

Sql Server 2012 - Group data by varying timeslots

I have some data to analyze which is at half hour granularity, but would like to group it by 2, 3, 6, 12 hour and 2 days and 1 week to make some more meaningful comparisons.
|DateTime | Value |
|01 Jan 2013 00:00 | 1 |
|01 Jan 2013 00:30 | 1 |
|01 Jan 2013 01:00 | 1 |
|01 Jan 2013 01:30 | 1 |
|01 Jan 2013 02:00 | 2 |
|01 Jan 2013 02:30 | 2 |
|01 Jan 2013 03:00 | 2 |
|01 Jan 2013 03:30 | 2 |
Eg. 2 hour grouped result will be
|DateTime | Value |
|01 Jan 2013 00:00 | 4 |
|01 Jan 2013 02:00 | 8 |
To get the 2 hourly grouped result, I thought of this code -
CASE
WHEN DatePart(HOUR,DateTime)%2 = 0 THEN
CAST(CAST(DatePart(HOUR,DateTime) AS varchar) + '':00'' AS TIME)
ELSE
CAST(CAST(DATEPART(HOUR,DateTime) As Int) - 1 AS varchar) + '':00'' END Time
This seems to work alright, but I cant think of using this to generalize to 3, 6, 12 hours.
I can for 6, 12 hours just use case statements and achieve result but is there any way to generalize so that I can achieve 2,3,6,12 hour granularity and also 2 days and a week level granularity? By generalize, I mean I could pass on a variable with desired granularity to the same query rather than having to constitute different queries with different case statements.
Is this possible? Please provide some pointers.
Thanks a lot!
I think you can use
Declare #Resolution int = 3 -- resolution in hours
Select
DateAdd(Hour,
DateDiff(Hour, 0, datetime) / #Resolution * #Resolution, -- integer arithmetic
0) as bucket,
Sum(values)
From
table
Group By
DateAdd(Hour,
DateDiff(Hour, 0, datetime) / #Resolution * #Resolution, -- integer arithmetic
0)
Order By
bucket
This calculates the number of hours since a known fixed date, rounds down to the resolution size you're interested in, then adds them back on to the fixed date.
It will miss buckets out, though if you have no data in them
Example Fiddle