Comparing multiple fields in two datasets to return a 3rd value - reporting-services

I am in report builder and I have my primary dataset that is from a SQL database, I also then created a second dataset (enter data). I need to compare 2 fields from each dataset to retrieve the correct value from the 2nd dataset and populate a column on my report. I have tried the IIF statements and Lookup statements but I keep getting the error "report item expressions can only refer to fields within the current dataset".
I have a attached a screenshot of what I am trying to do....
The IIF statement I tried to use.. If Acctnum and prodid = each other return IncodeNumber
=IIF((Fields!AcctNum.Value=Fields!AcctNum.Value, "IncodeAccount") AND
(Fields!ProdId.Value =Fields!ProdId.Value, "IncodeAccount")),(Fields!IncodeNumber.Value, "IncodeAccount"),"True")
See code in my problem.

You need to use LOOKUP(). The problem with LOOKUP() is that is can only compare a single value from each dataset. However, we can easily get around this issue by concatenating the two values you need to compare.
Note: This assumes the expression will be in a tablix that is bound to your first dataset and that IncodeAccount is your second dataset - the values you want to lookup. If this is not the case just adjust the expression accordingly
So for you, you probably need to do something like this..
=LOOKUP(
Fields!AcctNum.Value & "||" & Fields!ProdId.Value,
Fields!AcctNum.Value & "||" & Fields!ProdId.Value,
Fields!IncodeNumber.Value,
"IncodeAccount"
)
I've used two pipe symbols to join the values to avoid incorrect matches being found. e.g. Account 123 and product ID 4567 would incorrectly match to Account 1234 and product ID 567 as they would both be 1234567 when joined. By using the || the match would be 123||4567 and 1234||567 respectively.
You may need to convert the values to string using CStr()
Alternative approach
If you are going to do this 'join' multiple times in the same dataset then you could add a calculated column to the dataset that concatenates the two columns. Then you can use this single field in the lookup which will make things a little simpler.
Or, you could do this concatenation in a database view which would make things even easier.

Related

SSRS lookup missing dataset

I have a dataset which looks like this
Name Spend
"First Aid" 2
"Healing Arts" 0
"Surgeon" NULL
I then have three separate textboxes which will be filled with the value of the column which matches the name.
Example: show value of spend in textbox if value of name equals First Aid
for this I've made following expression
=Lookup(Fields!skill_name.Value, "First Aid", Fields!skill_spend.Value, "Skills")
My problem is however that I get an error saying that skill_name is missing its dataset, which doesn't make sense to me as it is informed in the end of the expression (skills)
I think you may be misunderstanding the purpose of Lookup and how it is used. The purpose of the Lookup function is akin to a JOIN in SQL in some ways. Basically, you would have two datasets that each have a matching field with the other. In that scenario, the expression would match on the skill_name field and lookup the skill_spend value and the expression would look something like the following.
=Lookup(Fields!skill_name.Value, Fields!skill_name.Value, Fields!skill_spend.Value, "Skills")
As the documentation shows, the first reference to skill_name is the field you are referencing from the current dataset. The second reference is to the dataset from which you are attempting to look up a value. The third expression is the field you are looking up and the dataset should be the one you are attempting to look up a value from, not the current dataset scope.
Lookup(source_expression, destination_expression, result_expression, dataset)
From the best I can tell, you have a single dataset but separate textboxes that need the correct spend value. I think the following expression will work.
= IIF(Fields!skill_name.Value = "First Aid", Fields!skill_spend.Value, Nothing)
This expression should get the skill_spend value associated with the row "First Aid" only and leave the textbox blank otherwise.

Filter Multivalue Parameter on Dataset

