In one of my previous articles, we discussed about synthetic keys (Synthetic keys in Qlikview – Simplified). We discussed why synthetic keys are generated and came to conclusion that if we have multiple synthetic keys in our data model, it might be a result of bad bad data model and may deliver unexpected results. We also saw a few ways to remove synthetic keys and improve our data model.
This article starts where we finished our last article. We will discuss two more techniques to remove synthetic keys and optimize our data model in our QlikView application. These two techniques are:-
Let’s understand these two techniques in detail using examples:
A sales oriented company has year on year transaction datasets (one dataset for each year) with one or two different fields (due to base system changes or defects) but rest of the fields are similar. Company wants to show YoY sales trends using these datasets.
In this scenario, let’s load all YoY datasets in QlikView. As you would expect, QlikView creates synthetic keys to join these tables, as these tables have multiple common fields. You can see the data model with synthetic key below. Now to remove synthetic key, we can’t rename/ drop all these fields because they are significant and related with each other. Here, we need all the fields in a table to show YoY trends, monthly seasonality over the year and many more things. As you know, Qlikview automatically concatenate/ combines tables if they have same granularity and columns. However, in our scenario, some of the columns are different. Here we need to force concatenation using CONCATENATE and combine the datastes in a single table (See Snpashot on the right).
Below, you can also see that in the SALES table, both Employee_Type and Branch_Type are appearing with their available values and total number of records is N1 (number of records in 2010) + N2 (number of records in 2011).
In similar manner, if the granularity and columns in the tables are same, then we can use Concatenate which will merge the tables into one and resulting table will have the sum of rows of the two tables.
To perform this we have five tables, in which two are fact table and others are dimension (Below is table structure).
Above you can see that tables, “Sales” and “Plan” have three common fields and Dimension tables are also associated with both the fact tables.
Now, if we load all these tables directly to QlikView, it will result in a data model with synthetic keys (screenshot below).
Since the fact tables do not have similar columns, we can not go for concatenation. At the same time, we need them for our analysis as well. Now to remove synthetic key in this data model we should use LINK table. It links two or more fact tables by taking all common fields out of the original tables and places them into a new table (called link table).The new link table contains all possible combinations of values for the set of fields through a unique key and is associated with the original tables.
In simple words, we can say that the link table replaces the synthetic key table and it has all the combinations of the key fields that are common for fact tables. We should also create a new compound key to connect all three tables (Two Fact tables and Link Table) and remove common fields from fact tables.
Rules to define Link Table:-
Now let’s look at the methods to develop data model using Link Table:-
Step – 1 Load facts table, form key for all common fields and comment all common fields.
Step – 2 Create the Link Table by loading the distinct values of the fact tables
Step-3 Load other dimension tables.
Step-4 Reload it and we would have following data model without a synthetic key.
Above, you can see a data model with Link table and it has all common fields of fact tables.
In the examples above, we looked at both the scenarios, where we should go with CONCATENATION or LINK table. Both methods have their own advantages, Let’s look at some of these:
As mentioned before, multiple synthetic keys typically reflects bad data model. We had looked at a few methods to remove synthetic keys in past. In this article, we particularly looked at two methods – LINK table and Concatenation. Both the methods have their own advantages and applications. The choice of method should depend on the business requirement and the kind of analysis required from the data.
Have you found this series useful? We have simplified a complex topic – Synthetic keys and have tried to present it in simple and understandable manner. If you need any more help Synthetic Key and Data model, please feel free to ask your questions through comments below..
Its really awesome. I have a doubt, Here why you are generating Key and Temp_Key fields, why you are dropping again, Shall we try with single column with the name of Key. Can you please explain.... @Sub2a
Hi Sunil Thanks for the info. I do have the similar situation 2 fact tables, opportunity and campaign where there are no common fields across these tables. I have opportunity create date in opportunity fact and campaign start date in campaign fact, where i need to derive common date calender. Could you please suggest me on the same. Thanks Satish
Hi Sunil, In the second approach(Link Table) we have three columns in Common between the two fact tables i.e. Employee_Id, Product_Code, Branch, what if we try the concatenation concept here.