Showing posts with label Variables. Show all posts
Showing posts with label Variables. Show all posts

Filtering Table data using "Request Variable" and Session varaible/OBIEE- Request Variable

When you want to filter the table data based upon the selected value in prompt

For example ,I have a table called SRC_ORDER_LINES it will show Sales amount and Order quantity based on order_id.My requirement is in dashboard i want to filter whole table data based on the VALUE(Order quantity) selected in Prompt

1) Create a session variable initialization block with following query


2) Next,variable creation and assign the above initialization block to this newly created variable

3) Double click on Variable qty,check in "Enable any user to set the value"
4) We need to add this session variable to the "BMM layer Content Level Filter" in order to filter the table


5) Double click on LTS(Logical Table Source) of SRC_ORDER_LINES click on content tab we need to give the condition under "Using where condition to filter out row counts"

6) Coming to answers create a prompt and declare a request variable with same name as session variable "Qty"

7) Add the prompt to dashboard and its respective report .when you select value of quantity as 1500 then the SRC_ORDER_LINES will limit the rows like where qty >1500,it will retrieves only those records

Below figure shows 

Quantity >1341 records

Quantity >1881 records


OBIEE Repository Variables, Filtering and Conditional Formatting

I’m currently sitting in Galway airport coming back from a few days work, and as I’ve got an hour or so free I thought I’d take a look at server variables in OBIEE. Repository and Session server variables are something you tend to encounter first when working with LDAP security, but they can also be used to create dynamic row-level filters, provide the filter values for conditional formatting and many other tasks, and it’s worth taking a few minutes to understand how they work.
Variables in the Oracle BI Server can be either repository or session variables, and are defined using the Variable Manager within the Oracle BI Administrator (Manage > Variables).
Variable1
This brings up the Variable Manager, that divides variables out into repository and session variables, with a further subdivision into static and dynamic repository ones and system and non-system session ones. You can also define variables at the Presentation Server level, these are scoped to individual dashboards and are used amongst other things for setting dashboard prompt values and integrating the dashboard with BI Publisher parameters. Here’s a screenshot of a typical Variable Manager screen.
Variable2
So what are server (repository) variables used for? Well if you’ve done any programming, either in languages like Visual Basic or on the web, variables these platforms use are the same as the ones that the BI Server uses. As with web development, you can either have server variables that hold the same value across all sessions (in BI Server terms, a “repository” variable), or variables that hold a value specific to each user session (“session” variables in the BI Server). Furthermore, repository variables can either be constants (“static repository” variables) or can have values that vary over time (“dynamic repository” variables), with the dynamic ones refreshed to a schedule via SQL calls and session variables usually set at the time of logon by reference to an LDAP server or an SQL statement. So now that’s all clear, in what sort of situation would they be used? Here’s a scenario that makes use of all these types of BI Server variable.
We have a reporting requirement where salespeople are responsible for a number of states, and these states change on a constant basis. Some of these states are shared with other salespeople and it’s not really practical to define set groupings of these states, hence we have a lookup table on our database that lists out the states each salesperson is responsible for, together with a marker against one particular state that is their home state. We want to use these lists of states as dynamic filters on the reports our salespeople run, and have reports default to their home state when they view their data in a dashboard. We also want to only show data for the current month in their reports, and hold the state in which our headquarters is based in a constant so that we can highlight the sales that are made near to it. In other words, we’ve got a good example of where variables in various shapes and forms can be used to make this process a whole lot easier to implement.
To start off, I use the Variable Manager to define a new static repository variable that I call HEAD_OFFICE_STATE and set to the value ‘NY’. This initial value, that stays constant for static repository variables, is called the “default initializer” and can either be typed in or set via an expression. The only way this variable value can change is if I go back into the Variable Manager and change it there.
Variable3
Next I create the second repository variable, this time a dynamic one, that is used to return the current month name and year to any query that requires it. Unlike static repository variables this variable’s values change over time, using an SQL statement executed to a schedule to update its values.
Variable4
As well as having a default initializer, dynamic variables get their values from initialization blocks that provide the mechanism to execute the SQL via a schedule. To define the initialization block I press “New” and enter the SQL, refresh frequency and variable target for the block. Notice in this example that I’m running the SQL against an Oracle database connection pool, and the truncated month and year name is generated through some Oracle function names and a reference to sysdate.
Variable5
So now I’ve got my two repository variables defined. Before I create my my session variable, I first have to locate a table in my database that lists out the selection of states that each salesperson is working with. Notice how there’s sometimes more than one row per salesperson.
Variable6
I now move on to creating the session variable. To use the data from the table above to provide the values for my dynamic filter, I first of all make sure that this table is accessible through a connection pool in the BI Server physical layer, and then go back to the Variable Manager to define my session variable. Now if this variable was going to hold a single, scalar value, I could define it as a regular non-system session variable, but as it need to hold data from more than one database row, I instead define it using an Initialization Block and row-wise variable initialization, which defines one or more variables based on a select statement, dynamically creating the variables as needed and concatenating the results of multiple rows into the variable.
To perform this task I first define a the new Initialization Block and call it STATE_SECURITY. I then define my SELECT statement as the following:
select 'STATE_FILTER',state from variable_example.salesperson
where salesperson_name = ':USER'
This will return one or more rows with STATE_FILTER, the new dynamically-created variable name, as the first column, and the set of allowed salesman states as the second value, filtered on the logged-in user ID. My initialization block now looks like this:
Variable7
To assign the results of this SELECT statement to a variable, the STATE_FILTER variable, I then click on the “Edit Data Target” button and select Row-Wise Initialization, rather than try and assign the results to individual variables. The initialization block will then take all the rows that have STATE_FILTER as the first column and create a single concatenated, comma-separated list out of the second column, so that I can then apply this value to a filter.
Variable8
Now that the session variable definition is complete, I move over to the Security Manager application, create a group for all of my report users and then define a filter for that group against the SALES table, the one I want to restrict access to. The filter references the session variable I just created, using an equality (“=”) operator rather than the “in” operator you’d have expected, this is a requirement for row-wise variables and OBIEE handles the translation properly in the background.
Variable9
Now if one of the affected users logs in an runs a report against that table, the results are filtered down without any intervention on their part.
Variable10
Conditionally formatting the State column based on whether the value each row contains is also fairly straightforward. When you create a conditional format condition you can specify that that value tested against is a variable; to access one of the repository variables you put biServer.variables[''] around the variable name so that, in my example, the variable name becomes biServer.variables['HEAD_OFFICE_STATE'].
Variable11
Displaying the report now shows all instances of “NY” highlighted in red, based on my conditional formatting rule and the value of the HOME_OFFICE_STATE static repository variable.
Variable12
So there you have it. Moving on from here, filtering the report again based on the dynamic repository variable is just a case of referencing the CURRENT_MONTH variable in my filter, and adding another session variable to hold the salesperson’s home state involves creating another initialization block that this time provides a value for a regular (i.e. not row-wise) HOME_STATE session variable.

