Crystal Reports - Combine Rows - sql-server

I'm very new to Crystal Reports and I've one little problem.
I'll be quick and on point.
The columns are as follow:
2016-01-05, 2016-01-06, 2016-01-07, 2016-01-08, 2016-01-09 (list goes on till end of year)
These dates come from Database field called NextDueDate and it populates the report with all the dates.
My question, is it possible to combine these rows or group them by weeks or perhaps by month?
Result I'm looking for is:
2016-01 (All january days combined here) or something like maybe even just January etc.
NextDueDate field is type string by the way.
Also, I'd like to do this without getting involved in the actual database itself due to specific reasons.

you can use this simple formula as
#year_month = left ({NextDueDate},7);
create formula as year_month
and put left ({NextDueDate},7); in formula editor click on save.
now you can see this formula under formula field in field explorer.
Drag this one to your detailed section.
You can group the result based on this formula.
OR
user these formula
#year_month = left ({NextDueDate},7);
#monthOnly = right( #year_month , 2);
#FullMonthName = if #monthOnly = '01' then 'January'
else if {#month} = '02' then 'February'
........................................
else 'December';

Related

Use query to combine data from ever-changing tab list

I am building a Sheet that will be taking imported data from other tabs and combining it in a summary sheet, for me to use elsewhere. The problem is, the tabs will be imported monthly, so there will be dozens of them very quickly, and I don't want to edit the query function every month. They will all have the same naming format, but I'm wondering if anyone has an idea about how to formulate the query with variables for the sheet name that I can pull from another cell. For example, I could import the tab names into another row, and have the query pull the sheet names from that row, but I am not familiar with how to do this. Here is an example of the query formula I'm using in a similar sheet:
=QUERY({'2019 TEAM SALES'!A2:ZY; '2020 TEAM SALES'!A2:ZY}, "select * Where Col1 is not null",1)
With this one, updated once a year isn't a big deal. But this new sheet will be monthly, so it will be unwieldy in just a few months. Is there a way using query to take the sheet names via cell info? Like if in row 1, I concatanate the sheet names with the range, so that A1 contains "'2019 TEAM SALES'!A2:ZY" and the next in B1 and so on, and then the query formula is:
=QUERY(A1:ZZ1, "select * Where Col1 is not null",1)
This way, it will always pull in all the current tabs necessary to combine.
Incidentally, I'm open to using other formulas or even a script to get this done, as it's the result I'm after, not the method.
I have seen a lot of answers similar to what's here, but this still forces extremely long formulas that I don't think are very efficient, given that this sheet will eventually have 50 or more tabs in it.
Thanks for any insights!
to generate list of sheets from start date to today's date +3 months in advance:
=ARRAYFORMULA(UNIQUE(TEXT(ROW(INDIRECT(
DATEVALUE("2020-4-1")&":"&
DATEVALUE(TODAY()+90))), "yyyy mmmm")))
converting it into ranges for query:
=ARRAYFORMULA("INDIRECT("""&UNIQUE(TEXT(ROW(INDIRECT(
DATEVALUE("2020-4-1")&":"&
DATEVALUE(TODAY()+90))), "yyyy mmmm"))&"!A:C"")")
to avoid possible errors for sheets that do not exist yet:
=ARRAYFORMULA("IFERROR(INDIRECT("""&UNIQUE(TEXT(ROW(INDIRECT(
DATEVALUE("2020-4-1")&":"&
DATEVALUE(TODAY()+90))), "yyyy mmmm"))&"!A:C""), {"&
TEXTJOIN(",", 1, SPLIT(REPT("""""♥", COLUMNS(A:C)), "♥"))&"})")
next we join it to create one single reference for query:
=ARRAYFORMULA("{"&TEXTJOIN("; ", 1,
"IFERROR(INDIRECT("""&UNIQUE(TEXT(ROW(INDIRECT(
DATEVALUE("2020-4-1")&":"&
DATEVALUE(TODAY()+90))), "yyyy mmmm"))&"!A:C""), {"&
TEXTJOIN(",", 1, SPLIT(REPT("""""♥", COLUMNS(A:C)), "♥"))&"})")&"}")
then we can build our query:
={""; ARRAYFORMULA("=QUERY({"&TEXTJOIN("; ", 1,
"IFERROR(INDIRECT("""&UNIQUE(TEXT(ROW(INDIRECT(
DATEVALUE("2020-4-1")&":"&
DATEVALUE(TODAY()+90))), "yyyy mmmm"))&"!A:C""), {"&
TEXTJOIN(",", 1, SPLIT(REPT("""""♥", COLUMNS(A:C)), "♥"))&"})")
&"}, ""select Col1,Col2 where Col3 is not null"", 0)")}
this way we generated our query formula as a string in G2 so the next step is either manually copy-paste this each month where you need it or you can use script which will auto-copy content of G2 and auto-paste it where you need it on any change so you dont need to touch it anymore
function onEdit() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1'); // sheet name
var src = sheet.getRange("G2"); // cell which holds the formula
var str = src.getValue();
var cell = sheet.getRange("G10"); // cell where I want the results
cell.setFormula(str);
}
spreadsheet demo
Solution
In order to use cell values (your variables) in your formula in order to automate the whole thing you can use the formula indirect which lets you use cell values in formulas. Therefore, you would end up with something like this:
=QUERY({INDIRECT(A1);INDIRECT(A1)}, "select * Where Col1 is not null",1)
where A1 is the cell containing 2019 TEAM SALES!A2:Y2
I hope this has helped you. Let me know if you need anything else or if you did not understood something. :)

Excel, counting within a formulatext on array

Imagine rows A2:A11 = name of customer, Columns B1:AE1 = days of the month.
To make it easy:
Daily, we tally if customers purchase (quantity) and separate them with a + to get the total of that day purchase. (example: on 2nd day of the month (C2:C5)
Abe =44+54+10
John =22+10+40
Sara =40
Mary=10+10
Also we need to count total “sales cases” of the whole day (in the above example it is 3+3+1+2)= 9 to show in the last row of the day. (B12 in this example)
The logic is something like
=SUMPRODUCT(LEN(FORMULATEXT(C2:C5))-LEN(SUBSTITUTE(FORMULATEXT(C2:C5),"+","")))
But I’m getting NA.
reminder: when there are no "+" signs & the value is more than zero, it should count as 1.
help?
There is a trick to do this, which might end up a bit convoluted to achieve the end result you want
First, define a new name in Name Manager (in the Formula menu bar)
Name: FormulaText
Refers to: =GET.CELL(6,OFFSET(INDIRECT("RC",FALSE),0,-1))
Now if you have a formula in cell B3 of =10+20+30 enter =FormulaText in cell C3 and you will get the text version of the formula
You can now count + symbols in that formula using =LEN(C3)-LEN(SUBSTITUTE(C3,"+",""))
In your specific circumstances, I would offset all of this to the right of your spreadsheet, say by 35 columns, in which case you will need to change the definition of FormulaText accordingly.
A fair bit of set-up work, but the result should work automagically.

Group by array or shared value

My invoice report pull due dates depending on the selection of Payment Plan Code on UI (either semi, monthly, quarterly, annually, or even 18 installments.) It also accordingly pulls gross premium per due date. I need to pull this table per due date and the sum of the gross premium if they fall into one due date.
What I do is break and save the due dates into array. How can I group by them? Crystal doesn't seem to allow me to group by a shared value, or group by array.
You don't need array for this purpose.. using array complicates the report instead you can manipulate grouping like below:
Create a formula like below and use this formula to group.
if parameterselection = "monthly"
then Month(duedate)
else if parameterselection = "yearly"
then Year(duedate)
.
.
.
.
formula till end
Edit-------------------------------------------
In this case as per your comment you need to create one more group (Group2) with due date.
Now you have two groups group 1 is the one I wrote first and group2 using due date and this works

SSRS MDX for Data from previous two weeks

I am trying to get the data fromt the present calender week to the previous two weeks. If I specify the date range as parameters in the MDX query, the table gets filled up properly.
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY { ([Project].[Projekt-Task].[Task].ALLMEMBERS* { [Date].[Date - CW].[Week].&[2014]&[201420] : [Date].[Date - CW].[Week].&[2014]&[201422]})} ON ROWS FROM [DWH]
But if I try and use the lag function, I get an error. Here is the MDX query.
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY { ([Project].[Projekt-Task].[Task].ALLMEMBERS* [Date].[DATECW - CW].[Week].CurrentMember.Lag(2) ) } ON ROWS
FROM [DWH]
Your first query (quite rightly) identifies a range of two weeks, by using two members from the dimension separated by a colon i.e. [Week X] : [Week Y].
Your second query uses the LAG function, which will return a member on your dimension, not a range of members.
Try this:
SELECT NON EMPTY {[Measures].[planned_cumulative],[Measures].[Last Forecast CW] } ON COLUMNS,
NON EMPTY {[Project].[Projekt-Task].[Task].ALLMEMBERS * TAIL([Date].[Date - CW].[Week],3) } ON ROWS
FROM [DWH]
Also, your first query refers to [Date].[Date - CW].[Week] whereas your second query uses [Date].[DATECW - CW].[Week]. I have gone with the first option as you say that query works.
Let me know if that helps.
Ash
EDIT: Apologies for that. I have amended the query to use the TAIL function, which will give you a number of tuples from a set starting at the end and working backwards. So with TAIL ({Set Of Weeks},3) this should give you the current week and the previous two weeks. I have tested a similar query on my cube and it seems produce the expected results.
Hope that solves it for you.
Ash

Reporting Services 3.0 Chart calling stored procedure with date time value , how to sort?

I have a Stored Procedure in SQL Server 2008. One of the values returned is:
SELECT DATEADD(mm, DATEDIFF(mm,0,e.Day), 0) AS [Month],
So I get values like: 2012-05-01 00:00:00.000
I want to display the month in the X axis for a chart report, obviously sorted chronologically. It would be great if I can display the month as "May-2012" or "2012 - May". Right now it displays the whole thing as 5/1/2012 12:00:00 AM and it is sorted alphabetically instead of by month.
When I check the Sorting it shows the field [Month] and order A to Z, so I assume it is not "seeing" it as a datetime value.
How do I get the sorting right?
How do I change the format to display the month as "May-2012" ?
Thanks in advance!
OK, work around:
I talked to my DBA, asked him to return a formatted field from the stored procedure, with values like "2012-05" and '2012-12' and sort by that field. Then I used that new field on the Report builder Chart, and remove all sorting.
Works for now, but still need to understand how to do it right for future reports.

Resources