Generating Calculated Fields by Time Period in Report Builder 3.0 - sql-server

I've spent weeks building a massive view, followed by a massive report in SSRS/Report Builder 3.0 that shows master inventory levels, purchasing levels, stock outages, etc. in fine form. The end result of this report is that I'll have every one of our stock items listed as rows, and collapse-ably grouped by vendor. As columns, I have time values, currently grouped by year and then by month (I'd like to add seasons/quarters here down the road, since we're a very seasonally oriented business, but that can wait right now).
Right now, the report shows the following columns for each month, per item (row): Quantity Shipped, Quantity Received in Stock, Stock Levels at the end of the Period, Avg Time Spent Out of Stock in the Period, and the Number of Outages within the period.
So, what I need now in order to finish up this beast is the estimated daily usage. To get this, I want to get the period length, by the column groupings, and subtract the sum(DaysOutofStock). The result would be Days in Stock, which I'd then divide by the totals shipped in the period.To boil this down, I just want to know that on days when the item is in stock, we're moving X on average.
So what I need is the period length, as a value to use in a function. Is there any way to get this automatically? Is it hidden somewhere in the function menu? Is there a function or expression I can use that will look at the month or grouping or whatever of the column and tell me that the month I'm in has X days?
My underlying data has massive gaps, especially earlier in the company's life, so there's no easy way to derive it from there (at least, that I can think of). Please help! I'm so close to finally finishing this project! Thanks!
If you need any more info or details, I'll be happy to provide.

Assuming you can do it in SQL code, the month in your report has the following days:
SELECT [Year], [Month], DateDiff(dd, CAST(CAST([Year] AS VARCHAR) + '-' + CAST([Month] AS VARCHAR) + '-01' AS DATETIME), DateAdd(mm, 1, CAST(CAST([Year] AS VARCHAR) + '-' + CAST([Month] AS VARCHAR) + '-01' AS DATETIME))) AS DaysDifference
FROM MyTable

Related

Snowflake Moving Averages

Im having issues creating a 4 week moving average calculation on Snowflake, the goal is to have a table with all the distinct weeks and descriptions for each product and the volume and amounts paid for them.
Here is the code that I'm using
Select
(date_trunc(WEEK, TO_DATE(YR || '-' || MTH || '-' || DT, 'YYYY-MM-DD'))) as Week ,
(DESCRIPTION) as "DESC",
AVG(C.VOL) OVER(PARTITION BY DESC ORDER BY Week ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) as TXN,
AVG(C.PRNLC) OVER(PARTITION BY C11.DMADES ORDER BY Week ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) as PRN
FROM C
GROUP BY Week, "DESC"
I keep getting an error
[CAST(C.VOL_17 AS NUMBER(38,3))] is not a valid group by expression
The Code works fine by removing the averages and window functions but then I'd have to go out on other tools and create the 4 week moving average I'd expect to see there, i have tried to remove the grouping and use it as distinct but it would still return 1 line for each record, this goes beyond my expertise I'm afraid, I'm not really sure how Cast comes into play here. Is there another way to accomplish what I want? do you guys see what I'm doing wrong here?
Thank you!

Getting Max and Min Values in Crystal Report Formula