Session Variable Manipulation in Answers

Recently I worked on a project that used Non-System Session variables for some of the reports. While on the project I learned a nice trick on how to modify value of these variables in Answers to perform testing without having to jump out to a dashboard.

In this sample Answers report I'm using a Non-System Session variable called ReportDate to calculate the employees most recent department and compensation for head count purposes.
The report below shows the head count and compensation numbers as of May 31, 2010 since that is what I defaulted the session variable to in the initialization block.

Now I can modify the value of the ReportDate session variable by clicking on the "Advanced" tab at the top and then scrolling down to the "Prefix" portion of the page. Within the "Prefix" box I can enter the following syntax to change the date to January 1, 2009: SET VARIABLE ReportDate='01/01/2009';



Here is the new screen shot of the report showing the break out of employees for January 1, 2009.


If you need to modify multiple Non System Session Variable values simply delimit them with a comma like so...SET VARIABLE ReportDate='01/01/2009', SessionVariable2='Something', SessionVariable3='SomethingElse';

Using Repository Session and Presentation Variable in Title/Subtitle/Narratives

Sometime Business demands to show the Repository Session and Presentation variable in Title , Subtitle , Narrative etc . Now let see the way to achive this :
Using Presentation Variable in Title,Subtitle,Narratives
Defined a variable called ‘var_ename’ as Presentation Variable.
Variable in Title1
Define the title with ‘var_ename’ and use it as below : @{var_ename} .Also same can be defined in Subtitle and Narrative .
Variable in Title2
The default looks of the report will be below where Ename has been “Is prompted”  in report criteria.
Variable in Title3
After choosing the prompt Title, Subtitle and Narrative would be changing automatically :
Variable in Title4
Using Repository Session Variable in Title,Subtitle,Narratives
Customer requirement was to show dynamically the Currency type of the country in report title to show the Sales currency of the individual country in multi national deployment in the same report .
To achieve this I have written a Session variable initialisation block called “Country_Int” and return the currency value for the user from LOV type and make a Non-System session variable target called “CURRENCY” .
Now in my Sales Report it has been picked up by Presentation services after defining the below syntax under Subtitle :
The currency displayed is @{biServer.variables['NQ_SESSION.CURRENCY']}.
Variable in Title5
Similarly variable could be used in Narratives as RPD dynamic variable  ‘var_country’ added under Prefix section of the BI Narrative view .  
Variable in Narrative

