官术网_书友最值得收藏!

Time for action – creating a hierarchy from multiple tables

Creating a relationship between tables in tabular and creating a hierarchy based on inter-related tables are two main parts that you will see in this example. In the first part of this example, we will create a relationship between the product table and the product category and product subcategory. In addition, in the second part, we will create a product category hierarchy based on the columns of these three tables:

  1. In the Model menu, click on the existing connections and open the current connection. Then, in the Import Tables wizard, choose DimProductCategory and DimProductSubcategory. Change their names to Product Category and Product Subcategory. Complete the wizard and import the records.
  2. Go to Diagram View; you will see two new tables added. There is a relationship between the product category and product subcategory. However, there is no relationship between the product subcategory and product. So we need to create a relationship.
  3. Drag-and-drop ProductSubcategoryKey from the Product table to ProductSubcategoryKey in the Product Subcategory table. A new relationship will be created as a one-to-many relationship. You can double-click on the relationship to change it if you need to.
  4. Go to Grid View of the Product table, add a new column at the end of table, and rename it as Product Subcategory. Write the following expression in the textbox near the fx icon:
    =RELATED(ProductSubcategory[EnglishProductSubcategoryName])
  5. Add another column and rename it as Product Category, then write the following expression for the new column:
    =RELATED(ProductCategory[EnglishProductCategoryName])
  6. Select the Product Category table and change its hidden property to true. Do the same for the Product Subcategory table. You will see that these two tables will be shown in gray after the change.
  7. Go to Diagram View, create a new hierarchy in the Product table, and name it as Product Category Hierarchy. Drag-and-drop the Product Category column (from the Product table), the Product Subcategory column (from the Product table), and EnglishProductName.
  8. Save the changes and view the model in Excel. You will see that a new hierarchy is created under the Product table with category, subcategory, and product names.

What just happened?

Relationships can be created easily in a tabular model. In the previous chapter, you saw that there are multiple kinds of relationships between measure groups and dimensions such as regular, referenced, fact, and so on. However, creating relationships in the tabular mode is as simple as a database relationship. This simplicity when creating relationships and also in other parts of tabular development is the main reason why a tabular model can be developed easier and faster than multidimensional. Steps 1 to 3 showed you how to add new tables into the tabular model and create or edit relationships. Only the one-to-many relationship is supported in the tabular model, but you can create many-to-many relationships with intermediate tables.

As you saw in the previous example, hierarchies can only be created from columns of a single table. As a result, if you want to create a hierarchy from multiple tables, you can use calculated columns. In this example, you saw how we created calculated columns in the product table that shows the corresponding category or subcategory value from the related tables. For creating those columns, a DAX function named RELATED is used; this function will get data from the related table's column (shown in steps 5 and 6).

After creating new calculated columns, there is no need to have two new tables from the user's point of view, so we make them hidden by just changing the hidden properties of those tables to true (step 7). We can also hide columns in each table as required.

Step 8 shows a method that is similar to the method in the previous example, same as the previous example for creating a hierarchy; it just uses columns of the product table to create a hierarchy of product categories.

主站蜘蛛池模板: 左云县| 罗源县| 敦煌市| 湖北省| 深泽县| 永登县| 轮台县| 黑山县| 台南市| 陆良县| 汤阴县| 平泉县| 新建县| 涞源县| 安吉县| 荃湾区| 栾城县| 深泽县| 台前县| 金昌市| 铜山县| 汾西县| 江阴市| 封开县| 应用必备| 井研县| 郴州市| 南乐县| 商城县| 福海县| 南开区| 同江市| 枝江市| 达孜县| 玉屏| 太白县| 博白县| 武鸣县| 阿坝县| 洛川县| 密山市|