module 11_ introduction to dax.pdf

Upload: amigosprovideo

Post on 14-Oct-2015

103 views

Category:

Documents


0 download

TRANSCRIPT

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 1/47

    Module 11: Introduction to DAX

    Contents:

    Lesson 1: DAX Fundamentals

    Lesson 2: Using DAX to Create Calculated Columns and Measures in a Tabular Data Model

    Lab 11: Creating Calculated Columns and Measures by Using DAX

    Module Overview

    You can extend the Microsoft PowerPivot for Microsoft Excel and Microsoft SQL Server 2012 Analysis

    Services tabular data models that you create by using the Data Analysis Expressions (DAX) language. DAX

    is a highly flexible language that enables you to create measures and calculated columns that you can use

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 2/47

    in PivotTable tables and PivotChart charts.

    This module explains the fundamentals of the DAX language. It also explains how you can use DAX to

    create calculated columns and measures, and how you can use these in your tabular data models.

    After completing this module, you will be able to:

    Describe the fundamentals of DAX.

    Use DAX to create calculated columns and measures.

    Lesson 1 : DAX Fundamentals

    To use DAX effectively, it is necessary to understand the components of the language and its capabilities.

    This lesson explains the fundamentals of DAX and provides an overview of the functionality that it offers.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 3/47

    After completing this lesson, you will be able to:

    Describe DAX and how it is used.

    Describe the different types of DAX functions.

    Describe the syntax for writing DAX formulas.

    Describe aggregations.

    Explain why context is important when writing DAX formulas.

    Describe the basics of DAX queries.

    Overview of DAX

    Formula-Based Language

    The DAX language consists of a library of functions, operators, and constants that enables you to create

    formulas that define the calculated columns and measures to extend the functionality of tabular data

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 4/47

    models. DAX was introduced in the first version of the PowerPivot for Excel add-in as the language for

    writing the formulas that define business logic in PowerPivot for Excel workbooks. In SQL Server 2012, you

    can use DAX in tabular data models in SQL Server Analysis Services and also in the tabular data models

    that you create in PowerPivot for Excel workbooks. New extensions make it possible to use DAX as a query

    language, too.

    Similar to Excel Formulas

    DAX is based on the Microsoft Excel formula syntax and uses a range of functions, operators, and values.

    However, unlike formulas in Excel, DAX does not work on individual cells or ranges of cells. Instead, you use

    it to work with the relational columns and tables that you import into a tabular data model. DAX also

    includes more advanced functionality that Excel formulas provide. The similarity between DAX and Excel

    formulas minimizes the learning time for information workers who want to learn to use DAX, enabling faster

    adoption of the language and reducing the need for training.

    New Functions

    The latest version of DAX includes new functions that increase both the flexibility and usability of the

    language, making it possible to create measures and calculated columns that enable you to solve even

    more sophisticated business problems. These new functions include:

    USERELATIONSHIP. Enables you to specify which relationship to use when multiple table relationships

    exist.

    LOOKUPVALUE. Enables you to return values based on the values in a different column.

    PATH and PATHITEM. Enable you to implement parent-child relationships.

    SWITCH. Enables you to return different values depending upon the input expression.

    DISTINCTCOUNT. Returns a count of the cells that contain unique values.

    RANK.EQ and RANKX. Enable you to produce data rankings.

    CROSSJOIN and GENERATE. Enable you to perform table joins.

    TOPN. Enables you to display only the top n rows of a set of data, where n is a number that you choose.

    DAX Functions

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 5/47

    DAX provides a range of functions that you can use to build formulas. Although many of these functions are

    the same as or similar to the functions that you use to build formulas in Excel, DAX includes many additional

    functions that make it possible to create much more complex and sophisticated reports than you can by

    using only Excel functions. For example, DAX includes functions that can navigate table relationships, take

    account of context to calculate values in PivotTable tables, or compare time periods to produce the desired

    result. A DAX formula can contain multiple functions, and you can nest functions within other functions with

    up to 64 levels of nesting.

    Note You should avoid having too many levels of nesting in your formulas because it makes them

    difficult to write and troubleshoot.

    DAX includes several categories of functions:

    Text. These functions include CONCATENATE, TRIM, and BLANK. They are based on the Excel string

    functions, and enable you to manipulate strings.

    Information. These functions include PATH, ISBLANK, and ISERROR. They enable you to test row values to

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 6/47

    see whether they match a type of value such as an error or a null, and to handle relationships in a table.

    This category also includes functions such as USERNAME, which you can use to implement row-level

    security in tabular data models in SQL Server Analysis Services.

    Filter and value. These functions include CALCULATE, ALL, FILTER, and RELATED. They enable you to

    manipulate data in a variety of ways, such as modifying the context for calculations, or using the

    relationships between tables to return specific results. There is no direct equivalent of these functions in

    Excel.

    Logical. These functions include TRUE, IF, SWITCH, and NOT. They enable you to perform logical tests on

    expressions to return information about the state of the tested values, and to use conditional logic.

    Mathematical and trigonometric. These functions include ROUND, FLOOR, and POWER. They are similar to

    the mathematical and trigonometric functions in Excel.

    Statistical and aggregation. These functions include SUM, COUNT, RANKX, and DISTINCTCOUNT. They are

    similar to aggregation functions in Excel.

    Date and time. These functions include DATE, MONTH, and YEAR. They are equivalent to date and time

    functions in Excel.

    Time intelligence. These functions include DATEADD, DATESBETWEEN, and FIRSTDATE. They enable you to

    work with columns that use the date data type in a more sophisticated way, for example, by calculating

    values for one time period with reference to another time period.

    DAX Syntax and Data Types

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 7/47

    DAX is syntactically similar to the Excel formula language, so users who are already skilled in this area

    should quickly be able to start creating DAX formulas. The following list summarizes basic DAX syntax and

    conventions:

    All DAX formulas must start with an equal sign (=), followed by an expression.

    Expressions can contain functions, operators, constants, and references to columns in tables. Object

    names that are referenced in expressions, such as table and column names, are case insensitive and can

    include spaces. You cannot use leading or trailing spaces, characters that are invalid in the names of SQL

    Server Analysis Services objects, or control characters in table, column, or measure names. Arithmetic,

    comparison, text concatenation, and logical operators are largely the same as for formulas in Excel, and

    they mostly work in the same way.

    DAX functions use tables and columns as inputs. You should use fully qualified names when you reference

    columns to avoid ambiguity. A fully qualified name uniquely identifies a column because it includes the

    name of the table to which the column belongs. Use brackets around the column names and single

    straight quotation marks around table names, for example, 'Date'[Calendar Year]. You can use

    unqualified names to refer to columns that are in the same table. For example, the fully qualified name

    'Date'[Calendar Year] can also be written as [Calendar Year].

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 8/47

    When you refer to a measure, for example, when you are writing a measure that references another

    measure, you should enclose the measure name in brackets, for example, [Total Profit].

    Note PowerPivot for Excel and SQL Server Data Tools support AutoComplete. You should use

    AutoComplete to ensure accuracy and compliance with syntactical requirements.

    Data Types

    DAX uses the following data types:

    I8 (eight-byte integer).

    R8 (eight-byte real number).

    Boolean.

    String.

    Date.

    CY (Currency).

    BLANK. Represents a blank or missing value or an empty cell in an Excel worksheet. You can test for blank

    values by using the ISBLANK function.

    Table. Used by functions that either require a table as input or return a table.

    DAX will implicitly convert data types where possible, so there is no need to explicitly convert a column to a

    different data type. For example, if you enter a date as a string and use that value in a date function, DAX

    will attempt to convert the data type to Date. There are no DAX functions for explicitly converting data

    types.

    Note BLANK and NULL are not the same thing. Handling of BLANK data types varies depending

    upon the operation that you perform. For example, BLANK + 5 = 5, but BLANK * 5 = BLANK.However, in SQL Server, both NULL + 5 and NULL * 5 result in a NULL value. SQL Server NULLvalues are implicitly converted to BLANK values when you import data into a tabular data model.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotatio 9/47

    For More Information For more information about Data Types Supported in PowerPivot Workbooks,see http://go.microsoft.com/fwlink/?LinkID=246791.

    Aggregations

    Information workers are typically interested in finding answers to questions such as "How much revenue

    did the company generate though its Internet sales channel in April?" or "What was the best-selling

    product in France last year?" Answering these kinds of questions requires you to group and aggregate

    data in some way. You group data by using values that give meaning and context to the data, such as

    Product Category or Sales Region. These values are usually stored in the tables that you import from your

    data sources. You can aggregate data by using DAX aggregation functions. The list below contains

    commonly used aggregation functions:

    SUM. Adds up all of the values in a numerical column, and provides a single total for that column. For

    example, the following code example adds up all values in the Sales Amount column in the Reseller

    Sales table.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 10/47

    =SUM('Reseller Sales'[Sales Amount])

    SUMX. Filters and then adds up all of the values in a numerical column, and returns a single total for that

    filtered column. For example, the following code example returns the sum total of the Freight column in

    the Internet Sales table, but the calculation only includes rows for which the SalesTerritoryKey value is

    5.

    =SUMX(FILTER('Internet Sales', 'Internet Sales'[SalesTerritoryKey]=5),[Freight])

    COUNT. Returns a count of the number of rows that are not blank for columns that contain either numbers

    or dates. COUNTA is similar, but it returns a count of the number of rows that are not blank for columns

    that contain nonnumeric values.

    For example, the following code example returns a count of the rows in the Order Date column in the

    Reseller Sales table.

    =COUNT('Reseller Sales'[Order Date])

    COUNTAX. Filters and then counts the number of values that are not blank in a column and returns a

    single count for that filtered column. For example, the following code example returns the number of rows

    in the Phone column in the Reseller table, where the value in the Status column is set to "Active".

    =COUNTAX(FILTER('Reseller',[Status]="Active"),[Phone])

    The COUNTX function works in the same way as the COUNTAX function, but it only counts numerical values.

    The COUNTBLANK function returns the number of blank values in a column, and the COUNTROWS function

    counts the number of rows in a table.

    MIN and MAX. Return the minimum and maximum values in a numerical column. The following code

    example returns the maximum value for the Sales Amount column in the Reseller Sales table.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 11/47

    =MAX('Reseller Sales'[Sales Amount])

    The MAXA and MINA functions return the maximum and minimum numerical values in a column, but they also

    handle nonnumeric logical values such as TRUE and FALSE, which evaluate to 1 and 0 respectively.

    MAXX and MINX. Return the maximum and minimum values in a column in a table that is filtered by using

    an expression. For example, the following code example returns the minimum value for the Freight

    column in the Internet Sales table for which the corresponding SalesTerritoryKey value is 5.

    =MINX(FILTER('Internet Sales', [SalesTerritoryKey] = 5),[Freight])

    AVERAGE. Returns the mean of all values in a numerical column. For example, the following code example

    returns the average value in the Total Product Cost column in the Reseller Sales table.

    =AVERAGE('Reseller Sales'[Total Product Cost])

    The AVERAGEA function does the same thing as AVERAGE, but it also handles nonnumeric data types.

    AVERAGEX. Enables you to supply an expression to obtain the average of values that are not neatly

    contained in a single column. For example, the following code example sums the Freight and TaxAmt

    values for each row in the Internet Sales table and then calculates the average value.

    =AVERAGEX('Internet Sales', 'Internet Sales'[Freight]+ 'Internet Sales'[TaxAmt])

    Using Aggregations

    When you add a column to the Values area in the PowerPivot Field List in a PowerPivot for Excel

    worksheet, the values in the column are automatically summed and the total added to the PivotTable table

    or PivotChart chart that you are using. If you need to summarize the data differently, you can select a

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 12/47

    different type of aggregation, such as COUNT, MIN, or AVERAGE, from a list; the DAX formula is automatically

    created for you. When you need to create aggregations that are more complex than this, you can create a

    measure.

    Context

    When you create a DAX measure or calculated column, you define a field that you can use in a PowerPivot

    PivotTable or a PivotChart. However, the specific values that appear in any given PivotTable or PivotChart

    will depend on the context, such as the columns and rows that you add to a PivotTable or a PivotChart, or

    the slicers that are enabled at the time. For example, if you create a PivotTable and add only the Sum of

    Sales Amount measure to the Values area and nothing to the Row Labels area, you will see a single value

    that represents the total sales amount for all categories, years, and countries for the Reseller Sales

    channel. If you then add Calendar Year to the Row Labels area, you will see multiple values, each one a

    total for a given year. Even though the same measure is used in both cases, the value that displays

    changes to reflect the context. This context-aware behavior makes it possible to perform dynamic analysis

    of data quickly and easily, without the need to reconfigure PivotTables or PivotCharts whenever you add a

    new measure.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 13/47

    It is important to be aware of context when you create measures and calculated columns. If you do not

    take context into account, you might find that your reports do not aggregate data in the way that you

    intended, and are inaccurate and misleading.

    Types of Context

    There are three types of context to consider:

    1. Row context. When you create a calculated column in a table, you specify the column or columns that it

    should reference. For example, the formula in the code example below references the First Name and

    Last Name columns in the Employee table.

    =CONCATENATE([First Name], CONCATENATE(" ", [Last Name]))

    The calculated column evaluates the expression for each row. It uses the row context to obtain the

    values in the First Name and Last Name columns for each row, and then uses these values to create a

    new value for each row by concatenating them. Row context is important when you are using any

    function that evaluates an expression for each row in a table, such as SUMX.

    2. Query context. When you add a measure to a PivotTable table, the PowerPivot engine evaluates the

    measure value for each cell. The evaluation takes account of the query context to calculate these

    values for a given cell. Query context is determined by the row labels, column labels, filters, and slicers

    that apply to the PivotTable table. Removing or adding a new slicer, row label, or column label will

    change the filter context and the PowerPivot engine must now take account of the new context that is

    performing the calculation for each row.

    3. Filter context. You can use DAX functions to modify the row context and query context by creating filters

    in formulas. The FILTER function enables you to produce a subset of rows on which you can then

    perform a calculation. For example, you might filter by specifying a product category and then

    aggregate the sales amount for that category only. You can override filters by using the ALL and

    ALLEXCEPT functions. ALL forces all filters to be ignored, and ALLEXCEPT enables you to selectively

    override filters by specifying particular columns to retain in the filter context; all other columns are

    ignored.

    DAX Queries

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 14/47

    The latest version of DAX includes extensions that enable you to write queries that retrieve data. Client

    reporting tools, such as Power View, use DAX queries to query tabular data models in SQL Server Analysis

    Services. Although it is not likely that you will need to write DAX queries manually, it can be useful to have a

    basic understanding of them to help you with troubleshooting.

    You can retrieve data from tables by using the EVALUATE keyword, which is functionally equivalent to the

    SELECT statement in SQL Server. The following code example returns all of the rows from the Reseller

    Sales table.

    EVALUATE('Reseller Sales')

    Queries can include FILTER to return a reduced result set, in much the same way as a WHERE clause

    returns a reduced result set in SQL Server. The following code example returns all of the rows from the

    Reseller Sales table where the OrderDateKey value is greater than 20040101.

    EVALUATE(FILTER('Reseller Sales', 'Reseller Sales' [OrderDateKey]>20040101))

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 15/47

    You can also order result sets. The following code example returns all of the rows from the Reseller Sales

    table where the OrderDateKey value is greater than 20040101. The results are ordered by Sales Amount.

    EVALUATE(FILTER('Reseller Sales', 'Reseller Sales'[OrderDateKey]>20040101))

    ORDER BY 'Reseller Sales'[Sales Amount] ASC

    For More Information For more information about DAX Table Queries, see

    http://go.microsoft.com/fwlink/?LinkID=246792.

    Lesson 2: Using DAX to Create Calculated Columns and Measures in a Tabular

    Data Model

    You use DAX to create calculated columns and measures, which you can then use in PivotTable tables and

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 16/47

    PivotChart charts in PowerPivot for Excel workbooks. This lesson shows you how to create DAX calculated

    columns and measures by using a variety of functions, including time intelligence functions and functions

    that manipulate table relationships. This lesson also shows you how to create dynamic measures that can

    use conditional logic to perform different calculations based on different inputs.

    After completing this lesson, you will be able to:

    Explain how to use calculated columns.

    Explain how to use measures.

    Describe how PowerPivot handles multiple relationships between tables.

    Describe time intelligence.

    Describe dynamic measures.

    Calculated Columns

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 17/47

    After you import data into a tabular data model, you can enhance the model by adding calculated columns

    to your tables. A calculated column is a named column that you populate by using a DAX formula. The

    formula will usually refer to other columns, either in the same table or in a different, related table, but you

    can also create calculated columns that are based on measures, or on other calculated columns. The

    following code example creates a calculated column in the Employee table. It obtains the values from the

    First Name and Last Name columns in the Employee table and concatenates them, putting a space

    between the first and last names.

    =CONCATENATE('Employee'[First Name], CONCATENATE(" ", 'Employee'[Last Name]))

    The following code example uses the RELATED function to retrieve columns from the Sales Territory table.

    It concatenates the columns to create a calculated column in the Customer table.

    =RELATED('Sales Territory'[Sales Territory Country]) & ", " & RELATED('Sales

    Territory'[Sales Territory Region])

    Creating Calculated Columns

    You can create calculated columns in a tabular data model in the same way as you can in a workbook in

    Excel. In the Data View window of a PowerPivot for Excel workbook, select the table that you want to add

    the column to, and then click Add Column. You can then provide a name for the column and type the DAX

    formula in the formula bar, which will determine the contents of the column. The PowerPivot engine

    performs the calculation immediately to populate the column. By default, the engine recalculates columns

    automatically if the source data changes.

    Note The formula that you use to create a calculated column applies to every row in the column.You cannot apply the formula to only selected ranges of data as you can in Excel worksheets.

    When creating calculated columns, be aware of the following factors:

    Calculated columns calculate a value for every row in a table, so it can sometimes take a long time to

    calculate these values when tables contain a large number of rows.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 18/47

    By default, calculated columns update when the underlying data changes. This can cause performance

    issues if the underlying data changes frequently, so you can change the PowerPivot workbook to use

    Manual Calculation mode if required.

    Changing the names of columns that are referenced by a calculated column will cause an error in the

    calculated column.

    Using Calculated Columns

    You can use the calculated columns that you create in PowerPivot for Excel workbooks by adding them to

    the Row Labels, Column Labels, Legend Fields, and Axis Fields areas in the PowerPivot Field List.

    Measures

    A measure is a named formula that can encapsulate complex business logic. You can add the measures that

    you create to PivotTable tables and PivotChart charts in PowerPivot for Excel workbooks to perform data

    analysis.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 19/47

    There are two kinds of measure that you can use in a tabular data model:

    Implicit measures. Implicit measures are created automatically in the model by aggregating columns by

    using the SUM, COUNT, MIN, MAX, AVG, or DISTINCTCOUNT aggregations. The name of an implicit measure

    derives from the column name, with a prefix such as Sum of or Average of, depending on the

    aggregation that is used. For example, an implicit measure that is based on the Sales Amount column

    might be called Sum of Sales Amount. Implicit measures appear in the PowerPivot Field List with the

    other columns and measures in a table. You cannot use implicit measures with Power View reports.

    Explicit measures. Explicit measures are the measures that you explicitly create in a tabular data model.

    You can create explicit measures that use a range of DAX functions to aggregate data in creative and

    sophisticated ways. You can use explicit measures as the basis of key performance indicators (KPIs), and

    you can also use them in Power View reports.

    Creating Measures

    You can create explicit measures in two ways:

    1. In a PowerPivot for Excel workbook, in the PowerPivot Field List, use the Add New Measure option.

    2. In a tabular data model in SQL Server Analysis Services or PowerPivot for Excel, use the Measure Grid.

    When you create a measure, you can specify the format that a measure should use when you display it in a

    PivotTable table or PivotChart chart. Formatting options include numerical, percentage, and currency. You

    also associate a new measure with a table; when users view the tables in the PowerPivot Field List, they

    see the measures that they contain alongside the columns, and they can select them in the same way as

    they select columns.

    Using Measures

    You can add measures to the Values, Legend Fields, and Axis Fields areas of the PowerPivot Field List.

    You can also group data by adding measure values to the Row Labels and Column Labels areas. For

    example, you can group the Sales Amount and Total Profit measures inside Calendar Year so that each

    Calendar Year value contains a value for both measures.

    The exact value that an individual cell displays for a measure depends on the context. Row context, query

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 20/47

    context, and filter context can all have a bearing on this.

    Demonstration: Creating and Using Measures and Calculated Columns

    Task 1: Create a calculated column

    1. Ensure that the MIA-DC1 and MIA-SQLBI virtual machines are both running, and then log on to MIA-

    SQLBI as ADVENTUREWORKS\Student with the password Pa$$w0rd. Then in the

    D:\10778A\Demofiles\Mod11 folder, run Setup.cmd as Administrator.

    2. In the D:\10778A\Demofiles\Mod11 folder, double-click the Demo.xlsx file to open it in Excel.

    3. In the Demo workbook, on the ribbon, on the PowerPivot tab, click PowerPivot Window. The

    PowerPivot window may open behind the workbook, so minimize the workbook so you can see it.

    4. In the PowerPivot window, click the Geography tab.

    5. In the Geography table, scroll across to the last column, double-click Add Column, type Full Location

    Name, and then press Enter.

    6. In the formula bar above the Geography table, type the DAX formula in the following code example,

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 21/47

    and then press Enter.

    =CONCATENATE([State or Province Name], CONCATENATE(", ",[Region Name]))

    7. Right-click the State or Province Name column header, and then click Hide from Client Tools.

    Task 2: Create a measure

    1. In the PowerPivot window, in the upper-left corner, click the Switch to Workbook button.

    2. Click the Add Measure Demo tab, and then click PivotTable1.

    3. In the PowerPivot Field list, click Refresh if notified that PowerPivot data was modified.

    4. In the PowerPivot Field list, right-click Reseller Sales, and then click Add New Measure.

    5. In the Measure Settings dialog box, in the Measure Name (all PivotTables) field, type Total Profit.

    6. In the Formula field, type the DAX formula in the following code sample, and then click Check Formula:

    =SUM('Reseller Sales'[Sales Amount]) - SUM('Reseller Sales'[Total Product Cost])

    7. In the Category field, click Currency, and then click OK.

    Task 3: Use measures in a PivotTable table and a PivotChart chart

    1. When you create the Total Profit measure, it should automatically appear in the PivotTable1 table. If

    it does not, in the PowerPivot Field list, expand Reseller Sales, and then select Total Profit.

    2. In Reseller Sales, select Sales Amount.

    3. Expand Product, drag Category to the Row Labels area, and then release it.

    Task 4: Use measures to create different groupings of data

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 22/47

    1. Expand Date, drag Calendar Year to the Column Labels area, and then release it below Values. (This

    groups the years inside each of the two measures.)

    2. In the Column Labels area, click Calendar Year, and then click Move Up. (This groups the two

    measures inside each of the years.)

    3. In the Column Labels area, drag Values to the Row Labels area. (This nests the two measures inside

    each category.)

    4. Leave the Demo workbook open for the next Demonstration.

    Multiple Relationships

    The new version of PowerPivot for Excel supports multiple relationships between tables. This is useful if, for

    example, Date and Sales tables have multiple relationships to support different dates such as order date,

    due date, and delivery date. Multiple relationships appear in the diagram view of the PowerPivot window,

    with the active relationship shown as an unbroken line. The nonactive relationships appear as dotted lines.

    Only one relationship can be the active relationship and this relationship will automatically be used when

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 23/47

    performing calculations that use data from the two tables. You can use the USERELATIONSHIP function

    when you are writing DAX formulas to override this behavior and force PowerPivot to use the relationship

    that you specify. For example, if the default relationship between your fact table and your date table is

    based on the Order Date column, but you need to slice data by shipping date, you can write a DAX

    measure that contains the USERELATIONSHIP function to achieve this.

    Note USERELATIONSHIP does not create a relationship. The relationship must already exist for youto use it.

    For example, the following code example calculates the sum of the Sales Amount column based on shipping

    date.

    =CALCULATE(SUM(Reseller Sales[Sales Amount]), USERELATIONSHIP(Reseller

    Sales[ShipDateKey], Date[DateKey]))

    Note You can only use the USERELATIONSHIP function with certain other functions, such asCALCULATE, which can accept a filter as an argument.

    For More Information For more information about the USERELATIONSHIP function, seehttp://go.microsoft.com/fwlink/?LinkID=246793.

    Demonstration: Using a Specific Relationship in a Measure

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 24/47

    Task 1: Specify the relationship to use in a formula

    1. In the Demo workbook, click the PowerPivot tab, and then on the PowerPivot ribbon, click PowerPivot

    Window.

    2. On the ribbon, in the View group, click the Diagram View button.

    3. Use the mouse to point to each of the three lines between the Reseller Sales table and the Date

    table, one at a time. The lines represent the relationships between the tables.

    4. In the PowerPivot window, in the upper-left corner, click the Switch to Workbook button, click the

    Multiple Relationships Demo tab, and then click anywhere in PivotTable1.

    5. In the PowerPivot Field list, click Refresh if notified that PowerPivot data was modified.

    6. In the PowerPivot Field list, expand Reseller Sales.

    7. Right-click Sales With Ship Date, and then click Edit Formula.

    8. In the Measure Settings dialog box, review the formula, and then click Cancel.

    9. Drag Sales With Ship Date to the Values area in the PowerPivot Field List, and then release it.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 25/47

    10. In the PowerPivot Field List, expand Date, drag Calendar Year to the Row Labels area, and then

    release it.

    11. Close Demo.xlsx and do not save changes.

    Time Intelligence

    Time intelligence functions enable you to compare data from one time period against equivalent data from a

    different time period. For example, you might want to compare sales for the first quarter of this year

    against sales for the first quarter of last year.

    Requirements for Using Time Intelligence

    To support time intelligence functionality, you should ensure that your data and tables meet the following

    criteria:

    The model should contain a separate table that contains only date information.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 26/47

    The date table should have a continuous range of dates without any gaps.

    The column in the date table that uses the date data type should use day as the lowest level of

    granularity.

    Unlike in the previous version of PowerPivot for Excel, the relationships between your fact table and your

    date table do not need to be based on columns that use the date data type to support time intelligence

    functionality. Instead, you can base these relationships on columns that contain integer values. This is

    useful because it is likely that the databases that you use as sources for PowerPivot analysis will contain

    fact and date tables that are related by columns that use the integer data type, particularly if the source

    database is a data warehouse. For example, in the AdventureWorksDW database, the FactResellerSales

    table relates to the DimDate table on the DateKey column.

    To enable the PowerPivot engine to handle time intelligence correctly when integers are used in

    relationships, you must manually mark the date table as Date Table and specify the column in the table

    that contains the date values. This column must use the date data type.

    Time Intelligence Functions

    DAX includes an extensive set of functions to support time intelligence analysis, including DATEADD,

    DATESBETWEEN, and DATESYTD. You can use these functions in measures to perform complex, date-based

    comparisons. For example, the following code example calculates the sum of Sales Amount for the previous

    year.

    CALCULATE([Sum of Sales Amount], DATEADD(Date[FullDateAlternateKey], -1, YEAR))

    For More Information For more information about PowerPivot: DAX: Time Intelligence Functions, seehttp://go.microsoft.com/fwlink/?LinkID=246794.

    Dynamic Measures

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 27/47

    DAX includes functions that enable conditional logic, context examination, and other advanced ways of

    manipulating data. You can use these functions to create dynamic measures. A dynamic measure is a

    measure that responds flexibly to varying criteria; for example, a dynamic measure might use different

    calculations for a given cell depending on which of a series of possible values are defined in the rows and

    columns for that cell. Dynamic measures can be useful if, for example, you need to create a PivotTable table

    that contains row labels or column labels that are not contained in any of the tables in the tabular data

    model. For example, the following code example creates a measure that uses a different calculation for

    each of three different input values. Each of the input values, Current Year, Previous Year, and YOY

    Growth, is intended to be used as a row label in a PivotTable table.

    IF([Check Single Values)], SWITCH(VALUES(Time Period[Period]), Current Year, [Sum Of

    Sales Amount], Previous Year, [Previous Year], YOY Growth , IF(NOT(ISBLANK([Previous

    Year])), [Sum of Sales Amount] [Previous Year], BLANK())), BLANK())

    This code example references a linked table called Time Period, which contains the input values for the

    dynamic measure. This table has a single column called Period. This column contains just three rows, with

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 28/47

    the values Current Year, Previous Year, and YOY Growth. Using the SWITCH function forces the

    PowerPivot engine to evaluate each row in the Period column and return a different value for each one:

    For the Current Year row, it returns the value that the Sum of Sales Amount measure calculates.

    For the Previous Year row, it returns the value that the Previous Year measure calculates.

    For the YOY Growth row, it returns the value that is calculated by subtracting Previous Year from Sum

    of Sales Amount.

    The code example also references another measure called Check Single Values, which uses the

    HASONEVALUE function to check that for each cell in the PivotTable, only one input value exists for each

    row. This is necessary because the code example above can only evaluate single values.

    Note The code example above references other measures to perform specific tasks. It is bestpractice to create simpler measures that you can reuse in this way because it makes writingmeasures easier and simplifies troubleshooting.

    Lab Scenario

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 29/47

    As a senior business analyst at Adventure Works Cycles, you want to perform some detailed analysis of the

    data in tabular data model. Specifically, you want to:

    View the full name of each employee and aggregate sales data based on the reporting hierarchy of

    sales employees and managers

    View the sales contribution of each sales employee and manager as a percentage of the total sales

    achieved

    View year on year sales growth as a percentage

    View sales values for the current year and previous year, and show the year on year growth as a

    monetary value

    To accomplish this, you will need to use DAX expressions to create calculated columns in the data model,

    add custom measures that can be used as PivotTable fields, use time intelligence support in tabular data

    models to apply temporal calculations to date fields, and create dynamic measures that calculate values

    based on conditional logic.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 30/47

    Lab 11: Creating Calculated Columns and Measures by Using DAX

    Exercise 1: Creating Calculated Columns

    Scenario

    Your users are now using PowerPivot and tabular databases to browse data. However, some users have

    requested that you extend their tabular data models to enable them to explore sales by employee and

    related manager. To do this, you will use DAX to create calculated columns and a new hierarchy.

    The main tasks for this exercise are as follows:

    1. Prepare the lab environment.

    2. Create a calculated column by using concatenation.

    3. Create calculated columns to support parent-child relationships.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 31/47

    4. Create a hierarchy that uses calculated columns.

    5. Create a PivotTable table that contains the Employees hierarchy.

    Task 1: Prepare the lab environment

    Ensure that the MIA-DC1 and MIA-SQLBI virtual machines are both running, and then log on to MIA-SQLBI

    as ADVENTUREWORKS\Student with the password Pa$$w0rd.

    Run the Setup Windows Command Script file (Setup.cmd) in the D:\10778A\Labfiles\Lab11\Starter

    folder as Administrator.

    Task 2: Create a calculated column by using concatenation

    In the D:\10778A\Labfiles\Lab11\Starter folder, open the Sales Overview.xlsx file.

    Open the PowerPivot window, and in the Employee table, add a calculated column called Full Name that

    uses the CONCATENATE function to concatenate the First Name and Last Name columns with a space

    between them.

    You can find the complete formula for this calculated column in the Module_11_Formulas.txt file in the

    D:\10778A\Labfiles\Lab11\Starter folder.

    The new column should look like this.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 32/47

    Task 3: Create calculated columns to support parent-child relationships

    In the Employee table, add a new column named Path with the following DAX formula:

    =PATH([EmployeeKey], [ParentEmployeeKey])

    You can find the complete formulas for the calculated columns in this task in the Module_11_Formulas.txt

    file in the D:\10778A\Labfiles\Lab11\Starter folder.

    The Path calculated column should contain a set of employee keys for each row that defines the

    employees position in the managerial hierarchy.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 33/47

    In the Employee table, add a new column called Employee Level 1.

    In the formula bar above the Employee table, type the DAX formula in the following code sample, and

    then press Enter.

    =LOOKUPVALUE('Employee'[Full Name], 'Employee'[EmployeeKey], PATHITEM([Path], 1))

    The Employee Level 1 column should contain the full employee name that maps to the first employee key

    for each row in the Path column.

    Repeat the steps above to create the following columns

    Employee Level 2: The full name that maps to the second employee key for each row in the Path

    column.

    Employee Level 3: The full name that maps to the third employee key for each row in the Path column.

    Employee Level 4: The full name that maps to the fourth employee key for each row in the Path

    column.

    The new columns should resemble this.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 34/47

    Right-click each of the five calculated columns that you created in this task in turn, and click Hide from

    Client Tools.

    Task 4: Create a hierarchy that uses calculated columns

    View the diagram for the data model, and add a hierarchy named Employees to the Employee table. The

    hierarchy should include the Employee Level 1, Employee Level 2, Employee Level 3, and Employee

    Level 4 columns as shown in the following image.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 35/47

    The hierarchy will display employees in order of seniority, with the most senior employees from the

    Employee Level 1 column at the top.

    Task 5: Create a PivotTable table that contains the Employees hierarchy

    Switch to the Excel workbook and rename Sheet 2 to Employee Sales.

    Add a PivotTable table to cell B1 in the Employee Sales worksheet.

    Use the PivotTable to view the Sales Amount value from the Reseller Sales table by the Employees

    hierarchy from the Employee table, as shown here.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 36/47

    Right-click the PivotTable and click Show/Hide Fields to hide the Employee Level 1 and Employee Level

    2 fields so that you can focus on the department heads and their subordinates.

    Results: After this exercise, you should have a data model with a hierarchy based on DAX expressions.

    Exercise 2: Creating Measures

    Scenario

    Some users have requested that you extend their tabular data models to enable them to view the

    percentage that each employee contributes to overall sales. To do this, you will use DAX to create the

    required measures.

    The main tasks for this exercise are as follows:

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 37/47

    1. Create a measure that calculates total sales for all employees.

    2. Create a measure that calculates percentage of sales per employee.

    3. Add conditional formatting to the PivotTable table.

    Task 1: Create a measure that calculates total sales for all employees

    In the Employee Sales worksheet you used in the previous exercise, add a new measure to the Reseller

    Sales table in the PowerPivot Fields List pane. The new measure should be named Total Sales for All

    Employees and should be based on the following formula and formatted as currency.

    =CALCULATE([Sum of Sales Amount], ALL('Employee'))

    You can find the complete formula for this measure in the Module_11_Formulas.txt file in the

    D:\10778A\Labfiles\Lab11\Starter folder.

    The measure settings for the new measure should look like this.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 38/47

    The CALCULATE function calculates the Sum of Sales Amount measure, and the ALL function overrides

    the context of the Employee table, so that the measure delivers a single total regardless of context.

    Remove this field from the PivotTableyou will reference it in another measure.

    Task 2: Create a measure that calculates percentage of sales per employee

    In the Reseller Sales table, create another new measure named Percentage of Sales based on the

    following DAX formula and formatted as a percentage.

    =[Sum of Sales Amount]/[Total Sales for All Employees]

    You can find the complete formula for this measure in the Module_11_Formulas.txt file in the

    D:\10778A\Labfiles\Lab11\Starter folder.

    The measure settings for the new measure should look like this.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 39/47

    The formula uses the Sum of Sales Amount and Total Sales for All Employees measures to calculate the

    percentage of sales that each employee contributed, as shown here.

    Task 3: Add conditional formatting to the PivotTable table

    In the Employees Sales worksheet, in the PivotTable table, expand all employees.

    Select all of the rows in the Percentage of Sales column, but do not include the column header or the

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 40/47

    Grand Total row value.

    Click the Home tab. Add conditional formatting to the Percentage of Sales column. Use the Blue Data

    Bar formatting option as shown here.

    Save the Sales Overview.xlsx workbook when you have finished, and keep it open for the next exercise.

    Results: After this exercise, you should have created two measures.

    Exercise 3: Using Time Intelligence

    Scenario

    Some users have requested that you extend their tabular data models to enable them to view sales by

    comparing them with the previous year. To do this, you will use a DAX time intelligence function to create

    the required measures.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 41/47

    The main tasks for this exercise are as follows:

    1. Create a measure that uses a time intelligence function.

    2. Create a measure that uses conditional logic.

    Task 1: Create a measure that uses a time intelligence function

    In the Sales Overview.xlsx workbook, switch to the PowerPivot window, and if necessary, switch to Data

    View.

    View the Date table, and in the ribbon, on the Design tab, mark the Date table as Date Table, using the

    FullDateAlternateKey column as the unique identifier. This enables PowerPivot to apply temporal

    calculations to data in this table.

    Switch back to the workbook and insert a PowerPivot PivotTable in a new worksheet, and then rename

    the worksheet Year on Year Growth.

    Add Calendar Year from the Date table to the Row Labels area and Sales Amount from the Reseller

    Sales table to the Values area so that the PivotTable resembles the following image.

    Add a new measure to the Reseller Sales table. The new measure should be named Previous Year and

    should be based on the following formula and formatted as currency.

    =CALCULATE([Sum of Sales Amount], DATEADD('Date'[FullDateAlternateKey], -1, YEAR))

    You can find the complete formula for this measure in the Module_11_Formulas.txt file in the

    D:\10778A\Labfiles\Lab11\Starter folder.

    The Previous Year measure uses the CALCULATE function with the Sum of Sales Amount measure, and

    the DATEADD function with the Date[FullDateAlternateKey] column to calculate sales amount for one

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 42/47

    year earlier than the current year. Remove this field from the PivotTableyou will reference it in another

    measure.

    Task 2: Create a measure that uses conditional logic

    In the Reseller Sales table, create another new measure named Year on Year Growth based on the

    following DAX formula and formatted as a percentage.

    =IF([Previous Year], ([Sum of Sales Amount] [Previous Year])/[Previous Year],

    BLANK())

    You can find the complete formula for this measure in the Module_11_Formulas.txt file in the

    D:\10778A\Labfiles\Lab11\Starter folder.

    The Year on Year Growth formula uses the IF function to calculate year-on-year growth. The Previous

    Year measure tests whether a previous year value exists, and calculates year-on-year growth by using

    the Sum of Sales Amount and Previous Year measures. The measure returns a BLANK value if there is

    no previous year value (which will always be the case for the first year in the list), as shown here.

    Save the Sales Overview.xlsx workbook when you have finished, and keep it open for the next exercise.

    Results: After this exercise, you should have created a measure that uses a time intelligence function tocalculate sales for the previous period, and a measure that uses conditional logic to calculate the growth

    in sales from the previous period to the current period.

    Exercise 4: Creating a Dynamic Measure

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 43/47

    Scenario

    Some users have requested that you extend their tabular data models to enable them to view a PivotTable

    table that displays appropriate values for each of these rows: Current Year, Previous Year, and YOY

    Growth. To do this, you will use DAX to create a dynamic measure.

    The main tasks for this exercise are as follows:

    1. Create a linked table.

    2. Create a measure that checks whether inputs have a single value.

    3. Create a measure that dynamically calculates values.

    Task 1: Create a linked table

    In the Sales Overview.xlsx workbook, click the Time Period worksheet tab.

    Select cells A1 to A4, and on the PowerPivot tab, click Create Linked Table. Select the My table has

    headers option.

    In the PowerPivot window, rename the new table Time Period as shown here.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 44/47

    Task 2: Create a measure that checks whether inputs have a single value

    Switch back to the workbook and insert a PowerPivot PivotTable in a new worksheet, and then rename

    the worksheet Dynamic Time.

    Add the Period column from the Time Period table to the Row Labels area and Calendar Year from the

    Date table to the Column Labels area so that the PivotTable resembles the following image.

    In the Reseller Sales table, create another new measure named Check Single Values based on the

    following DAX formula as a TRUE/FALSE value.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 45/47

    =IF(HASONEVALUE('Time Period'[Period]) && HASONEVALUE('Date'[Calendar Year]), TRUE(),

    FALSE())

    You can find the complete formula for this measure in the Module_11_Formulas.txt file in the

    D:\10778A\Labfiles\Lab11\Starter folder.

    The Check Single Values measure uses the HASONEVALUE function to evaluate whether the Time

    Period[Period] and Date[Calendar Year] inputs each contain one value for a given cell in the

    PivotTable table. The IF function returns TRUE if they both return single values and FALSE if they do not.

    Remove this field from the PivotTableyou will reference it in another measure

    Task 3: Create a measure that dynamically calculates values

    In the Reseller Sales table, create another new measure named Sales Over Time based on the

    following DAX formula and formatted as currency.

    =IF([Check Single Values], SWITCH(VALUES('Time Period'[Period]), "Current Year", [Sum

    of Sales Amount], "Previous Year", [Previous Year], "YOY Growth",

    IF(NOT(ISBLANK([Previous Year])), [Sum of Sales Amount] - [Previous Year], BLANK())),

    BLANK())

    You can find the complete formula for this measure in the Module_11_Formulas.txt file in the

    D:\10778A\Labfiles\Lab11\Starter folder.

    The Sales Over Time measure uses the SWITCH function to provide different calculations for each of the

    values in the Period column (Current Year, Previous Year, and YOY Growth):

    For the Current Year value, it uses the Sum of Sales Amount measure.

    For the Previous Year value, it uses the Previous Year measure.

    For the YOY Growth value, it subtracts the Previous Year measure from the Sum of Sales Amount

    measure and uses the ISBLANK function to check for an empty Previous Year measure.

    The completed PivotTable should look like this.

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 46/47

    Save the Sales Overview.xlsx workbook and close Excel.

    Results: After this exercise, you should have created a dynamic measure

    Module Review and Takeaways

    Review Questions

    1. Do you or your colleagues use Excel formulas? What limitations have you encountered? What features

    of DAX might help you to overcome these limitations?

  • 29/5/2014 Module 11: Introduction to DAX

    https://skillpipe.courseware-marketplace.com/reader/es-ES/Book/BookPrintView/fa7b6122-f33c-4f39-861b-6c643000d5ec?ChapterNumber=13&FontSize=1&FontType=verdana&AnnotationFilterOwn=true&Annotati 47/47

    2. Do you have any table relationships for which the ability of DAX to support multiple relationships might

    be useful?

    3. Will time intelligence functionality be useful to you? What kinds of analyses might you want to perform?