Variables in direct database requests

In Answers we have possibility to write SQL directly to the database.


I'll show how to use repository, session and presentation variables in direct database request and whether this is possible or not and compare this with normal Answers request.

Repository initialization block:
select to_char(min(time_id), 'dd.mm.yyyy') from sales

Repository variable (dynamic):
rv_test_date_to_char

Session initialization block:
select 'Photo' from dual

Non-system session variable:
CAT, enable any user to set the value checked, without default initializer

Dashboard prompt fields:
PRODUCTS.PROD_CATEGORY, drop-down list, set request variable CAT
CHANNELS.CHANNEL_DESC, drop-down list, set presentation variable pv_channel_desc

The first one re-sets session variable and the second one sets presentation variable.

Normal Answers request columns and filter:
PRODUCTS.PROD_CATEGORY
VALUEOF(NQ_SESSION.CAT)
VALUEOF(rv_test_date_to_char)
'@{pv_channel_desc}'
PRODUCTS.PROD_CATEGORY is prompted

Direct database request:


SQL statement:
select
'VALUEOF(NQ_SESSION.CAT)' session_variable,
'VALUEOF(rv_test_date_to_char)' repository_variable,
'@{pv_channel_desc}{Internet}' presentation_variable,
channel_desc
from channels
where channel_desc='@{pv_channel_desc}{Internet}'

We see inside the statement what is the syntax for referencing variables, for that I know that works correctly.

Now if we put all three objects in the dashboard page at initial we get this:


We change values from the prompt and re-set session and presentation variable:


Everything works fine in the direct database request except we cannot view new value of non-system session variable no mather how many times we refresh (re-set) it, it only takes value that we defined in the initialization block code. New (refreshed) value affects only Answers request.

Using multiple values row wise session variables in dashboard prompts and requests

In this post I'll show how to use multiple values session variable in the dashboard prompt and how to re-set it and use it in the request.

There are some questions in OTN forum like this one
http://forums.oracle.com/forums/thread.jspa?threadID=962729&tstart=15 about using multiple values in variable and how to put them in the dashboard prompt as predefined values.

First at all the only way to achieve this is using multiple values row-wise session variable in initialization block. You are able to use it only in the WHERE condition of the SQL Results of the dashboard prompt and not in the prompt edit column formula.

If we do this in prompt edit column formula we get error:

The session variable, NQ_SESSION.VARIABLE, is defined as Row-Wise Initialization. It is restricted to usage with equality comparisons.

The same error we get if we make the same in the Answers edit column formula.

So if you intend to use it in the WHERE condition a mandatory thing is to have table in the presentation layer with the same data, to filter the same data that already exists in the table.

At this point I ask myself the question why would we use variable with multiple values at all? We could simply hardcode any value or any set of values in the SQL Results in WHERE condition of the dashboard prompt.

Note that you cannot use multi-select control and have possibility to set presentation or session variable from the dashboard prompt. So in this example we'll use drop-down list control and leave possibility to set session or presentation variable.

So if you have for example SQL Results like:

SELECT PRODUCTS.PROD_NAME FROM "Normal model" where PRODUCTS.PROD_CATEGORY=('Hardware', 'Electronics')

you could simply replace it with:

SELECT PRODUCTS.PROD_NAME FROM "Normal model" where PRODUCTS.PROD_NAME=VALUEOF(NQ_SESSION.PRODUCTS)

Where PRODUCTS session variable is a multiple values session variable that we get from the row wise initialization block.

With using multiple values in variable the advantage is that you don't harcode value in the dashboard prompt and the end user don't see the code and if you want to change set of values you do that in the RPD be changing the code of your initialization block.

