Microsoft Docs

Define Relationships in Multidimensional Modeling

In SQL Server Analysis Services (SSAS) multidimensional modeling, relationships are fundamental for connecting dimension tables to fact tables and for establishing hierarchies within dimensions. Understanding and correctly defining these relationships is crucial for accurate data aggregation, slicing, and dicing in your OLAP cubes.

Table of Contents

Introduction to Relationships

Relationships in SSAS multidimensional models define how data from different tables in your data source view are associated. These associations are used by the Analysis Services engine to join data from fact and dimension tables when processing queries against your cubes.

There are two primary types of tables involved in relationships:

The most common relationship is between a dimension table and a fact table, typically a foreign key in the fact table referencing a primary key in the dimension table.

Types of Relationships

Analysis Services supports several types of relationships, though the most prevalent are:

Analysis Services also handles proactive caching and data mining relationships, but for core multidimensional modeling, one-to-many and many-to-many are the primary focus.

Defining Relationships in Visual Studio

Relationships are typically defined within the Data Source View designer in SQL Server Data Tools (SSDT) or Visual Studio for Analysis Services projects.

Data Source View Designer Interface
Visual representation of defining relationships in the SSAS Data Source View Designer.

To define a relationship:

  1. Open your Analysis Services project in Visual Studio.
  2. Navigate to the Data Source Views folder and open the relevant data source view.
  3. In the designer, you will see the tables from your data source.
  4. For One-to-Many: Drag the primary key column from the dimension table and drop it onto the corresponding foreign key column in the fact table.
  5. For Many-to-Many: You will define two one-to-many relationships: one from each "outer" table to the bridge table.

When you create a relationship, Analysis Services will automatically attempt to infer the cardinality (e.g., one-to-many). You can verify and adjust this.

Key Relationship Properties

Once a relationship is created, you can inspect and modify its properties:

Note on Cardinality: While you can set cardinality manually, it's best practice to let Analysis Services infer it from the underlying data whenever possible. Incorrectly set cardinality can lead to performance issues or incorrect query results.

Common Relationship Scenarios

Fact to Dimension Relationships

This is the most standard relationship in a star schema. The fact table contains foreign keys that link to the primary keys of dimension tables.


-- Example in SQL Server Data Source View
-- Relationship between DimDate (PK: DateKey) and FactSales (FK: DateKey)
CREATE RELATIONSHIP "FactSales_DimDate"
FROM FactSales "DateKey"
TO DimDate "DateKey"
CARDINALITY "Many:One";
        

Many-to-Many Relationships

When a fact table needs to be associated with two or more dimensions where the relationship is not one-way, a bridge table is used. This is often seen with dimensions like "Product" and "Store" if sales can happen at a store, and products can be promoted by stores, but a product can be in many stores and a store can sell many products directly.

You would create two one-to-many relationships:

  1. From the bridge table to the first dimension.
  2. From the bridge table to the second dimension.

Analysis Services then understands this as a many-to-many relationship between the two outer tables.

Best Practices

Important: Relationships in the Data Source View are logical connections. The actual SQL joins are generated by Analysis Services when processing or querying the cube.

By carefully defining relationships in your Analysis Services multidimensional models, you lay the groundwork for a powerful and responsive analytical solution.