TSQL Paramterised Pivot Query SP SSRS - sql

Wonder if anyone has done this before and what solution you came up with?
I have a Stored Proc that basically is a financial aged debtors query. I have added params to it so that you can select the Start Date, Frequency and Period length of the query. e.g. I could pick 12 months back from TODAY or 01/01/2012 or 4 Quarters back or 1 year back depending on the detail of the output I wish to see.
All well and good and when I run it in SQL it is lovely and works beautifully. I have pivoted the data within the SP in different ways depending on the Period param so that the columns show correctly (i.e. Shows the month name column for months, Quarter name for quarters etc etc.) so the pivot is conditional on the input parameter.
As I say in SQL it is a beautiful thing... now how the hell do I display it in SSRS!? :o)
As the PIVOT is conditional I can't see the available fields and I've got to a point where I can't think anymore of a way around it.
Any help appreciated!

Assuming I got you right, there is an option to place the entire SP in the Dataset.
In the dataset properties , change the query type to Store Procedure.
Hope it helps for you...

Related

SQL Method for Cascading Workload Based on Rank and Available Hours

Recently I created an automated production scheduling tool through Excel that assigns a rank to items being produced in the same process, and then uses that rank in combination with the workload to create a schedule.
It functions exactly the way it is intended to, but due to the large amount of data and it being excel it has very slow performance, which is why I am looking to move the calculations over to SQL.
The general logic is like this:
-Always produce everything from the first day before the second day
-Always produce items from an earlier rank before items from a later rank
You can see how this plays out in the image below, where the line has 21.5 hours today, so items will be produced on day 1 until it equals 21.5, where the remainder is then carried over to day 2 and so on.
I was able to do this in excel using lengthy positional based formulas, but I am trying to think of a way to get the same result in SQL without having to rely on looking at the row above.
I am not sure how to convey something like 'Subtract from the available time production time of higher priority items produced on the same day'.
I apologize if the question is unclear, but any advice would be appreciated.
Image of Production Hours Cascading by Priority and Day
Example of Position-Based Fomula
Thanks to shawnt00, that put me in the right direction. Ultimately I had to modify the case statements a bit to go off of the cumulative total instead, but I was able to get the desired results using a sum() Over (partition by order by ) statement.

DAX sum different DateTime