So lets take one simple example of using multiple values row wise session variable.

Example one

Initialization block:

select 'PRODUCTS', prod_name from
(
select prod_name as prod_name from sh.products
where prod_category in ('Hardware', 'Electronics')
)

We use row-wise initialization:


Variable test:


So, this is our list of the products that we'll use in the dashboard prompt and request. We use only products that are in the list, for the Hardware and Electronics product group.

Report in Answers:


Dashboard prompt:


SQL Results:

SELECT PRODUCTS.PROD_NAME FROM "Normal model" where PRODUCTS.PROD_NAME=VALUEOF(NQ_SESSION.PRODUCTS)

Our list:


We put dashboard prompt and request on the dashboard page.

In the list we have only 15 products and at initial request start we get more products than we have in the list because the prompt is not filled with any products in the list and we get all products from the PRODUCTS table (we use IS PROMPTED in the request):


The same is for All Choices.

The query is:
select T21473.PROD_NAME as c1,
sum(T20550.QUANTITY_SOLD) as c2
from
SALES T20550,
PRODUCTS T21473
where ( T20550.PROD_ID = T21473.PROD_ID )
group by T21473.PROD_NAME
order by c1

How to get only products that are in the list, at initial start, not all products from the table?

1. Duplicate initialization block that sets PRODUCTS row-wise session variable:


In the new block write:
select 'PRODUCTS2', prod_name from
(
select prod_name as prod_name from sh.products
where prod_category in ('Hardware', 'Electronics')
)

2. In the dashboard prompt turn off All Choices:


In the SQL Results add the line:
union all
select case when 1=2 then PRODUCTS.PROD_NAME else 'All Choices' end From "Normal model"

3. Set request (session) variable PRODUCTS2 in the dashboard prompt:


4. In the Answers report remove IS PROMPTED and add in advanced convert this filter to SQL option:


(VALUEOF(NQ_SESSION.PRODUCTS2)='All Choices' and PRODUCTS.PROD_NAME=VALUEOF(NQ_SESSION.PRODUCTS)) or PRODUCTS.PROD_NAME = VALUEOF(NQ_SESSION.PRODUCTS2)

Explanation what we did:

At initial, the report goes to OR part and returns all products (all products that we have defined in the list). For that we use PRODUCTS2 session variable (cloned). We harcode All Choices in the prompt, and initialize it so we need to re-set session variable PRODUCTS2 with this value. If PRODUCTS2 are All Choices we use original PRODUCTS session variable to get again all rows (first part). And if we choose single value from the prompt we re-set PRODUCTS2 session variable and use the OR part of the query.

We could resolve this by set presentation variable in the dashboard prompt pv_products and use only this code in the report filter:

(
('@{pv_products}' like '%pv%' or '@{pv_products}' ='')
and PRODUCTS.PROD_NAME=VALUEOF(NQ_SESSION.PRODUCTS)
)
or PRODUCTS.PROD_NAME = '@{pv_products}'

If you are using presentation variable remove union all part from the SQL Results and leave All Choices box as enabled on the dashboard prompt:


Test

Initial start:


Only products that are included in the prompt list.

NQQuery.log:


Now, set value to Bounce and GO:


If you now look the NQQuery.log you'll see that we set PRODUCTS2 session variable with choosen value (Bounce):


The same result we get if we use the combination of presentation variable pv_products and multiple values row wise session variable.

Now lets look another example, more complex.

Example two

For the UserA and UserB we'll read product groups from our row-wise initialization table (in this example it is more like LOV table) and populate it into row-wise initialization block that return product list for each user and product group.

I'll use the row wise initialization table like in my previous post
http://108obiee.blogspot.com/2009/03/external-table-authentication-and-row.html

Create four new entries for the user UserA and UserB in the table:


Initialization block:


Leave duplicated block from previous example and update the code inside:


When used, these blocks and coresponding row-wise session variables will return all products that users can see, UserA sees only products in the Hardware and Electronics and UserB Software/Other and Photo product group.

We used the same request with filter and dashboard prompt like in previous example.

Test

Log in with UserA:

Initial start (all products visible for UserA, 15 products):


Product list for UserA:


Log in with UserB:

Initial start (all products visible for UserB, 35 products):


Product list for UserB (notice All Choices that we add in the prompt):