I am Using Crystal Report with VS 2012 with Dataset and Populating the Report with Run Time.
I am getting Repetitive rows for My Report, It is because the Data is Entered with Different Period (Month/Year) like the salary of Person for Month and Year.
So What I thought to get rid of the Repetition of rows is I am going to Get the Values of Summarized (Group footer) portion of a Particular Employee instead of GETTING DETAIL SECTION where Every record repeats instead of Period. I have achieved it, But the Problem is the Minimum Function is not getting the accurate Minimum Period as Shown the picture.
The formula for I which I used to Show Minimum and Maximum period values is:
Maximum ({DataTable1.Period}, {DataTable1.PPO_NO})&" - "& Minimum ({DataTable1.Period}, {DataTable1.PPO_NO})
and My Formula for getting Employee Month and Year are:
SELECT PPONO, Name, Father_Name,Designation,BPS,Claimant,Age,Type,IOT, TblPPOBook.Pay_Month + '/' + CAST(TblPPOBook.Pay_Year AS nvarchar(4)) AS Period, Pension, Allow etc. From EmployeeTable Where PayDate Between Date1 ANd Date2.
The Early help in this regard will be highly appreciated.
Create a formula to convert string to date i.e
#String to date:= Datevar Date_Period:=CDate({Period});
Second use this formula to find min and max date i.e
Maximum ({#String to date}, {PPO No#})& " to " & Minimum ({#String to date}, {PPO No#}) ;

MS SQL - Calculating plan payments for a month

I need to calculate how much a plan has cost the customer in a specific month.
Plans have floating billing cycles of a month's length - for example a billing cycle can run from '2014-04-16' to '2014-05-16'.
I know the start date of a plan, and the end date can either be a specific date or NULL if the plan is still running.
If the end date is not null, then the customer is charged for a whole month - not pro rated. Example: The billing cycle is going from the 4th to 4th each month, but the customer ends his plan on the 10th, he will still be charged until the 4th next month.
Can anyone help me? I feel like I've been going over this a million times, and just can't figure it out.
Variables I have:
#planStartDate [Plan's start date]
#planEndDate [Plan's end date - can be null]
#billStartDate [The bill's start date - example: 2015-02-01]
#billEndDate [One month after bill's start date - 2015-03-01]
#price [the plan's price per billing cycle]
Heres the best answer I can give based on the very small information you have given so far(btw, in the future, it would really help people answer your question faster/easier/more efficiently if you could specify a lot more info;tables involved, all columns, etc..):
"I need to calculate how much a plan has cost the customer in a specific month."
SELECT SUM(price), customerID(I assume you have a column of some sort in this table to distinguish between customers) FROM table_foo
where planStartDate BETWEEN = 'a specific date you specify'
Its a bit rough of a query, but thats the best I can give till you specify more clearly your variable (i.e. tables involved, ALL columns in table, etc etc.....)

T-SQL - Work backward through values till I get something above Zero

I have Billing Rate History table. We are required to report billing rate changes to clients, so I compare this period's (month) billing rate to that of last period. If there is a delta, I report that. At the close of the routine, I record current rates for comparison next month. It works fine, but there is a challenge...
If employee Tom Thumb works for the client in question this month, but did not work last month, there is nothing against which to compare. Or, if Tom worked Overtime this month, but only straight time last month, I have no overtime rate against which to compare.
I'm trying to find a way to walk backward by Period till I find a valid > 0 rate for comparison.
So, let's say we are billing Period 201403.
Tom has a straight time bill rate of 54.04 for Period 201403.
He has an overtime rate of 81.06 for that same Period.
Now, I look at Period 201402 for his straight time and overtime rate. If there is no delta, I move on to the next employee.
But what if Tom has no ST or OT rate in Period 201402? I need to walk backward to 201401, 201312, etc. till I find the rate he had the last time he worked for this client.
I've read that it is not a good practice to use loops on a DB. What's the best practice for accomplishing what I need to accomplish?
You should be able to do this with a cte, a self-join, and a ranking function. Something like:
with cte as (
select *, row_number() over (partition by employeeid order by periodid) as [rn]
)
select employeeid, curr.billrate, [last].billrate
from cte as [curr]
left join cte as [last]
on curr.employeeid = last.employeeid
and curr.rn = last.rn + 1
You'll have to jigger this to fit your actual table structure, but this should get you pretty close. Feel free to add a where clause (i.e. where curr.billrate <> [last].billrate)

Sum report item in a column group (SSRS 2008)

I have the following payroll table in an SSRS 2008 (R2) report:
The dataset returns labor transactions consisting of the following fields:
STARTDATE
STARTTIME
FINISHDATE
FINISHTIME
REGULARHRS (difference between finish and start)
REFWO (such as "Travel", "Holiday", "Work", etc and is used to sort into the categories shown in the table above)
TIMEWORKED (0/1 flag that indicates whether or not it counts towards "Time Worked" category)
I have a column grouped on STARTDATE so that it displays each day of the week (our weeks go Mon through Sun). Everything down to "Unpaid Time Off" is a simple expression (usually just in the format Sum(IIF(something,A,B)) in both the daily column and the weekly (Totals) column. In the "Interim Regular" box (for the day grouping), I have the following expression:
=IIF(Weekday(Fields!startdate.Value)=1
OR Weekday(Fields!startdate.Value)=7
OR ReportItems!Holiday.Value>0,
0,
ReportItems!TimeWorked.Value-ReportItems!Holiday.Value-ReportItems!Bereave.Value)
Basically what I'm doing is saying: If STARTDATE is a Saturday, Sunday, or Holiday, the regular hours would be 0 since it would fall into OT1.5 (overtime, time and a half), otherwise I calculate the regular hours worked by subtracting Holiday time and Bereavement time from the Time Worked (since both are included as part of Time Worked). This part works great!! But when I try to sum up the total for the week using =Sum(ReportItems!InterimRegularDaily.Value) it tells me that I can't use an aggregate on a report item and that aggregate functions can be used only on report items contained in page headers and footers. I've done extensive googling to see if there is a solution, but everything seems to involve writing custom functions. I can't believe it would be THAT hard to simply sum up a group calculation in an outer group!
Any and all help would be greatly appreciated!!
Thanks!
You can add the scope the your Sum() expression to reverence the the whole dataset or a group:
'Returns the sum of the whole DataSet
=Sum(Fields!TestField.Value, "YourDataSetName")
'Returns the sum of a group
=Sum(Fields!TestField.Value, "YourGroupName")
You can also stack them:
=Sum(Sum(Fields!TestField.Value, "Level3GroupName"), "Level2GroupName")

Resources