I have a problem here, i would like to sum the work time from my employee based on the data (time2 - time 1) daily and here is my query:
Effective Minute Work Time = 24. * 60 * (LASTNONBLANK(time2,0) -FIRSTNONBLANK(time1,0))
It works daily, but if i drill up to weekly / monthly data it show the wrong sum as it shown below :
What i want is summary of minute between daily different times (time2-time1)
Thanks for your help :)
You have several approaches you can take: the hard way or the easier way :). The harder (at least for me :)) is to use DAX to do this. You would:
1) create a date table,
2) Use the DAX calculate function to evaluate your last non-blank and first non-blank values (you might need to use calculate table, but I'm not sure; DAX experts jump in). Then subtract one vs. the other.
This will give you correct values for a given day for a given person. You can enforce the latter condition by putting a 'has one value' guard on the person name so that your measure informs the report author if they're not using it right.
Doing the same for dates is a little trickier. In the example you show you are including the date in the row grouping. But if you change your mind and want instead to have 'total hours worked by person' or 'total hours worked by everyone' you're not done with modelling yet.
Your next step is to use calculate table in combination with calculate to create a measure that returns the total. You'll use calculate table so you evaluate each date and the hours worked on that date by person. Then you'll use calculate to summarize that all down to a single number. If you're not careful with your DAX (or report authoring) you might mix which person you're summarizing for so that your first/last non blank are not at the person level. It gets intense quickly.
Your easier solution, though it might be more limited in its application - depends really on your scenario - is to use the query to transform the data into a summary by day and person using the group by command. This will give you a row per person per day with their start and end times. Then you can quickly calculate the hours worked on that day. Then you can quite easily build visuals on top of the summary data. Of course you give up some of the flexibility of the having a proper data model. However if you have a date table, a person table, and your summary table and then setup your relationships correctly you can achieve answers to the most common questions.

Business Objects (webi) stuck forever on retrieving data for specific dates

I am very new to BO webintelligence.
I am doing a very simple query, retrieve Sales Amount for dates between 2012 and 2013. Just for this simple query when I run it, my BO crashes or gets stuck on "please wait" window. Why is this happening? If I select like 3 or 4 days like between Jan 1st 2012 and Jan 4th 2012, it runs fine. Is there anything from my end that I am doing wrong? This is in production mode.
I also wanted to point out that I have tried limiting my data set for specific region etc.
Too many unknowns to successfully assist. I presume this is webi 3.1.x or 4.1.x and I presume that the Sales Amount is a measure and that your query includes a date which does not include a time component. If you only have these two objects in your results pane and have the region in your conditions then pulling something like [Date] and sum([Sales Amt]) should not take long to execute. As previous poster suggests, try to execute the sql in a tool like SQL*Developer or Management Studio or however the source database / OLAP is stored.
Even though you've limited the data to a "region" this may be too much data. Try selecting a smaller result set of 100 rows or changing the option for "retrieve duplicate rows" on the query panel.
If possible, post the query from your report using generic object names?

Building SSRS time series report from SQL datetime column

I need to create a SSRS report where the user specifies the start and end date (e.g. March 2012 to July 2012) and the result is a report with the following headings:
User Department Product Mar-12 Apr-12 May-12 Jun-12 Jul-12
The dates are stored in a datetime column (e.g.2012-06-11 14:48:04.787) in my SQL table which I'm using to build the time-series report.
Do I need to convert/transpose my SQL table first? Or should I be processing this in SSRS?
Your advice is much appreciated!
Based on your question it appears all your dates are in a single datetime column and your trying to display them, formatted as a month, in a single report with the report columns grouped and renamed based on the month. This is a pivot and I don't think you can do that within SSRS. The dataset will already need to be pivoted by the time SSRS gets it.
So, to me this sounds like something you'll have to do both pre and post SSRS. I did a similar report using days of the month. You can see the winning answer here. as opposed to months of the year.
Thinking through your logice, I think you'll probably have to build the query with the pivot and then send to SSRS. On the SSRS side you need 12 columns for all 12 months and then put some logic to hide the ones that don't get data. Your original pivot will need to be smart enough to pass the empty months so SSRS doesn't error out. The end result will appear the same but needs a lot of pre-thought and design effort up front.
You do the processing using SSRS - you would design it in the GUI editor(BIDS ) , and you can customize things using the VBasic code also(i.e, dates and logic)
Looks like you need an intro, I really recommend this video

variable column names in sql table valued function

Suppose that I am using a sql server to keep track of all my personal expenses and that I am tagging everything with a date and a category.
This allows me to do things like aggregate monthly expenses per category and look at the last several months of expenses with each category as a row and the most recent months as columns.
What I am stuck on is the fact that I am having to name the columns things like "most recent month", "previous month", and "2 months ago". I would really prefer to be able to name them something like "Jan10", "Feb10", or "Mar09" or something like that and have them update automatically every month.
Calculating the names is simple enough, but I'm not sure how to get sql server to interpret a formula or join or anything like that as the alias for a column.
Any insights on this one?
What I am stuck on is the fact that I am having to name the columns things like "most recent month", "previous month", and "2 months ago". I would really prefer to be able to name them something like "Jan10", "Feb10", or "Mar09" or something like that and have them update automatically every month.
That approach means creating multiple columns, which will eventually hit the limit. In SQL Server, that's 1,024 for a non-wide and 30,000 for a wide table ...
A better approach is to store the date, and section/partition the data in the query as needed. Because Jan 10th is going to have a different value for 2009, 2010, 2011, etc...
It looks to me like you're trying to store data the same way you want to view it. That is not a very good way of going about it. I would suggest the following:
Your table should have the essentials, say:
Store (This could become its own table, but we can leave it as a string)
Category
Amount Spent
Date
Then, once you have this data, you can report on it. This report will do the heavy lifting, and will be dynamic in the sense that you don't need to hardcode values. The following is just an example, and I can't promise the syntax is even correct.
SELECT Store, SUM(CASE WHEN Date > GETDATE() - 14 THEN AmountSpent ELSE 0 END)
FROM YourTable
GROUP BY Store
The above will give you all money spent at each store in the last 14 days. This window will be "sliding", every time you run it, it will look back two weeks; no hardcoding.
You may wish to acquaint with Entity-attribute-value model and approaches.