Report Builder 3.0 How to filter on different columns - sql

I have a parameter in my report (Report Builder 3.0) called REPORT_FILTER. This parameter has 4 available values. The labels are (1) Home Group, (2) Home Branch, (3) Other Group, and (4) Other Branch. The corresponding values are 1, 2, 3, and 4.
The SQL used to run this report adds "1" to the "Filter1" column for the Home Group and "2" to the "Filter1" column for the Other Group.
The SQL used to run this report adds "1" to the "Filter2" column for the Home Branch and "2" to the "Filter2" column for the Other Branch.
When the SQL runs, I get 1 or 2 in the "Filter 1" and "Filter 2" columns. This data is just what I expect.
What I would like to do is add a filter to the report so the user only gets the records they want. What I'm struggling with is that the filter(s) will have to use 2 columns and I'm not sure how to do that.
For example, if the user selects, Home Group (value = 1), the report should only return those records that have a 1 in the Filter1 column
If the user selects, Other Branch (value = 4), the report should only return those records that have a 2 in the Filter2 column
This report already takes a while to run several minutes, so (I think) I would prefer to filter the results in Report Builder instead of with the SQL. If I could do it with SQL, that would be acceptable. I've tried to write SQL to do this but haven't been able to succeed as well.
Any suggestions would be appreciated. Thanks for the help.

Pretty easy to do in SQL, just add this WHERE condition:
WHERE
CASE
WHEN
#REPORT_FILTER = 4 and Filter2 = 2 then 1
WHEN
#REPORT_FILTER = 3 and Filter2 = 1 then 1
WHEN
#REPORT_FILTER = 2 and Filter1 = 2 then 1
WHEN
#REPORT_FILTER = 1 and Filter1 = 1 then 1
ELSE 0
END = 1
And similarly in SSRS, just use a Filters expression of
(Parameters!REPORT_FILTER.Value = 4 And Fields!Filter2.Value = 2)
OR
(Parameters!REPORT_FILTER.Value = 3 And Fields!Filter2.Value = 1)
OR
(Parameters!REPORT_FILTER.Value = 2 And Fields!Filter1.Value = 2)
OR
(Parameters!REPORT_FILTER.Value = 1 And Fields!Filter1.Value = 1)
Set the expression type to Boolean and set the comparison value to True.

Related

Dataframe change column value on if statement and keeps the new value to next row

I wish you good health to you and your family.
In my dataframe I have a column 'condition' which is filled with .astype(float).
Based on information that i put in this dataframe for every row it makes math and if is over specific amount it increase the value of 'condition' by 1 . Everything works fine with it and as it should be.
I made another column named ['order']. Which change its value if ['condition'] has value of 3. That's the code with witch you can see what I mean:
import pandas as pd
import numpy as np
def graph():
df = (pd.DataFrame(np.random.randint(-3,4,size=(100, 1)), columns=[('condition')]))
df['order'] = 0
df.loc[(df['condition'] == 3) & (df['order'] == 0) , 'order'] = df['order'] + 1
df.loc[(df['condition'] == -3) & (df['order'] == 1) , 'order'] = df['order'] + -1
df.to_csv('copy_bars.csv')
graph()
As you can see it changes the value in 'order' row to 1 when it fill first condition. But it never change back from 1 to 0 because of second if statement. It changes to 0 just because at the begging I give the row amount of 0.
How could I modify the code so when it is changed to 1 to keep this new value until second if statement fill ?
Row, Condition, Order
0 -1 0
1 3 1
2 -1 0
3 2 0
4 -2 0
5 -3 0
6 0 0
instead of this I would like to get in Order column for line from 1 to 4 to be represented with value of 1 so can my second condition trigger.
If I understood what you want this should be something like what you want. Because it is row by row and is based on two values it is not easy to vectorize but probably someone else can do it. Hope it works for you.
order = []
have_found_plus_3 = False
for i, row in df.iterrows():
if row['condition'] == 3:
have_found_plus_3 = True
elif row['condition'] == -3:
have_found_plus_3 = False
if have_found_plus_3:
order.append(1)
else:
order.append(0)
df['order'] = order

Pulling previous cell value using conditional lag function

I am trying to condense down a data table which has separate rows for a particular ID: one row has an intent string and the following rows have one or more log strings. There can be more than one set of intents/logs for each ID. I want to pull down the intent string cells in a separate column so they are listed on the same row/s as the associated log strings.
I've "tried" LAG(tobi_intent, 1,0) OVER (ORDER BY datevalue) as AssociatedIntent
but firstly, this isn't valid code, and secondly, wouldn't ensure that the associated intent and logs are for the same ID.
Can anyone advise on the correct sql code to get the output below?
expected table output:
ID log intent associated_intent
1 x
1 b x
1 a x
1 u
1 f u
2 x
2 f x
5 e
5 a e
5 s e

Lookupset with duplicate records SUM not working SSRS 2008

