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.
Related
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.
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.
In order to update records with a range of values I am aware of using the BETWEEN operator e.g.
UPDATE datetable
SET tableflag = 1
WHERE date BETWEEN '2017-02-01' AND '2017-03-01'
will set the field 'tableflag' to 1 for every record where the date lies between the 1st of February 2017 and the 1st of March 2017. This is a simple way for handling a single or multiple fixed ranges (by using several or conditions betwee BETWEEN operators).
For a dynamic range e.g. set 'tableflag' to 1 for every record where the date is between the first and last Sunday in February for the year 2017, one can use a subquery to generate the dates for the first and last Sundays in February.
However if the condition in the above problem is generalized and one needs to set 'tableflag' to 1 for every record where the date is between the first and last Sunday in February, regardless of year, the problem becomes quite a bit more complicated. The between operator appears to require a single row and passing it two equally dimensioned subqueries throws an error (1242: Subquery returns more than 1 row).
Any idea how I can solve the generalized problem?
You are describing complex patterns where the dates aren't consecutive dates within a range, but a set of individual dates.
If you need a pattern of specific dates, I would use the IN() predicate instead of BETWEEN.
It would be awkward to generate a complex pattern of dates in SQL, so use a client application with access to a robust date library. Generate a list of desired dates with more convenient methods, then use that list in an IN() predicate in SQL.
I have a table with a start and end date for each record, and a duration between the two that I have calculated. I now need to group these durations by intervals (1-29, 30-59, etc) and count how many durations fall in each category, displayed in a form/report. How should I both count the number of durations as well as filter them by size?
Construct a field with calculation that assigns an identifier for the interval groupings.
Switch([Duration]<30,1, [Duration]<60,2, [Duration]<90,3, True,4)
Or try the Partition() function. https://support.office.com/en-us/article/Partition-Function-1A846A33-60C7-4371-8E77-C94278274DC5?ui=en-US&rs=en-US&ad=US&fromAR=1
Use Report Sorting & Grouping features and do the summary aggregate calcs in textboxes on report.
I'm trying to calculate the average for only particular records
For example, we have a field called FurthestSlide. This field will contain numbers. We have another field called SlideCount. This is also a number field.
I only want it to calculate the average for the records where the FurthestSlide does not equal the SlideCount number.
What I am trying to find is the average FurthestSlide number for those people who did not view the entire message.
I do not want those who finished the message to be calculated in the data.
Sample Data:
SlideCount=40
FurthestSlide=(30,20,40,40,40)
The answer should come out to 25. (30+20)/2
You can exclude certain rows from an aggregate.
With your data:
And a simple report:
I have calculated the average for non-matching rows with the expression:
=Avg(IIf(Fields!FurthestSlide.Value <> Fields!SlideCount.Value
, Fields!FurthestSlide.Value
, Nothing))
Here we set matched rows FurthestSlide value as Nothing, which means they are excluded from any average calculation.
This gives the required result for your data: