Showing posts with label BISM. Show all posts
Showing posts with label BISM. Show all posts

Tuesday, July 30, 2013

Use DAX Search Function to Retrieve Subset Data in SSRS Report

Most of you would be familiar with substring search in T-SQL. If you have a SSRS report using DAX, do you ever winder how to do the search using the pass in parameter?

Here is the sample DAX query used to retrieve the Order Count and Sales Amount for Customers from Internet Sales:

EVALUATE

SUMMARIZE (

CALCULATETABLE(
'FactInternetSales'
)
,DimCustomer[CustomerName]
,DimCustomer[Gender]
,"Order Quantity", [Order Count]
,"Sales Amount", [Sum of Sales Amount]

)



Here is the sample result:

 

image

 

If you only want to return certain customer, you will need to add a filter by pass in the Customer name as the report parameter.  Here is the sample DAX query:

 

 


EVALUATE

SUMMARIZE (

CALCULATETABLE(
'FactInternetSales', DimCustomer[CustomerName] = @CustomerName
)
,DimCustomer[CustomerName]
,DimCustomer[Gender]
,"Order Quantity", [Order Count]
,"Sales Amount", [Sum of Sales Amount]

)



Then you could get the result that has exact match to the customer name passed in.

 

image

If you need to return all the customers that has name like “Peter”, you could use DAX Search function to retrieve the list.  Here is the example:

 


EVALUATE

SUMMARIZE (

CALCULATETABLE(
'FactInternetSales',
IFERROR(Search(@CustomerName, DimCustomer[CustomerName]), -1) > 0
)
,DimCustomer[CustomerName]
,DimCustomer[Gender]
,"Order Quantity", [Order Count]
,"Sales Amount", [Sum of Sales Amount]

)


 

The above query equals to the following T-SQL:


SELECT 
[CustomerName] ,[Gender], [Order Count],[Sum of Sales Amount]
FROM [dbo].[FactInternetSales] F
INNER JOIN [dbo].[DimCustomer] C
On C.CustomerKey = F.CustomerKey
WHERE C.[CustomerName] like @CustonerName





You can see the result as below.  All the customers with name containing “Peter” got returned.

 

image

 

Sometime you may also want to handle the situation when user passed in Space or Blank parameter.  Here is a technique you could use.  When Blank customer name entered, the report will return all the customer without any filter.


EVALUATE

SUMMARIZE (

CALCULATETABLE(
'FactInternetSales',
IF(@CustomerName = blank(), 1=1, IFERROR(Search(@CustomerName, DimCustomer[CustomerName]), -1) > 0 )
)
,DimCustomer[CustomerName]
,DimCustomer[Gender]
,"Order Quantity", [Order Count]
,"Sales Amount", [Sum of Sales Amount]

)


 

Here is the result:

 

image

 


Reference




Saturday, September 22, 2012

Create SSRS Report using DAX

 

We could query BISM Tabular model using either MDX or DAX. People are very familiar with how to use MDX in SSRS.  Using DAX in SSRS is totally different from using MDX.  Here is an example to create SSRS report using DAX language:

image

List of Steps:

1. Open SQL Server Data Tools to create a SSRS report.

2. Add an existed BISM Tabular Model as the Shared data source.

3. Write the DAX in the SSMS or DAX Studio first. The DAX Studio is an Excel Add-Ins that you could download from the CodePlex.

EVALUATE
SUMMARIZE
    (    
        CALCULATETABLE( 'Internet Sales'
        ,'Product Category'[Product Category Name] = "Bikes"       
    ) ,
                  
 'Date'[Calendar Year],
 'Product Category'[Product Category Name],               
  "Total Sales", 'Internet Sales'[Internet Total Sales],               
  "Total Tax Amount", 'Internet Sales'[Internet Total Tax Amt],              
  "Total Margin", 'Internet Sales'[Internet Total Margin]           
 )

 


4. Create SSRS dataset




    • Add a new dataset and connect to the BISM Tabular Model
    • Open Query Designer
    • Click on the Command Type DMX button on the toolbar to switch to the DMX query designer.

image




    • Click on the Query/Design Mode button to switch to the Query mode.

image




    • Paste or type your DAX query into the Query window and then click on OK button.
    • Click on the Fields tab to fix/rename the field name to make it more readable. The field name generated by the query designer will not lineup with the column name you have in the DAX query.

 image




    • Now, you could add those fields to the report.


5. Add parameter



  • Go to dataset query, open the query designer.
  • Change the DAX query by replacing the filer part of Query with parameter name and click on the Parameter Button to open the Query Parameters window

image


image



  • Add Parameter and set the default value, then click OK to exit out the designer
  • On the report data window, you will find a parameter been created with default value that you typed in.

image


6. Create parameter dataset



  • There is no parameter query generated by the tool. You need to create it if you allow user to pick from the drop down list.
  • Write a DAX query with VALUES() function using the same steps before to create a dataset that will contain the list of parameter values.

EVALUATE 
values('Product Category'[Product Category Name])
ORDER By 'Product Category'[Product Category Name]




  • Associate this dataset to the report parameter

image



  • Now your SSRS report should work with single value selection.

Report Parameter Allows Multiple Values


If you allow multiple values for the report parameter, you could not use filter with equal sign (“ = ”) in the DAX query.  You could use PATHCONTAINS() function in the filter part of query to filter the result set.


PATHCONTAINS(<path>, <item>)


Since the multi select parameter string pass in will contain extra special characters that could not be used for the Path() function, you need to use Substitute() function to replace them.


Here is the complete DAX query for the report.



EVALUATE
SUMMARIZE(
    CALCULATETABLE( 'Internet Sales'
            ,PATHCONTAINS(
                    substitute( 
                        substitute( 
                            substitute( 
                                    @CategoryName
                              , "{ ", "") 
                        , " }", "") 
                    , ",", "|") 
        ,'Product Category'[Product Category Name] ) 
    
),                
 'Date'[Calendar Year],
 'Product Category'[Product Category Name],               
  "Total Sales", 'Internet Sales'[Internet Total Sales],               
  "Total Tax Amount", 'Internet Sales'[Internet Total Tax Amt],              
  "Total Margin", 'Internet Sales'[Internet Total Margin]           
 )

 

After you update your query, the SSRS report will work for multiple selections from the user now.

 

 


Reference:


http://daxstudio.codeplex.com/


http://msdn.microsoft.com/en-us/library/gg492182.aspx

Tuesday, June 5, 2012

The Simple DAX Functions as SELECT Statement


Do you even wonder how to use DAX function to query BISM to return a simple dataset or a dataset that is grouped by some attributes? 

Evaluate is the core DAX function that returns a table of data. It is similar to the SELECT statement in T-SQL.

Here is the Syntax:

EVALUATE <table>  




Example:

We want to select all the data from the Internal Sales table.







EVALUATE('Internet Sales')



By using the Evaluate function, the query returns all the rows and columns from the Internet Sales as below:


image

To sort the result,we could add ORDER BY at end. For example, we want to sort the result by Customer Id and product ID, we could do the following:


image

CalculateTable is a function that return a table that modified by the giving filters. It is similar to the SELECT * statement with WHERE Clause in T-SQL



Here is the Syntax:



CALCULATETABLE(<expression>,<filter1>,<filter2>,…)






This function takes expression for a table as the first parameter and any number of Boolean expressions as filter.



Example:




We want to return the Internal Sales records that were ordered in 2007 and the [Product Category] equals to “Bikes”.







EVALUATE
CALCULATETABLE(
 'Internet Sales',
 'Date'[Calendar Year] = "2007", 
 'Product Category'[Product Category Name] ="Bikes" 
)
ORDER BY 'Internet Sales'[Order Date]



By using the CalculateTable function, the query returns the records that match the filters provided.




image
Summarize is a DAX function that returns a table for the requested totals over a set of groups. It is similar to the SELECT statement with Group By in T-SQL.




Here is the Syntax:







SUMMARIZE(<table>, 
<groupBy_columnName>[, <groupBy_columnName>]…[, <name>, 
<expression>]…)


This function takes table of data as the first parameter, any number of columns as group by parameter, the name given to the Sum of the column, and the expression



Example:


We want to Sum up the total of [Internet Total Sales], total of [Internet Total Tax Amt], and total of [Internet Total Margin] FROM the [Internet Sales] table grouped by the [Calendar Year] and the [Product Category].




EVALUATE

SUMMARIZE('Internet Sales',
'Date'[Calendar Year],'Product Category'[Product Category Name],
"Total Sales", 'Internet Sales'[Internet Total Sales],
"Total Tax Amount", 'Internet Sales'[Internet Total Tax Amt],
"Total Margin", 'Internet Sales'[Internet Total Margin]
)ORDER BY 'Date'[Calendar Year],'Product Category'[Product Category Name]


By using the Summarize function, the query returns the sum of the measures that are grouped by the columns provided.


image



Reference:




http://msdn.microsoft.com/en-us/library/gg492156.aspx



http://msdn.microsoft.com/en-us/library/ee634760(v=sql.105).aspx

http://technet.microsoft.com/en-us/library/gg492171.aspx

Wednesday, May 23, 2012

Deployment Options for BISM Project

 

There are two Query Mode options you could use when you deploy the BISM project to the AS Tabular Model server.

    • In-Memory
    • DirectQuery

    image

    By default, data in tabular model is processed, compressed using the xVelocity in-memory analytics engine (VertiPaq).  This in-memory columnar storage engine has been optimized for high performance analysis and exploration of data.  It provides fast query times for aggregation queries.

    However, there are some drawbacks:

    • Data is not updated when the source data changes. Model needs to be processed to refresh the data.
    • When you turn off the computer hosting the model, the cache is saved to disk and must be reopened when you load the model.
    • The save and load operations can be time-consuming.
    • Need lots memory to hold the large fact data

    On the other hand, DirectQuery mode uses data that is stored in a SQL Server database. It lets users retrieve data directly from a SQL Server data source in  real time.  This mode also lets you create models and build reports for large data sets that cannot reside in memory.

    Here are additional benefits:

    • The data is guaranteed to be up-to-date.
    • Use the advantage of provider-side query acceleration, i.e. SQL 2012 column indexes.
    • Could use row-level security provided by the backend  database.
    • Analysis Services can perform optimization to ensure the query plan against the backend database will be as efficient as possible.

    There are some design considerations if you are planning to use DirectQuery mode:

    • During design phase, you may need to use Preview or Filter function to load subset data into your project.
    • MDX queries are not supported for a model in DirectQuery mode. You cannot use PPS or other clients that only issue MDX queries to consume it.
    • Currently this model only supports one data connection. You cannot query two or more SQL Servers from a DirectQuery enabled model.
    • Calculated columns and some DAX functions are not supported. You may need to use SQL View or other technique for that purpose.

    DirectQuery supports a hybrid deployment mode that can use either the cache or the relational source. For more information, see DirectQuery Mode (SSAS Tabular).

     

    Reference

    Formula Compatibility in DirectQuery Mode

    DirectQuery Mode (SSAS Tabular)

    White Paper: Using DirectQuery in the Tabular BI Semantic Model

    Wednesday, September 28, 2011

    Create a SSAS BISM Tabular Model Project

    To create a SSAS tabular model project, you will need to install the BIDS (now called Microsoft SQL Server Data Tools in SQL 2012) and have a SQL Server Analysis Services running in tabular mode (xVelocity in-memory analytics engine (VertiPaq)). It is recommended that the AS and the BIDS are installed on the same machine.

    Install an Analysis Services instance running in tabular mode

    • Add new feature to existed instance or add new instance

    image 

    • Select the Server Mode: Tabular Mode and add yourself as the administrator

    image 

    You could verify the AS Server mode using the SQL Server Management Studio and note the icon next to the server name in the Object Explore. You could also check the DeploymentMode property (0 =Traditional,  1 = PowerPivot for SharePoint, 2 = Tabular) in the msmdsrv.ini file.

    image

    Create New project

    • Open Visual Studio 2010 and select New project
    • There are three types of project template as shown below:
      • Analysis Service Tabular Project: Use this template to create an Analysis Service project with tabular models
      • Import from PowerPivot: Use this template to create a tabular project by extracting the metadata and data from an existing PowerPivot workbook
      • Import from Server (Tabular): Use this template to create a tabular project by extracting the metadata from an existing tabular AS server

    image

    • Once you create a project, you could import your data source by clicking on the Model on the toolbar and select the Import From Data Source to start the Table Import Wizard.

    image

    • You could either create a connection or use an existing connection to import the tables or data you need.  There are lots of choices such as Relational databases, Multidimensional Source, Data Feeds, or Text Files.

    image

    • You could change your model view from Data View to Diagram View by either clicking on the Model View on the Model or the icons on the right low corner.

    image image

    • You could manage the relationships or create hierarchies using the Diagram View.  You could create and manage measures in Data View.
    • Once you finishing modeling your project, you may deploy it to your AS Tabular model server to be used.

    image

    Issue or Error

    You may encounter the following error: Unable to connect to default workspace database server, which indicates that you have not yet set up a default workspace server.

    image

    To fix it, you could click on the Options from the Tools menu and then select the Analysis Services option to set the default workspace server and deployment server.

    image

     image

    If you are interested about how to use the model once you created, you may want to check out my other post : Publish BISM Tabular Model Database Connection on SharePoint 2010.

     

    Reference:

    http://msdn.microsoft.com/en-us/library/hh231722(v=SQL.110).aspx

    http://blogs.msdn.com/b/analysisservices/archive/2011/07/13/welcome-to-tabular-projects.aspx