Spotfire Case statement not working - data-analysis

I am trying to write a case statement in which I want to categorise my column values into 4 cases. But only 1 case got created-"HV-HM". The code is:
case
WHEN Sum([Sales])/UniqueCount([Quarter])>=${HighVolume} and Avg([Net
Margin])>=${HighMargin} THEN "HV-HM"
WHEN Sum([Sales])/UniqueCount([Quarter])>=${HighVolume} and (Avg([Net
Margin])<${HighMargin}) and Avg([Net Margin])>${LosingMargin} THEN "HV-LM"
WHEN Sum([Sales])/UniqueCount([Quarter])<${HighVolume} And Avg([Net Margin])
<${HighMargin} And Avg([Net Margin])>${LosingMargin} THEN "LV-LM"
WHEN Avg([Net Margin])<=${LosingMargin} THEN "Losing"
END as [Sector]
Can anyone please fix the issue?

Too long to comment....
Each case in your statement is unique, so that means the first "case" is met for all of your data. Since you don't think this shouldn't be the case, the best thing to do is break this up into calculated columns so you can see the values. Create a calculated column for each case.
Create a calculated column for these two methods:
Sum([Sales])/UniqueCount([Quarter])
Avg([Net Margin])
Then, you can manually see if they will be above or below your High and Low margin property controls.
You can also create a calculated column for each case:
case when Sum([Sales])/UniqueCount([Quarter])>=${HighVolume} and Avg([Net Margin])>=${HighMargin} then 1 end as [Test 1]
case when Sum([Sales])/UniqueCount([Quarter])>=${HighVolume} and (Avg([Net Margin])<${HighMargin}) and Avg([Net Margin])>${LosingMargin} 1 end as [Test 2]
etc...

Related

Access Expression help - Calculated fields