So I have a multiple value parameter than contains 3 options. >250K, <250K, >2M.
I also have a table that consists of multiple columns.
. Because the parameter is a multivalue, i am having difficulties filtering the dataset.
I need to filter the dataset by checking, (if > 250K is selected, filter the dataset accordingly), (if < 250K is selected, filter the dataset accordingly) and (if > 2M is selected, filter the dataset accordingly).
I was told to use a join and split on the parameter within the (>250K condition, then do a contains to see if it contains any of the parameter values) but I am not as advanced in my knowledge of coding to be able to do that.
Any Suggestion? Thanks in Advance
I previously tried the method below but then i came to realise that it wont work because the parameter is a multi value.
I know its been a while since you raised this, you were on the right track but all you should need to do is add a filter to the Tablix on the field you will be filtering, use the 'in' operator and in the Value type [#Yourparametername] the square brackets and case sensitivity are important. Also ensure the expression type is correct, in your case it looks like you are using Integer. The image should help.
If you want to use multi-parameters, In the dataset, you can read parameter value using JOIN.
Example:
If you want to read multiple values for #MyParamter in a dataset given in the following example:
Dataset Parameters
you need to use =JOIN(Parameters!myMultiParamter.Value,",") as an expression to read all selected values in CSV form.
Expression
Now the #ParameterValues param has all selected values as comma separated values and you can use them in your dataset code as per design requirements.
Note: It's not necessary to use a comma but u can use anything you want to separate values.
Your sql query where should look like
Where
(
(0 IN (#Parameter) AND ValueColumn<250000)
OR
(1 IN (#Parameter) AND ValueColumn>=250000)
OR
(2 IN (#Parameter) AND ValueColumn>=2000000)
)
One parameter
Two parameters
All parameters
Once you return the value you can also use charindex or patindex* and look for where the value in your where clause is a pattern where the index number is > 0 . For instance if the returned string from SSRS is '01,02,03' and then your where clause has something like this right(field, 2) which would result in value '03'. you change your where clause to be where patindex('%' + right(field, 2) + '%', #returnedstring) > 0 which will give you results. The keeps you from having to parse apart the #returnedstring parameter in your sql code.

Dynamically change column names as week number on every weekly run

I want to build a SSRS report that has column as week numbers - 8 weeks for 8 columns starting with current. This report is run every week and current week number is set then. So both column names and their values should change .Is it possible to build something like this in SSRS?
I tried doing this with a dynamic SQL based stored proc in dataset. However for every run I don't even see the columns values updating dynamically
Here's an example :
Also I am trying to avoid these week numbers as row values and then using matrices
My stored proc looks something like this
declare #n tinyint = datepart(wk, getdate())
declare #n1 tinyint = (#n+1), #n2 tinyint =(#n+2), #n3 tinyint =(#n+3), #n4 tinyint =(#n+4), #n5 tinyint =(#n+5), #n6 tinyint =(#n+6)
exec ('Select b.sku, b.['+#n+'], b.['+#n1+'], b.['+#n2+'], b.['+#n3+'], b.['+#n4+'], b.['+#n5+']...
Will appreciate any help in this direction.. many thanks!
When working with SSRS it's generally best to avoid dynamic SQL and pivoting the data in the SQL. Use the SQL to get the raw data you need and then let SSRS do the pivoting and aggregation. This way you take advantage of what they each do best. I know you said you want to avoid matrices, but it is the best way to make the report dynamic.
So you should either return all the data in one dataset and use filters on your matrices OR write two queries and have each one populate a matrix. BTW a matrix is just a table with a column group added, so don't be intimidated by them.
There are 2 ways to do this with a standard tablix.
Calculate the column headers as expressions using concatenation of Wk and some date math to find the correct week number and return the same sort of thing from your query (e.g. columns are current_week, week_minus_1, week_minus_2...)
Return the column headers as additional columns in your query that are the same value for every row (e.g. ColHeader0, ColHeader1...). Your data columns would still be relative weeks (e.g. ValueWeek0, ValueWeek1...). In your report the column header would have an expression like =First(Fields!ColHeader0.Value). This is a more flexible approach since it lets you pick 8 historical weeks instead of only the last 8 weeks if you add a parameter.
EDIT - Clarifications
The reason that you get the blank column Wk48 is approximately that you have created your report looking for that column that won't be there the next time. SSRS looks for exact columns. You should you use relative column names for either of the options I have specified:
exec ('Select b.sku, b.['+#n+'] as Wk0, b.['+#n1+'] as Wk1, b.['+#n2+'] as Wk2, b.['+#n3+'] as Wk3, b.['+#n4+'] as Wk4, b.['+#n5+'] as Wk5...
This will allow you to populate the aliased Wk0 column with the appropriate current week data and still make sure that it can be consistently referenced as the base week by SSRS.
To change the column headers you can:
Independently calculate the week numbers in SSRS in the column header expressions: ="Wk" + CStr(<correct week calculation>).
Return the column headers in the result set and access them in the column header expression:
exec ('Select b.sku, b.['+#n+'] as Wk0, b.['+#n1+'] as Wk1, b.['+#n2+'] as Wk2, b.['+#n3+'] as Wk3, b.['+#n4+'] as Wk4, b.['+#n5+'] as Wk5..., ''Wk'''+#n+' as ColHeader0, ''Wk'''+#n1+' as ColHeader1...
and reference the returned column headers from the SSRS column header expression as =First(Fields!ColHeader0.Value).
Here's a solution that worked for me:
Create parameters (say CurrWk, CurrWk1) ,set as hidden and store 'Default value' and 'Available value' equals to current week number (datepart(wk, now()) and any subsequent week by doing a +1, +2, +3.. etc.
Write a query expression . Click onto fx beside dataset query space and write the select query for your program embedding parameter values in the expression window. For eg ="Select SKU, [" & Parameter!CurrWk.Value & "] as Wk1,
[" & Parameter!CurrWk.Value & "] as Wk1 from Sales_Table"
Before passing this query as a 'command text expression' please ensure this query is working in sql ssms.
Save the expression. Now find 'Fields' tab on the left hand side panel.You need to map the fields manually from the query here. If this is not done, there is a very high chance you seean empty field list and wont be able to access them at all. This may be because ssrs do not store query metadata directly from expressions.
You can avoid part of the issue by having atleast the static fields , for example here SKU listed in the 'Fields' list by first running a sql query with static field(select SKU from Sales_Table ). You can then go back to update dataset- change query to expression and embed the parameterized field names.
Map field names. In this example I chose 'Query Type' fields and set Field names as SKU, CurrentWeek, NextWeek and mapped to source SKU, Wk and Wk1 respectively.
Click on 'Refresh Fields' at the bottom. Now you have a dataset with the complete field list. Use these in charts, tables . Run it every week and note the numbers changing as expected.
In case you are using this dataset in a table, make sure you set headers with Labels of Parameters (for eg here I did =Parameters!CurrWk.Label for col with current week data)
That's it!

How do I pass a value returned from one dataset as parameters to a second dataset to return a value from the second dataset in a cell expression?

How do I pass two dynamic values returned from one dataset as parameters to another dataset to return one row of a second dataset in a cell?
For each cell in a header row I need to determine A) Which column to get based off the meal period, and then B) take that value and pass it and another parameter to a query to get one of the columns (I'll take first) from the query.
I am already using cascading parameters, but I'm not sure if or how to use them in this scenario.
Example:
#SalesReceipt
#MealPeriod (A cascaded parameter. There should be one meal period per receipt)
Expression in an example cell:
=IIF(Parameters!#MealPeriod.value = "Lunch", Fields!Burger_Lunch_Type.value, Fields!Burger_Dinner_Type.value)
That returns either the lunch or dinner Type from a fact table, depending on meal period.
Given the Type and Item ID I need to get a value from another query or dataset and add that text to the cell expression. So the expression would look something like:
=IIF(Parameters!#MealPeriod.value = "Lunch", Fields!Burger_Lunch_Type.value, Fields!Burger_Dinner_Type.value) & " and " & (select top row from dataset given the correct type field and Item ID)
The cell should look like: Table1.Type and Table2.Breadtype AKA Bacon Burger and Whole Wheat.
Does that make sense?
I would use the dreaded Lookup function for this:
http://technet.microsoft.com/en-us/library/ee210531.aspx
I say dreaded because it is notoriously tricky to code and debug. I've even seen a case where a failed Lookup caused the entire row to disappear!
The Lookup target dataset itself should not use the parameters required for the Lookup - it's purpose is to return all the possible rows needed by the Lookup function.
If you need to use two columns as the key to your lookup, you can concatenate them e.g.
Fields!Burger_Lunch_Type.value & "|" & Fields!ItemID.value
Good luck!

concatenate using Allen Browne's example

I am using the 'concatenate related' module created by Allen Browne to concatenate rows into a single field. At first I had a lookup field at the table level and later realized this is not a good approach. So I deleted the lookup column and instead made a query for selecting values from the lookup table on my form and then store that value as a number in the table.
The module works when I concatenate the values but it is listing the number (id) whereas I would like the actual description (i.e. 1 = Red, 2 = Blue, etc.)
My SQL query code is as follows:
SELECT DISTINCT
tblCompany.JobID,
concatrelated("type","tblMonitor","JobID = " & [jobID]) AS Expr1
FROM tblCompany;
I would like "type" to display the description instead of the number. I know if I store my lookup value as text instead of number it will work. But for efficiency it seems the number should be stored in the table and then query for the description when you need it....or maybe text is fine??? I'm guessing I would need to add the lookup table to this query. I have tried but with no luck so far.
Create a query which joins tblMonitor with the table which holds the type description field. Then use that query with ConcatRelated.
ConcatRelated("type_descriptn","YourQuery","JobID = " & [jobID])