I have the below data in a dataset called Questions and all IDs in another dataset called Dataset1
ID Answer
1 Yes
2 Yes
2 No
2 Yes
3 No
My expected output should be as below
ID Yes No
1 1 0
2 2 1
3 0 1
I am trying to match the ids from Dataset1 and get the Answer from Questions dataset.
If I just use Lookup, it is just checking the first match and ignoring the second record. For eg, in the above data, for ID-2, it is checking the first record with id 2 and counting 'Yes' and ignoring the other 'No' and 'Yes'
=Sum(iif(Lookup(Fields!ID.Value, Fields!ID.Value, Fields!answer.Value, "Questions") = "Yes", 1, 0))
I want to count all Yes and No like shown in the expected output above
I have tried using Lookupset but I couldn't get it working. Is there any easier way without using custom code. If custom code is necessary, could you please advise on how to achieve this.
Thank you in advance.
Why do you need a second dataset?
Have a matrix on the questions dataset, row grouped by ID then use a sum in each column as follows:
=SUM(IIF(Fields!answer.Value="Yes",1,0))
=SUM(IIF(Fields!answer.Value="No",1,0))
You can count the output from a lookupset by using the following custom code:
Function CountAnswers(ByVal AnswerArray As Object(), Answer As string) As Object
If AnswerArray Is Nothing Then Return cint(0)
If Answer Is Nothing Then Return "Nothing"
Dim TotalCount As Decimal = New Decimal()
Dim AnswerPosition As Decimal = New Decimal()
TotalCount = 0
For AnswerPosition = 0 to AnswerArray.GetUpperBound(0)
TotalCount = Switch(IsNothing(AnswerArray(AnswerPosition)),TotalCount, cstr(AnswerArray(AnswerPosition))=Answer,TotalCount + 1, TRUE,TotalCount)
Next
return TotalCount
End Function
Then enter the following under "Yes" and "No" on a matrix on Dataset1:
Code.CountAnswers(Lookupset(Fields!ID.Value, Fields!ID.Value, Fields!answer.Value,"Questions"),"Yes")
Code.CountAnswers(Lookupset(Fields!ID.Value, Fields!ID.Value, Fields!answer.Value,"Questions"),"No")

IF OR Statements which always evaluate to TRUE

Recently I found an error which was due to the way I constructed an IF OR statement.
The original statement:
If a = 1 OR 2 OR 3 Then
`Execute code 1`
`Else if a = 4 OR 5 OR 6 Then`
`Execute code 2`
`Else`
`Do nothing`
The corrected code:
If a = 1 OR a = 2 OR a = 3 Then
`Execute code 1`
`Else if a = 4 OR a = 5 OR a = 6 Then`
`Execute code 2`
`Else`
`Do nothing`
So, the issue was the first part of the IF statement would always evaluate true regardless of the value of a, because it also evaluates the boolean value of the number 2, which is true. So simply spelling out that each number should be compared to the value of a fixes that issue.
My questions are:
1. When using an if statement and you have a comparative operation ( if a =1) followed by "OR 2", in what situation would you actually want to look at the boolean value of the number versus comparing the number against the value of the previously referenced variable?
Is there a way to identify if statements present in the code which will logically ALWAYS be true (and probably have a error)?
Edit: the_lotus pointed out that the boolean value of each number was not being evaluated but bitwise operations were performed between all the values and then a boolean evaluation was performed on the result. In this particular case it may be possible for it to evaluate either true or false depending on the value of a, though I am still interested in identifying if statements which always evaluate true.
As per your Edit, since you want to check if the value of A falls within a range, what you could do is store the ranges as an array and perform IndexOf to see if the value of A is in the collection.
Here is a quick example:
Dim a As Integer = 4
Dim collection1() As Integer = {1, 2, 3}
Dim collection2() As Integer = {4, 5, 6}
If Array.IndexOf(collection1, a) > -1 Then
Console.WriteLine("a = 1, 2, or 3")
ElseIf Array.IndexOf(collection2, a) > -1 Then
Console.WriteLine("a = 4, 5, or 6")
Else
Console.WriteLine("a is not a value between 1-6")
End If
Fiddle: Live Demo

Business Objects CountIf by cell reference

So I have a column with this data
1
1
1
2
3
4
5
5
5
how can I do a count if where the value at any given location in the above table is equal to a cell i select? i.e. doing Count([NUMBER]) Where([NUMBER] = Coordinates(0,0)) would return 3, because there are 3 rows where the value is one in the 0 position.
it's basically like in excel where you can do COUNTIF(A:A, 1) and it would give you the total number of rows where the value in A:A is 1. is this possible to do in business objects web intelligence?
Functions in WebI operate on rows, so you have to think about it a little differently.
If your intent is to create a cell outside of the report block and display the count of specific values, you can use Count() with Where():
=Count([NUMBER];All) Where ([NUMBER] = "1")
In a freestanding cell, the above will produce a value of "3" for your sample data.
If you want to put the result in the same block and have it count up the occurrences of values on that row, for example:
NUMBER NUMBER Total
1 3
1 3
1 3
2 1
3 1
4 1
5 3
5 3
5 3
it gets a little more complicated. You have to have at least one other dimension in the query to reference. It can be anything, but you have to be counting something in conjunction with the NUMBER dimension. So, the following would work, assuming there's another dimension in the query named [Duh]:
=Count([NUMBER];All) ForAll([Duh])