I need some help with an Access expression. I keep getting a syntax error.
I'm trying to get a calculated field from the combination of another calculated field.
Note that the LaserCuttingRate field is also a Lookup field. So it's not a simple entry field. It becomes a selector of the 3 choices.
IIf([LaserCuttingRate]="200",([CuttingTime]*200/60),IIf([LaserCuttingRate]="400",([CuttingTime]*400/60),IIf([LaserCuttingRate]="100",([CuttingTime]*100/60)))
All help appreciated.
Cam
I think you're missing a final close bracket.
There are 6 open brackets ( but only 5 closing brackets )
UPDATE - second issue
Two extra issues
The checks on number either don't need, or shouldn't have double-quotation masks
The final IIF also needs a false clause at the end (in the example below, 0).
Try this - I have put it into an Access database scratch table and it worked. (If relevant, note that I had LaserCuttingRate and CuttingTime set as doubles, as well as the Result Type for the calculated column).
IIf([LaserCuttingRate]=200,([CuttingTime]*200/60),IIf([LaserCuttingRate]=400,([CuttingTime]*400/60),IIf([LaserCuttingRate]=100,([CuttingTime]*100/60),0)))

SSRS 2012 csv export - dynamic column header name

I need to export my SSRS report to csv format. Issue I am facing is that few column header names change with country code (parameter) and I want to show the same name in my exported csv. I have gone through other related questions and topic, specifically this one. It suggested that there is a work around by setting data value to null. I have tried this by adding columns and hiding these based on country code, along with setting data value to null for that dataset field. But it did not work. I still get the hidden column in my export, with no values in it.
Can someone confirm that this workaround works or is there any other way apart from creating different reports for each country?
UPDATE: (added report screenshot and description for clarification)
Based on apporoach I have taken,
I need to only show Column 'Town' for Country A and only 'Suburb' for Country B. This is easily done by hiding columns based on Country Parameter( and works fine for EXCEL export), but when exported to CSV, both columns are exported.
UPDATE 2
Found this link which is similar to what I have been trying to figure out and looks like there is no solution which can be achieved using only one report.
According to this post
MSDN Social: Hide CSV columns conditionally in SSRS report
The XML and CSV renderers use the DataElementOutput property to
control visibility. We can select which item we want to hide in the
report, and set the “DataElementOutput” property with value “NoOutput”
to work around the issue.
Alternatively, we can add below expression to control the item’s
visibility. Please refer to the expression below:
=IIF(Globals!RenderFormat.Name="CSV",True,False)
This, incidentally, is the second answer in the thread that you posted. That answer links to the following article which explains how to do exactly what you need
Hide/Show Items Dependant On Export Format
UPDATE
It appears that you cannot programmatically set DataElementOutput for CSVs. However, according to this post SSRS - Programatically controlling the DataElementOutput property
in RS 2005 / 2008, you should be able to get the desired effect by adding a filter on the tablix directly (in addition to the visibility condition):
Filter expression: =(Parameters!DataPeriod.Value = "DAY")
Filter operator:    =
Filter value:     =true
Thereby, for the cases where the tablix is not visible, you are also filtering out all the data.
You may be able to show/hide country specific columns this way instead.
My approach would be to do this in SQL. I'm not sure how you determine which Countries require the town to be returned and which require the suburb to be returned but here's a couple of approaches that hopefully cover your scenario.
In either case, the idea is to return the town/suburb data in the same column and then an additional column that will contain a a caption that we can use as the column header.
a. Only one of either the town or suburb column in your table is populated. In this case it's pretty simple.
SELECT
Country
, ISNULL(TownOrCity, StreetSuburb) AS TownSuburb
, CASE WHEN TownOrCity IS NULL THEN 'Street-Suburb' ELSE 'Town-City' END AS Caption
FROM myTable
b. You know upfront which Countries require what and you can pass a parameter in to get the correct column. In this example we'll use a parameter called #TS and pass in either a T or and S
SELECT
Country
, CASE #TS WHEN 'T' THEN TownOrCity ELSE StreetSuburb END as TownSuburb
, CASE #TS WHEN 'T' THEN 'Town-City' ELSE 'Street-Suburb' END AS Caption
FROM myTable
Whichever approach we take you will end up with a simple table
Country TownSuburb Caption
Testland TownA Town
Testland TownB Town
Testland TownC Town
In you report, you don;t need to do anything except make the town/suburb column caption an expression something like =FIRST(Fields!Caption.Value)
That's it, the report is now nice and simple and should export without any issues.
UPDATE to method:
--
-- Dump data into a temp table
--
SELECT
Country
, CASE #TS WHEN 'T' THEN TownOrCity ELSE StreetSuburb END as TownSuburb
INTO #t
FROM myTable
--
--rename the column
--
DECLARE #NewColumnname sysname = CASE #TS WHEN 'T' THEN N'Town-City' ELSE N'Street-Suburb' END
EXECUTE tempdb..sp_rename N'tempdb..#t.[TownSuburb]', #NewColumnname, 'COLUMN'
--
-- finally get the result
--
SELECT * FROM #t

lookup in ssrs and return a value

I have a table with three columns, in column 1 I have a name and column 2 I have a quantity.
I want to look up column 1 and return the value in column 2.
I used to the expression below and it wouldn't return what I wanted.
=Lookup(fields!NAME.Value, "Paul" ,1 , 0)
Could anybody tell me what expression I need to use?
You are close, sort of. To use the Lookup function properly, you need to change some things. Here is an example using the Lookup function.
=Lookup(Fields!Field1.Value, Fields!Field1.Value, Fields!Field2.Value, "DatasetB")
It takes 4 parameters. The first is the field/value from the current (in scope) data set that you want to be the value to match in the lookup data set. The second is the field in the lookup data set to match on. The third in the field to return when a match is found, and the last parameter is the name of the lookup data set.
Based on the expression in your question, it may actually work like this:
=Lookup("Paul" , Fields!NAME.Value, Fields!QUANTITY.Value , "DataSet2")
Of course, hard coding the name in the first parameter is probably not what you want to do.

Tell from Which table MySQL COALESCE returned the value

i have the following part of query:
SUM(COALESCE(user_a, user_b)) AS income_adsense
Now, i have a html table in my web app where i present the data from this query.
problem is i want to mark data in one color if answer is from col user_a and different color if answer is from user_b col.
is there a way to achieve that in my query itself? (some sort of flag maybe?)
.
right now the only solution i have is to return all col's and work with the data on the client side but i am wondering if there's a cleaner/best practice solution.
guess it's worth mentioning i don't want to change the table structure.
Well, to make sense of the data I would do something like:
CASE
WHEN SUM(user_a) > SUM(user_b)
THEN 'User_A'
ELSE 'User_B'
END [Most of the data comes from]
You could also have two separate SUM() columns to make sense of this and compare the sum of values in your application.
SUM(user_a) [User_A Score Weight]
, SUM(user_b) [User_B Score Weight]

Nested IIF statement to produce 1 of 3 results

I am trying to piece together a nested "IIF" statement to produce one of three Values but my line is not working
=IIF(Fields!LeaseNumber.value = "", "Vacant",Fields!tenantName.value) AND (IIF(Fields!tenantName.Value ="",Fields!LeaseDBA.Value, Fields!tenantName.Value))
My expected outcome should be:
If No lease# - "Vacant"
if no tenantName - "LeaseDBA"
all other rows just give me - "tenantname"
All the other questions I reviewed didn't seem to match this type of IIF clause.
Any help really is appreciated.
I've actually recently just tried to expalain how the IIF expression works and how to nest them. https://stackoverflow.com/a/35289515/4579864
To check if a value is null or empty, you're better off using one of these functions:
You can use IsNothing() to check if there is a value. Applied to your example would result to this:
=IIF(IsNothing(Fields!LeaseNumber.value)
,"Vacant"
,IIF(IsNothing(Fields!tenantName.Value), Fields!LeaseDBA.Value, Fields!tenantName.Value))
You can calculate the length of your value with Len() and then check if it is larger than 0.
=IIF(Len(Fields!LeaseNumber.value) > 0
,IIF(Len(Fields!tenantName.Value) > 0, Fields!tenantName.Value, Fields!LeaseDBA.Value)
,"Vacant" )
If No lease# - "Vacant" (If there is no LeaseNumber then show "Vacant" -String only)
If no tenantName - "LeaseDBA" (If there is not tenantName then show Fields!LeaseDBA.Value
And for other rows just give me Fields!tenantName.Value
If this is you want then try below expression.
IIF(Trim(CStr(Fields!LeaseNumber.Value))="","Vacant",CStr(Fields!tenantName.Value)) & IIF(Trim(CStr(Fields!tenantName.Value))="",Fields!LeaseDBA.Value,CStr(Fields!tenantName.Value))