How To Query A SSAS Tabular Model Database Using DAX Functions
How To Query A SSAS Tabular Model Database Using DAX Functions
Español
To make the first steps into the BI world easier, you just need to build one SSAS Tabular model
database. In this article, we will learn some basics of querying a SSAS Tabular model database with
simple DAX queries, starting with a theoretical approach, and then retrieve data and analyze it.
Requirements for querying with DAX include SQL Server Management Studio 2014 or higher with
an installed SSAS Tabular instance, and a deployed SSAS Tabular model database.
Querying a SSAS Tabular model database in this article will perform in SSMS, within the MDX query
window, as shown in the picture:
Note: Don’t get confused with DAX code presence in a MDX query – both of them, DAX and MDX,
can query a Tabular model database.
That trend continues, because of simplicity and fast query execution (related to DirectQuery mode
in SSAS Tabular).
Functions are the main part of the query, and they act similar to some T-SQL statements (as we
will see later). Expressions consist of values, operators and constants. The difference between ex‐
pressions in Excel (Power Pivot) and SSMS should be apparent; In Power PIvot, expressions start
with equal sign “=”, and they look like formulas:
='Product'[Standard Cost]
In this example, ‘Product’[Standard Cost] is value, and there are no operators (+, – e.g.) or
constants.
DAX expressions in SSMS are more complex, because of the whole syntax. Although there are
many rules, the following are just some examples.
evaluate Product
SELECT * FROM dbo.Product
The query result, the selected columns, would be the same as in querying the SQL Server database,
but the main difference is the naming of columns. Columns in SSAS Tabular model database don’t
use CamelCase or pascalCase naming methods. They include the actual table name in the title and
allow space characters, as shown in the picture below:
It is important to know the exact names of the elements, because of using particular tables and
columns in DAX expression(s). Regarding the syntax, format tables like this:
evaluate 'Product'
Now, the table Product in this expression is surrounded with apostrophe characters, and executing
the query shows the same result, as with ‘Product’ expression without apostrophes.
evaluate 'Product'
//or
EVALUATE 'Product'
To start creating some complex queries, format the evaluate statement as shown:
evaluate
(
‘Product’
)
evaluate('Product')
Putting brackets (as parenthesis operator) after function means that query will become complex,
although this query shows the same results as in the previous queries. Also, choose a suitable for‐
g q y p q
mat according to your personal preferences, but pay attention on the examples from this article
(number of brackets etc.).
1. VALUES function
This query will show all values of the Product Name column only:
evaluate(values('Product'[Product Name]))
Values function shows the distinct values of specified column, without duplicates:
2. ORDER BY function
evaluate('Product')
order by [Product Subcategory Id]
It orders the query result according to the selected column as a parameter. In this case, the
parameter is Product Subcategory Id.
evaluate('Product')
order by [Product Subcategory Id], [Color]
Order can be ascending or descending (ASC or DESC in the end of statement):
evaluate('Product')
order by [Color] DESC
3. START AT function
This function sets the starting point (first row) from which values will be displayed:
evaluate('Geography')
order by [State Province Name]
start at "Illinois"
4. FILTER function
With FILTER function particular column values with distinctive rows can be isolated as in T
With FILTER function, particular column values with distinctive rows can be isolated as in T-
SQL WHERE statement:
evaluate(filter('Geography', [State Province Name] = "North Carolina"))
evaluate(filter('Product', [Product Id] >= 82))
Notice that the value 82 doesn’t have any surrounding symbols, and “North Carolina” has
quotation marks – that is because of the data type of the columns (Int and Text).
5. ADDCOLUMN function
This is function adds a custom column Daily Profit, and it is dependent on the FILTER
function:
evaluate
(filter(addcolumns(
'Product',
"Daily Profit", 'Product'[List Price] - 'Product'[Standard Cost]),
'Product'[List Price] > 1))
order by 'Product'[Product Subcategory Id]
As can be seen, column Daily Profit is the product of calculation (subtraction) between two
previously specified columns.
6. SUMMARIZE function
This function acts as an aggregated and simplified substitute of T-SQL GROUP BY or JOIN
statements in T-SQL. In one case, the desired columns are retrieved and shown as a result of
the query, without any mathematical operation:
evaluate
summarize('Geography', 'Geography'[City], 'Geography'[Postal Code])
The syntax for the SUMMARIZE function is to define the primary table (Product), then define
the columns which will be included in the result.
evaluate
summarize('Geography', 'Geography'[City], 'Geography'[Postal Code])
order by [Postal Code]
In this example we will include summary calculations of Total Days of Manufacture August
2016:
evaluate
summarize('Product', "Total Days of Manufacture August 2016",
sum('Product'[Days To Manufacture]))
This result was created by defining the primary table, and then naming the measure (the title
must be within quotation marks) and performing the calculation with a SUM function on the
particular column with the table specified.
7. ROW function
These statements can be very interesting to show a combination of particular functions while
querying a SSAS Tabular model database.
The ROW function returns a single, custom titled column and one row with the specific value.
Execute these specific (and interesting) expressions:
evaluate(ROW("Current date and time", Now()))
evaluate(ROW("The PI Number", PI()))
evaluate(ROW("Degrees to Radians", DEGREES(90)))
evaluate(ROW("Average Price", AVERAGE('Product'[List Price])))
Quick tip: Try other aggregate functions like MAX, MIN or COUNT instead of AVERAGE, and
don’t forget to rename the column properly
Retrieve the position of specified letter in particular word with SEARCH function:
evaluate(ROW("Position of letter B", SEARCH("b", "Tabular")))
We can even perform some basic mathematical operations within the query, and
also add some more complex functions:
evaluate(ROW("Calculator", 25*6-6))
I hope you have enjoyed these examples and querying your SSAS Tabular model database!
Useful links
Data Analysis Expressions (DAX) Overview
DAX Syntax Reference
Tabular Models (SSAS)
See more
For a collection of SSAS tools including SSAS system performance monitoring and SSAS doc‐
umentation, see ApexSQL SSAS tools
Daniel Tikvicki
Daniel is a librarian who ran into a vortex of IT world, where he is
levitating and learning constantly. He likes books, merely all forms of
artistic expression (being a musician/poet himself), and he is under‐
ground publisher (fanzines and experimental music). Also, the points of
interest include theology, mythology and pseudoscience.
Related Posts:
1. How to query a SSAS Tabular model database with MDX expressions
2. Time Intelligence in Analysis Services (SSAS) Tabular Models
3. Creating your first SSAS tabular model database
4. How to create a simple SSRS Report using a SSAS Tabular model database
5. How to automate SSAS tabular model processing in SQL Server 2016
In this article, we will cover This article will show the This article will describe how Thi
the most common questions Copy Data Tool to import to work with Python in bui
about this nice SQL … data into Azure SQL … Power BI. Lea
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
This was super helpful for me. Thanks for putting it up.
Thanks.
△ ▽ • Reply • Share ›
© 2022 Quest Software Inc. ALL RIGHTS RESERVED. | GDPR | Terms of Use | Privacy