Creating Measures

Measures are essential building blocks in Business Intelligence. They represent calculations performed on your data, allowing users to derive meaningful insights. In Analysis Services, measures are typically written using the Multidimensional Expressions (MDX) language.

Understanding Measures

Measures are dynamic calculations that respond to user interactions. For example, a user might slice and dice sales data by different dimensions (like Time, Product, or Geography). A measure for "Total Sales" would automatically recalculate based on the selected slices.

Key Concepts:

Creating Your First Measure

Let's walk through creating a simple measure for "Sales Amount."

Steps:

  1. Open SQL Server Data Tools (SSDT): Load your Analysis Services project.
  2. Navigate to the Measures Folder: In Solution Explorer, expand your project and then expand the Cubes folder. Right-click on the cube where you want to add the measure and select New Measure.
  3. Configure the Measure:
    • Name: Enter a descriptive name, e.g., [Sales Amount].
    • Source: Select the table containing the sales data.
    • Aggregation Function: Choose the appropriate function, such as Sum.
    • Source Column: Select the column that holds the sales values (e.g., [SalesAmount] from your Sales table).
    • Format String: Optionally, specify a format string for how the measure should be displayed (e.g., #,##0.00 for currency).
  4. Deploy the Cube: After creating the measure, deploy your Analysis Services project to make it available for querying.

Tip: Always aim for clear and concise measure names. Users should be able to understand the measure's purpose just by its name.

Advanced Measure Creation with MDX

For more complex calculations, you'll write MDX expressions. Here are a couple of common scenarios:

Example 1: Year-over-Year Growth

This measure calculates the percentage difference in sales compared to the previous year.


CREATE MEMBER CURRENTCUBE.[Measures].[Sales YoY Growth] AS
    ( [Measures].[Sales Amount] - ([Measures].[Sales Amount], PREVIOUSDAY([Date].[Calendar].CurrentMember)) )
    / ([Measures].[Sales Amount], PREVIOUSDAY([Date].[Calendar].CurrentMember)),
FORMAT_STRING = "0.00"
            

Important: Ensure your cube has a proper date dimension and that relationships are correctly defined for time-based calculations like YoY growth to work accurately.

Example 2: Profit Margin

This measure calculates the profit margin as a percentage of sales.


CREATE MEMBER CURRENTCUBE.[Measures].[Profit Margin] AS
    IIF(
        NOT ISERROR([Measures].[Sales Amount]),
        [Measures].[Profit Amount] / [Measures].[Sales Amount],
        NULL
    ),
FORMAT_STRING = "0.00%"
            

Best Practices for Measures