=Switch(Fields!sale.Value = "1", Fields!REC_ISU.Value, "0", Fields!sale.Value = "2", Fields!REC_ISU.Value, "0", Fields!sale.Value = "3", Fields!REC_ISU.Value, "0") This code is throwing error. Can anyone help. Thanks in advance
There are problems with the expression but without knowing what you expect this to do it's hard to give a complete answer.
The SWITCH expression works as follows...
For example if we wanted to look at a brand name and return whether it was a soft drink or alcohol we could do something like
=SWITCH(
Fields!Brand.Value = "Coca-Cola", "Soft",
Fields!Brand.Value = "Fosters", "Alcohol",
Fields!Brand.Value = "Smirnoff", "Alcohol",
True, "Something else"
)
This basically says "If the brand is Coca-Cola return "Soft", if it's Fosters or Smirnoff return "Alcohol", ELSE return "Something else". SWITCH stops evaluating when it finds a condition that is true, so the TRUE at the end acts like an else, it means none of the other tests are true.
We could make this shorter by replaceing
Fields!Brand.Value = "Fosters", "Alcohol",
Fields!Brand.Value = "Smirnoff", "Alcohol",
with
Fields!Brand.Value = "Fosters" OR Fields!Brand.Value = "Smirnoff", "Alcohol",
As you can see you can combine comparisons easily.
For more info look here https://learn.microsoft.com/en-us/sql/reporting-services/report-design/expression-examples-report-builder-and-ssrs?view=sql-server-2017
Related
I am looking for help updating an expression used in a SSRS report.
The current expression is something like this:
=IIF(Fields!Type.Value = "TKT", "Ticket No.",
IIF(Fields!Type.Value = "CUN", "Customer Number",
IIF(Fields!Type.Value = "ANM", "Account Number",
IIF(Fields!Type.Value = "CID", "Client ID", ""))))
We have added a few more "Types"
So for Type "TKT" now we have: "TKT1", "TKT2". For Type "CUN", now we have "CUN1", "CUN2, and so on for the last 2.
I am not familiar with how when using an IIF function, multiple values can be specified (similar to an IN operator).
If anyone could share some light regarding how this is done, that would be awesome.
Thank you for your help in advance.
You would probably be better off converting this expression to a SWITCH statement before this thing gets out of hand. A SWITCH accepts as many conditional and result pairings as you need to add. The following expression allows you to add as many checks as you need and the final pairing true,"" simply sets any that don't match the switch statement to a blank value.
=SWITCH(Fields!Type.Value = "TKT1", "Ticket No.",
Fields!Type.Value = "TKT2", "Ticket No.",
Fields!Type.Value = "CUN1", "Customer Number",
Fields!Type.Value = "CUN2", "Customer Number",
Fields!Type.Value = "ANM", "Account Number",
Fields!Type.Value = "CID", "Client ID",
[add additional pairings here],
true, "")
An additional solution would be to use the Contains keyword in SSRS. This would search the string to find a particular substring. You could simply modify each conditional to the following which would return true if the field contains that substring.
Fields!Type.Value.Contains("TKT")
I have a column of data called Date and I am using that to perform a switch. Basically the figure needs to be 120 on a weekday and 0 on a weekend so I have used.
=SWITCH
(Weekday(Fields!Date.Value) = "1", "0",
Weekday(Fields!Date.Value) = "2", "120",
Weekday(Fields!Date.Value) = "3", "120",
Weekday(Fields!Date.Value) = "4", "120",
Weekday(Fields!Date.Value) = "5", "120",
Weekday(Fields!Date.Value) = "6", "120",
Weekday(Fields!Date.Value) = "7", "0"
)
Which works great. However I also want a Total at the bottom of the sheet. I (somewhat naively) tried adding
=SUM( ... )
to the expression but that resulted in an #Error in the textbox. I also tried
=SUM(ReportItems!Textbox85.value)
and that didn't even run throwing the error
The Value expression for the textrun
'Textbox84.Paragraphs[0].TextRuns[0]' uses an aggregate function on a
report item. Aggregate functions can be used only on report items
contained in page headers and footers.
So my question is how do I sum up this switch function or do I need to rethink this? I guess functionally all I really need is count of the total weekdays so far "this" month (that stays accurate no matter what month is picked using the date parameters). I can then * that number by 120.
So I came at it from a different angle and tried a SQL based solution. I added a column with the code
,CASE
WHEN
DATEPART(dw,convert(date,format(dateadd(hh,1,[Start Time]),'dd/MM/yyyy'),103)) in (1,7)
THEN 0
ELSE 1
End as [weekday]
Then used
=Fields!weekday.Value*120
in my textbox and
=Sum(Fields!weekday.Value, "DataSet1")*120
in my total. Got the desired results.
I have a table that will return a number and i need to convert it into a text label
20 = Entered, 30 = Returned, 200 = Cancelled, 220 = Complete, 300 = Deleted
I want these to show in my report as simply 'Complete' etc.
Im able to use the replace function to get one value to show correctly in the report:
=Replace(Fields!status.Value,"220","Complete")
But i cant work out how to do this for each possible number that will show in this column
Best way would likely be modifying the query with a CASE statement as mentioned, if you are able to do that. But if not, a cleaner alternative to the nested Replaces would be to simply use a Switch statement:
=Switch(
Fields!Status.Value = "20", "Entered",
Fields!Status.Value = "30", "Returned",
Fields!Status.Value = "200", "Cancelled",
Fields!Status.Value = "220", "Complete",
Fields!Status.Value = "300", "Deleted"
)
This is not the most efficient way to do this, but it's a quick fix:
=Replace(Replace(Replace(Replace(Replace(Fields!status.Value,"220","Complete"), "200","Cancelled"),"300","Deleted"),"20","Entered"),"30","Returned")
A better way would be to modify your DataSet query to replace the numbers with a CASE statement. See this documentation:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql
i have columns with imperial values and columns with metric values. i want to hide one or the other based on which region a customer may be from but also if they are part of the company or not. So example if person.value = "Employee" then show imperial and metric but if person.value = "Customer" then CustomerRegion.Value = "Europe" , would be shown metric and CustomerRegion.Value = "North America" would be shown imperial. what would i use to construct an expression to hide one or the other and what would be the easiest way to do it.
You would need to use an IIF statement and add the logic for the various possibilities by nesting multiple IIFs inside each other.
Since you always want to show Imperial to Employees, I would make that the first part of the IIF. For showing and hiding, one column would have one expression while the other would have the same with the TRUEs and FALSEs reversed.
Imperial Hide:
=IIF(PARAMETERS!person.value = "Employee", False,
IIF(PARAMETERS!person.value = "Customer AND PARAMETERS!CustomerRegion.Value = "North America", False,
IIF(PARAMETERS!person.value = "Customer AND (PARAMETERS!CustomerRegion.Value = "Europe" OR PARAMETERS!CustomerRegion.Value = "Asia"), True, True))
Swap the TRUE and FALSE for the Metric column.
Instead of hiding one of the columns, why not use the expression to determine which data to show in a single column? Instead of TRUE or FALSE, you could use the IMPERIAL or METRIC fields.
Apologies Hannover, I've used your answer as a template for mine.
A SWITCH statement would be a better option here IMHO. IIF evaluates all parts of the expression which can be slower whereas SWITCH stops at the first expression that returns TRUE. If more conditions need to be added, nesting IIF's quickly gets messy.
The SWITCH version would be slightly cleaner but definitely more extensible.
Imperial would be
=SWITCH(
Parameters!Person.Value = "Employee", False,
Parameters!Person.Value = "Employee" AND Parameters!CustomerRegion.Value = "Europe", True,
Parameters!Person.Value = "Employee" AND Parameters!CustomerRegion.Value = "North America", False,
True, False)
The last pair of expressions act like an ELSE, it will always return true. Here I set the return to False so by default the column would be shown (Hidden = False)
The metric column would just be the opposite except you would probably want to leave the last pair (the True, False at the end) so the metric column is also shown by default.
This could be made even simpler assuming you only ever have Employee and Customer for person.value as you would not have to check it past the first expression. You can also add multiple checks for regions in a nice consice way rather than put them on separate lines, although you could do this with the IIF answer too..
Something like this.
=SWITCH(
Parameters!Person.Value = "Employee", False,
Parameters!CustomerRegion.Value = "Europe", True,
"North America Asia Somewhere Else".ToLower.Contains(Parameters!CustomerRegion.Value.ToString.ToLower), False,
True, False)
I've used ToLower to make the comparison case insensitive but you could exclude that if you wish.
Thanks for all the insightful answers guys. They all work and give the desired result but i have chosen to go with the "Switch" Statement
I Had structured it in this way for the metric column;
=SWITCH(Parameters!Person.Value = "Employee", False, Parameters!CustomerRegion.value = "North America", True)
And i just changed CustomerRegion.value to "Europe" for the imperial columns. It worked just fine this way. Hope this helps :)
I am starting out, so this code may not be as efficient as it could be, but I try. This is a text based game, something happens and the player must decide if he wants to continue or not.
but Everyone is different. so everyone is going to answer with some variation of "yes". How can I get the program to move on if someone types any variation of "yes" the commas give me a syntax error, and i don't know how else to get it to separate the possible answers.
def continueyn():
option1_des1 = raw_input("> ")
if option1_des1 == "yes", "Yes", "forward", "Forward", "Full impulse", "Full Impulse", "yeah", "yup", "Yup", "Yeah":
gravityp1()
elif option1_des1 == "no":
bearing(), userinput()
else:
print "Sorry Captain, I didn't get that. What did you say?"
continueyn()
Using 'or', as pointed out in the comments is incorrect. The correct way to go ahead through this would be either using lists and a loop, or iterating using "in":
Method 1:
option1_des1 = raw_input("> ")
if option1_des1 in {"yes", "Yes", "forward"} :
print "yes"
elif option1_des1 == "no":
print "no"
else:
print "Sorry Captain, I didn't get that. What did you say?"
Method 2:
You can also make a list and then go across values of the list like this-
flag=True
yesList = ['yes', 'Yes', 'forward']
option1_des1 = raw_input("> ")
for element in yesList:
if option1_des1 == element:
print "yes"
flag=False
break;
if option1_des1 == "no" and flag:
print "no"
elif flag:
print "Sorry Captain, I didn't get that. What did you say?"
Even better, you can use regex to match several variations of one type instead of figuring out every permutation. The xample below only covers "yes", but you can make a list of words like in method 2.
import re
pattern = 'yes'
option1_des1 = raw_input("> ")
matchObj = re.search(pattern, option1_des1, re.I)
if matchObj:
print "yes"
elif option1_des1 == "no":
print "no"
else:
print "Sorry Captain, I didn't get that. What did you say?"
Similar to sbhatla's answer you could use a list and use the built in "in" function which returns as a boolean.
yesoptions = ["yes", "Y", "etc"]
if option_des1 in yesoptions:
#runs actions for a yes answer
elif option in:
etcetc
else:
etc..