SSRS Expression: First(Value) but only if Value2 = X - reporting-services

Sorry, not sure how much sense my title makes.
Basically...I have a report and the dataset (Named DataSet1) returns data something sort of like this.
Quarter MonthID Month Value1 Value2 QtrAvgValue1 QtrAvgValue2
Q-1 1 Mar 2017 12 28% 12.3 24.0%
Q-1 2 Feb 2017 15 12% 12.3 24.0%
Q-1 3 Jan 2017 10 32% 12.3 24.0%
Q-2 4 Dec 2016 18 25% 17.3 24.3%
Q-2 5 Nov 2016 14 18% 17.3 24.3%
Q-2 6 Oct 2016 20 30% 17.3 24.3%
The last two columns are just the averages for that rows entire quarter. The [Quarter] column is read like -1 means last quarter, -2 means the quarter before. The MonthID column is just an auto increment that goes up for every month older going back. So the most recent month would be 1, the next month older would be 2.
I need an expression that will let me say something like this:
WHEN Quarter = Q-1 THEN return QtrAvgValue1; Which would return 12.3;
I would also need an expression that could say:
WHEN MonthID = 1 THEN return Value1; Which would return 12;
Is this possible? I would hate to have to pivot my dataset to be a crap ton of columns with ridiculous column names
NOTE: I have tried using IIF with SUM/AVG and it works with numerical values as a sort of hack...but I cannot think of a way to handle string values.

UPDATE: I figured it out, using the Lookup and LookupSet functions in SSRS, which I have never used before.
Example:
=LookupSet("Q-1", Fields!Quarter.Value, Fields!QtrAvgValue1.Value, "DataSet1")(0)
LookupSet is normally used for One to Many relationships, but it will still work in one to one relationships as well.
This expression basically says to look up all rows that have the Quarter of "Q-1" and return the column [QtrAvgValue1].
The Lookup set function puts data into an array, so the "(0)" at the end indicates to use the first value in the array...essentially the same thing as the "First()" function.

Related

Populate a cell in SSRS report by testing value conditions from other column value

Populate a cell in SSRS report by testing value conditions from other column values
I have 3 columns (category, Month, Amount) in my dataset, with values similar to these below:
Category Month Amount
A Jan 20
A Feb 25
A Mar 10
R Jan 15
R Feb 50
R Mar 55
On the report I need:
Jan Feb Mar
A 20 25 10
R 15 50 55
I have tried placing this expression in each group row column, for example, in the "Feb" column it would be:
=IIF(Fields!Category.Value = "A" and Fields!Month.Value = "Feb", Fields!Amount.Value, 13)
Using IIF, which does not short-circuit, is not evaluating the conditions properly. The "Month" value being a string, it displays always the first row found for the conditions, in this case it would display value 20, instead of 25. If I change the "Month" value to int, it displays the false(13);
How can I populate my cell correctly, using IIF or something other way?
Any help on this is deeply appreciated.
You don't need to do anything like that.
Just use a matrix instead of a table.
Add the matrix to the report, you'll see three placeholder names, (from memory) something like Rows, Column and Data. Drag your Category field to the rows cell, your month field to the columns cell and your Amount field to the data cell. That's it....
The only problem you will have is the columns would be ordered alphabetically by default. It would be better to have the Month number in your dataset too so you can order by that. You set the column order by right-clicking the column group name in the row/column group panel which is under your main report design area.

SSRS Grouped total sum issue

