SSRS - Sum Last 12 items - reporting-services

I have a Total column and am summing all the values in the row with:
=Sum(Fields!MyField.Value)
How can I sum only the last 12 items?
This is valid:
=Last(Fields!MyField.Value)
I need something like the below...
=Sum( LastX(Fields!MyField.Value, 12) )

You could achieve this result by creating a subquery in your main dataset query. You will just have to have a value that you want to sort on in descending order.
Say you have a table sorted by the price in ASC order. You want to sum the last 12. You just need to flip the sort in the subquery and sum the top 12.
SELECT OtherFields, (SELECT TOP 12 Sum(S1.MyField) FROM MyTable S1 ORDER BY S1.MyField DESC) AS BottomTwelveSum
FROM MyMainQueryTable
Yes this value will repeat in your dataset, however if you place it correctly in the Table you should be fine, just dont show the value in the details section. This wont add additional rows. Another solution would be using a subreport, however this is much quicker on render time.

Related

SSRS 2016 - How to calculate a percentage difference between two values in a group?

I'm creating a report that shows the sales of item before a date range and after a date range.
The part I ran into trouble with is the percentage difference between the total sales on Date 1 and Date 2.
Items can have no sales for a certain week.
The user can select multiple item ID's in the item ID parameter.
I can update the question to post my SQL query if needed.
What I've tried
Since I put a group on item ID I thought the First and Last functions would work.
Here's my expression on the column PCT.
=(Last(Fields!total_sales1.Value, "Date1")- First(Fields!total_sales1.Value, "Date1")) / First(Fields!total_sales1.Value, "Date1") * 100
But when I run the report I get the following results.
I need an expression on PCT column that will give me a percentage difference for each item pairs.
It looks like your scope is incorrect. Check what the rowgroup is called where you group by item id (let's say the row group is called "yourItemRowGroupName").
Then change you expression to use that scope rather than "Date1".
In fact you may not need the scope at all as it should work within the scope that expression sits (in your case, within your ItemID group.).
So try
=(Last(Fields!total_sales1.Value)- First(Fields!total_sales1.Value)) / First(Fields!total_sales1.Value) * 100
Or..
=(Last(Fields!total_sales1.Value, "yourItemRowGroupName")- First(Fields!total_sales1.Value, "yourItemRowGroupName")) / First(Fields!total_sales1.Value, "yourItemRowGroupName") * 100
You may have to handle divide by zero but try this to start with before you add any more complications.

Is it possible to limit RunningValue to look back for a certain number of rows?

Im trying to average the past 5 rows in my table i created in SSRS grouped by date(Monday of every week). Ive tried runningValue however it looks back at all the past rows for each group. Is there a way to limit the scope to just the past 5 rows or weeks for each Date group.
Thanks
I would accomplish this with grouping. I don't know what your dataset is like but I assume it is a SQL query you can modify. The easiest solution would be to add a week number column to your query. For example:
SELECT datepart(week, YOURDATE) as WeekNumber
More info on datepart:
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017
Once you have a week number, use the table creation wizard in Report Builder and add WeekNumber as a row group. This will group your values by week number and give you a total under each week. You can change the total by double clicking and making it AVG() instead of SUM().
Note: If you already have each 5 day period in a group, you should be able to right click that and add total. At which point you can just change the SUM to AVG there.

SSRS Matrix Grand Total

I have the followin matrix that shows a total per year, and I need to show the grand total across all years below that. Here is my current Matrix with the yearly totals:
I have the grand total stored in a variable but I'm struggling to get the formatting correct.
The closest that I have got is to add the grand total as the first row. To do this I right click on the year column > Add Group > Group by x > Select Add Group Header > OK. Then delete the extra column that was added.
It doesn't matter if I select add header or add footer, it always places the new row above the existing row. How do I put this total row at the bottom of the matrix? I need to have the total value span across all year columns and centered.
Try adding a group adjacent below. You would need to group by a variable that is unique in the context of the matrix, so that the resulting adjacent row group has only one row. Then manually write the expression for the aggregate you want in the relevant cell.

Matrix ordering inside group with specific grouping first

I'm having a problem with a report layout.
All these values are coming from the bank, no calculation is needed in SQL.
I have three columns, Ranking, Year and Company.
I have a row group for Company, and a column group for Year. I want to sort the results of the ranking, but I want the latest year to sort "first":
You can set up your group to have two sorting expressions.
The second will just be your current sorting expression, i.e. based on Ranking.
The first sorting expression will be something like:
=IIf(Fields!Year.Value = DatePart(DateInterval.Year, Today()), 0, 1)
This is checking your Year value against the current day's year - if they match, the expression assigns a value 0, and all other rows will be assigned a value of 1.
This means that the current year will always be first, but then all other years will be sorted by the second sorting expression, e.g. Ranking in your case.

mySQL group by?

Say I have 5 rows in the database, three of them have an ID of 2, two of them have an ID of 1.
The three with ID of 2 have dates, lets say for example 1st June, 2nd June, 3rd June.
The two with ID of 1 have dates, lets say for example, 1st July, 2nd July.
How do I go about selecting only 1 of the latest values, for each ID?
When currently using GROUP BY id it is returning the first row for each, not the latest added.
I want the latest one added... any idea?
Thank you.
Same Kyle R from SitePoint?
Every column in your SELECT list must either appear in your GROUP BY clause or be an aggregate function, otherwise the return value is undefined by specification and MySQL is free to give you anything it wants.
Think about what grouping means. Take some set of rows, and collapse them down into a single row representing the group. In doing so, you must tell MySQL from which row each column in the representative row should come. If you want the greatest value from the group, you use MAX. If you want the smallest, you use MIN, etc.
maybe something like:
select id, max(date)
from thetable
group by id