Format date for group by function

22strider

Active Member
Joined
Jun 11, 2007
Messages
311
Hello Friends,


For my report I have to group records by Month/Year. The data has date in the format of date+month+year+time; I was able to reformat to Month-Year format. But the problem is that the reformat is just a view. When I try to group based on the reformatted data Access considers date+month+year+time and does not group based on Month-Year.

Second part of the requirement is being able to sort data by month-date.

Could you please tell what my options are.

Regards,
Rajesh
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
You can use the MONTH and YEAR functions to pull the MONTH & YEAR out in a calculated fields and use those for your Grouping and Sorting Purposes.
 
Upvote 0
Wanted to add that I am using MS Access 2013

Hello Friends,


For my report I have to group records by Month/Year. The data has date in the format of date+month+year+time; I was able to reformat to Month-Year format. But the problem is that the reformat is just a view. When I try to group based on the reformatted data Access considers date+month+year+time and does not group based on Month-Year.

Second part of the requirement is being able to sort data by month-date.

Could you please tell what my options are.

Regards,
Rajesh[/QUOTE]
 
Upvote 0
Hello Joe4,

Thanks for your replay.
I did try that. The problem was with sorting. The date field was getting sorted alphabetically rather than in date chronological order.

Regards,
Rajesh
 
Upvote 0
Can you post the Calculated field calculation you created?
 
Upvote 0
Hello Joe4,

Following is my calculation:

DatePart("M", [Effectivity Date])&"/"&DatePart("yyyy", [Effectivity Date]) AS [Calculated Date]

Upon sorting results based upon above given script '4/2015' comes before '9/2014'
 
Upvote 0
OK. By using the DatePart functions and concatenating them together like that, you are creating a string (it is not a date value). As such, it is sorted alphabetically, and since "4" comes before "9", your data is sorted like that.

There are a few ways around this. Here are two:

Method 1:
Use a calculated field that lists year before month, i.e.
MyDate: Format([Effectivity Date],"yyyy-mm")
and use that. This returns a string, but since Year is listed before Month, and all Months are two digits, it will sort/group like you want.

Method 2:
Use two calculated fields, one for MONTH and one for YEAR that returns both values as numbers, i.e.
MyYear: Year([Effectivity Date])
MyMonth: Month([Effectivity Date])
then Group on BOTH of these fields. In sorting, sort first by MyYear, and second by MyMonth.

Keep in mind that if you want to display your date on your Report in "m/yyyy" format, you can still use your original calculated field, you just also need to include one of these other two options in you query, and sort/group based on those fields instead of your original calculation.
 
Upvote 0
thank you very much. I am sure both methods you suggested would work. I tried Method1 and it worked; going with Method1.

Thanks again,
Rajesh
 
Upvote 0

Forum statistics

Threads
1,214,422
Messages
6,119,395
Members
448,891
Latest member
tpierce

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top