I'm creating a SSRS report that shows projected sales for the next 12 months, grouped by ProductID. While the detail cells are showing correctly, the group sums for each month are displaying all sales for the 12 months rather than just the related month.
For example, here are all the table values for a single Product:
ProductID EstimatedDate ProjSales
123A Oct 10/2017 100
123A Nov 15/2017 100
123A Dec 01/2017 100
123A Dec 31/2017 100
However, this is what the report is currently showing for this Product:
Product EstimatedDate Oct 2017 Nov 2017 Dec 2017
123A Oct 10/2017 100 0 0
Nov 15/2017 0 100 0
Dec 01/2017 0 0 100
Dec 31/2017 0 0 100
Total 400 400 400
As seen above, the detailed cells calculate perfectly as each record in the detail section displays a Projected sales value if the Year/Month matches, otherwise it displays 0. Unfortunately, the final row with the "Total" amounts is incorrect as the monthly cells are showing the total of projected sales for all months rather than just the month in question.
Here are my report expressions for December 2017:
Detail Cell:
=IIF(Year(Fields!EstimatedDate.Value) = 2017 AND Month(Fields!EstimatedDate.Value) = 12, Fields!ProjSales.Value, 0)
Grouped Cell
=IIF(Year(Fields!EstimatedDate.Value) = 2017 AND Month(Fields!EstimatedDate.Value) = 12, SUM(Fields!ProjSales.Value), 0)
Any idea how I can change the grouped expression to retrieve the projected sales for each month?
Edit : format code
Use a Matrix.
Add a column group and group by year then month.
Then simply have ProjSales in the detail row group and SUM(ProjSales) in the Group total. There is no need to use expressions to calculate the values.
The column group captions will have to be expressions in order to pull out the month and year from the EstimateDate column. If you have the stored dates in a more conventional format YYYYMMDD etc then it would be easier as I'm not sure any DATE functions will recognise the format you show above.
If you need a more detailed answer let me know. I'm not able to do anything more at the moment.

write a condition in SSRS as specified

I am trying to find out how can I write the below logic in SSRS:
The structure of my report is:
All the rows and columns are grouped together.
The New Week Status has 2 fields: Current Week and Previous Week.
Current Week ranges from 30th Jan - 5th Feb
Previous Week ranges from 23th Jan - 29th Feb
The Gross Sales is in %.
I need to display only those restaurants where at least 5 days in Current Week and Previous Week is above 2% Gross.
Example:
In the above example, R1 has more than 5 days > 2%, so it will be displayed
R2 has less than 5 days > 2%, so it won't be displayed
How can the logic be implemented for this?
If you are not calculating the Gross Sales in the report this could work for you.
Select the whole Restaurant row and right click it, select Row Visibility... option:
Select Show or hide based on an expression: and use this expression:
=IIF(SUM(IIF(Fields!Gross_Sales.Value> 0.02,1,0))>=5,False,True)
Now only rows with at least 5 dates that have more than 0.02 (2%) Gross Sales are visible.
UPDATE: Adding examples.
I've recreated your dataset and the matrix, so having this structure:
Using the expression I posted above it hides the R2 row as expected.
UPDATE:
=IIF(
SUM(IIF(Fields!Total_Cost.Value/Fields!Sales_Gross.Value>0.02,1,0))>=5
or
SUM(IIF(Fields!New_Week_Status.Value = "Current Week" and Fields!Total_Cost.Value/Fields!Sales_Gross.Value>0.02,1,0))>=3
,False,True)

Calculate percentage in ssrs report

I have one ssrs report having matrix table which looks like this
YEAR A1 B1 Total percentage
2012 23 11 34
2013 12 12 24
2014 32 43 75
here i need to find percentage using below formula in ssrs expression
(Total of current year-Total of previous year)/(Total of previous year)*100
[for ex take 2012 - (Total for 2012-Total for 2011)/(Total for 2011)*100
The easiest way is to add a column to your data set that had the previous year total. Then you would right click the Percentage field and create an expression like the one above you want to calculate.
Expression Examples
One way easy way would be to use the Function previous, in SSRS this function returns the previous row item. Calculating the percentage would then be something like:
((Previous(Count(Fields!Day.Value))-ReportItems!Textbox22.Value) / ReportItems!Textbox22.Value )
Note: Textbox22.Value would be the base item/count you are comparing as a percentage.

SSRS 2008 month number not displayed in order

I have a SSRS 2008 report that generated columns of the months along with other data based on year halves. I have the tablix column group and sort set for [Mon] and the first half of the year generated just fine but when I run the report for the second half it does not display in order :
MonthNumber 10 11 12 7 8 9
MonthName October Movember December July August September
The SQL code that is used generated the following rows which appear in order of month number.
Mon
7
8
9
10
11
12
I would say that Mon is being treated as a string value, for whatever reason, i.e. from the query or in the dataset definition, as you can see that in your example the columns are being sorted as strings, i.e. 10 will be before 7 when sorted as text and not numeric values.
You have two options:
First is to sort by an expression like: =CInt(Fields!Mon.Value), i.e. explicitly sorting as an integer, which solve the issue if Mon is being treated as text.
The other option is to make sure that Mon is being treated as an integer at the dataset level - either way should be fine.