Creating Measures
Measures are the quantitative values that users analyze in a cube. They represent business metrics such as sales amount, quantity, profit, or employee count. Measures are typically stored in the fact tables of your data warehouse.
Understanding Measure Types
Measures can be classified into two primary types:
- Simple Measures: These directly correspond to a column in a fact table. For example, a
SalesAmountcolumn in a fact table can be directly represented as a simple measure. - Calculated Measures: These are measures computed based on existing measures or other calculations using MDX (Multidimensional Expressions). They allow for more complex analysis, such as year-over-year growth, percentages, or ratios.
Creating Simple Measures
In SQL Server Data Tools (SSDT) for Analysis Services, you can create simple measures through the Cube Designer.
- Open your Analysis Services project in SSDT.
- Navigate to the Cube Designer for the desired cube.
- In the Measures pane, click New Measure.
- Source Table: Select the table containing the measure data (usually a fact table).
- Source Column: Select the column from the fact table that holds the numeric data for your measure.
- Name: Provide a descriptive name for the measure (e.g., "Sales Amount").
- Aggregation Function: Choose an appropriate aggregation function (e.g.,
Sum,Count,Average,Min,Max). For most transactional data,Sumis commonly used for measures like sales or quantity. - Click OK to create the measure.
Creating Calculated Measures
Calculated measures are defined using MDX expressions.
- In the Cube Designer, click New Calculated Measure in the Measures pane.
- Name: Provide a descriptive name for the calculated measure (e.g., "Year-over-Year Sales Growth").
- Expression: Enter the MDX formula. For example, to calculate the percentage of sales for a specific product relative to total sales:
-- Assuming [Measures].[Sales Amount] is your base measure -- and [Product].[Product Category].[Category].CurrentMember is the current member in the Product dimension DIVIDE([Measures].[Sales Amount], SUM([Measures].[Sales Amount] * [Product].[Product Category].[Category].CHILDREN))For a year-over-year growth calculation (assuming you have a Time dimension with Year level):
DIVIDE( ([Measures].[Sales Amount] - ([Measures].[Sales Amount], [Date].[Calendar].[Calendar Year].PrevMember)), ([Measures].[Sales Amount], [Date].[Calendar].[Calendar Year].PrevMember) ) - Format String: Optionally, specify a format string to control how the measure is displayed (e.g.,
"Percent","$#,##0.00"). - Click OK.
Pro Tip: Use the MDX Script view in the Cube Designer to manage and organize your calculated measures and to define scope assignments for performance optimization.
Measure Properties
Measures have several properties that affect their behavior and presentation:
- Name: The display name of the measure.
- Source Column: For simple measures, the source column in the fact table.
- Aggregation Function: How the measure values are aggregated (e.g., Sum, Average).
- Format String: Controls the number formatting.
- Description: A textual description of the measure.
- Kpi: Link to a Key Performance Indicator if applicable.
Important: Ensure that the data types of your source columns are appropriate for the aggregation function you choose. Numeric data types are required for most aggregation functions.
Best Practices
- Granularity: Measures should be at the lowest level of granularity defined by the associated fact table.
- Naming Conventions: Use clear and consistent naming conventions for measures.
- Calculated Measures: Use them sparingly and ensure their logic is well-documented and tested. Complex calculations can impact performance.
- Aggregation Designs: Properly configure aggregation designs to improve query performance for frequently accessed measures.