Group By Year, Month, Week, Day Dynamically in Crystal Reports

crystal-reports

This must be simple, yet I can't seem to find an answer.

I have a simple Query:

SELECT InvoiceTotal, InvoiceDate, InvoiceNoUnit from InvoiceLineItem

Which I want to display in Crystal Reports from my App. Rather than build many reports, I want to be able to have the user change a parameter that allows the resulting report to be grouped by date, based on the level of granularity selected by the user.

EG: I want the user to be able to select "By Month" and the report is grouped by Month (with corresponding Chart displayed by month. Then the user runs the report "by week" and the report changes to group by week.

I want them to select this in my app, and for me to pass a parameter to CR to change the grouping.

Any suggestions?

Best Answer

You can create a parameter where the user selects "Year", "Month", "Day" etc.

From there, you can create a formula that specifies the grouping based on the parameter above, and then create a Grouping in the report that uses this formula to group by.

I don't have Crystal Reports handy, but the formula could look something like this:

IF @UserDefinedGrouping = "Year" THEN
    DATEPART(yyyy, {Table.InvoiceDate})
ELSEIF @UserDefinedGrouping = "Month" THEN
    DATEPART(m, {Table.InvoiceDate})
ELSEIF @UserDefinedGrouping = "Quarter" THEN
    DATEPART(q, {Table.InvoiceDate})
' etc...
Related Topic