First Thoughts On Best Practices For Combining Fabric Semantic Views

My post from two weeks ago on combining semantic views generated a lot of interest and made me start thinking about what the best practices for this are. It also raised an important question: if you want to combine two semantic views that share one or more common datasets (ie tables), how do you handle the problem that a semantic view can’t contain duplicate dataset definitions? I don’t have all the answers but I thought I would write a follow-up blog post showing a different way of organising and combining semantic views to solve the same scenario I described in that first blog post and mention some things I did differently which might turn out to be best practices.

Let’s start with the SalesSemanticViewLH lakehouse which, if you remember, contains three tables: the Sales fact table and the Employee and Product dimension tables. In that first post I put these tables in the dbo schema in the lakehouse which, apart from being lazy, also led to some confusion on my part when I ran into the behaviour documented here whereby semantic views can infer datasets even if they aren’t explicitly defined if the tables exist in the dbo schema:

Apart from confusing me, this behaviour may go away before GA so it’s not a good idea to depend on it. Thank you to the PM for semantic views, Vivienne Vereen, for explaining all this to me! So I moved the three tables from the dbo schema to a new schema called sales.

Next, I created one semantic view for each of the tables like so:

version: 0.2.0.dev0
name: "EmployeeSemanticView"
datasets:
- name: Employee
source: sales.Employee
version: 0.2.0.dev0
name: "ProductSemanticView"
datasets:
- name: Product
source: sales.Product
version: 0.2.0.dev0
name: "SalesSemanticView"
datasets:
- name: Sales
source: sales.Sales
metrics:
- name: SalesAmount
expression:
dialects:
- dialect: DAX
expression: "SUM('Sales'[SalesAmount])"

Note that I defined the SalesAmount metric on SalesSemanticView. When you’re combining semantic views to make more complex semantic views, it seems like a good idea to me to define metrics on the most basic semantic view that contains all the datasets needed for the metric.

Then I created a fourth semantic view that defined all the relationships between the three previous semantic views:

version: 0.2.0.dev0
name: "SalesRelationshipsSemanticView"
relationships:
- name: EmployeeToSales
from: Sales
to: Employee
from_columns:
- EmployeeID
to_columns:
- EmployeeID
- name: ProductToSales
from: Sales
to: Product
from_columns:
- ProductID
to_columns:
- ProductID

Last of all in this lakehouse I created a semantic view called SalesStarSchemaSemanticView which contained no yaml definition but contained only shortcuts to the four other semantic views defined earlier. When published this semantic view resulted in the following semantic model:

Here’s what the lakehouse looked like:

So as you can see there is one semantic view for each table, another for the relationships, and one which combines them all to create a semantic model containing a star schema.

I then moved onto the TargetsSemanticViewsLH lakehouse which only contains one table called Targets; again, I moved this table to a new schema called targets. I created one semantic view for the one table and defined the SalesTarget metric on it:

version: 0.2.0.dev0
name: "TargetsSemanticView"
datasets:
- name: Targets
source: targets.Targets
metrics:
- name: SalesTarget
expression:
dialects:
- dialect: DAX
expression: "SUM('Targets'[SalesTarget])"

…as well as a semantic view containing the relationships:

version: 0.2.0.dev0
name: "TargetsRelationshipsSemanticView"
relationships:
- name: EmployeeToTarget
from: Targets
to: Employee
from_columns:
- EmployeeID
to_columns:
- EmployeeID

The problem here is that there is no Employee table in this lakehouse (it’s in the Sales lakehouse) yet and no dataset for it defined anywhere. I don’t think there’s a good solution to this particular problem – more on this later – but I uploaded a text file to the TargetsRelationshipsSemanticView folder noting that there was a dependency on the Employee table from the SalesSemanticViewsLH lakehouse and the semantic view built on it.

After that I created a shortcut to the sales schema in the SalesSemanticViewsLH lakehouse so the Employee table was present (I could have just shortcut to the Employee table too) and then created semantic view called TargetsStarSchemaSemanticView containing only shortcuts to the TargetsSemanticView and TargetsRelationshipsSemanticView semantic views from the current lakehouse and a shortcut to the EmployeeSemanticView semantic view from the SalesSemanticViewsLH lakehouse. When published the resulting semantic model looked like this:

And here’s what the lakehouse looked like:

Finally, in the SalesAndTargetsLH lakehouse I created shortcuts to the sales and the targets schemas from the two previous lakehouses to bring the data in, as well as a semantic view called SalesAndTargetsSemanticView with shortcuts to all of the semantic views for the individual tables and the relationships (but not the two semantic views representing the star schemas) from those previous lakehouses. Here’s what this lakehouse looked like:

The only thing defined in this semantic view was the SalesPctTarget metric:

version: 0.2.0.dev0
name: "SalesAndTargetsSemanticView"
metrics:
- name: SalesPctTarget
expression:
dialects:
- dialect: DAX
expression: "DIVIDE([SalesAmount],[SalesTarget])"

Publishing this semantic view resulted in a semantic model with all the tables, relationships and metrics for the sales and targets data:

So, in summary, by creating individual semantic views for each table and one for the relationships in both of the source lakehouses, I was able to combine them all into a single semantic view. This isn’t an ideal solution though – if I create a shortcut to the Targets data, how do I know I also need to shortcut in the Employee table and semantic view from the Sales data if I don’t read the dependencies text file I mentioned? – and I would have preferred to shortcut in the two semantic views containing the star schemas for the Sales and Targets data and have Fabric somehow deal with the duplicate Employee dataset automatically.

This isn’t the only possible solution either. For example Donald Parish blogged about a totally different approach here. Yet another solution using all the semantic views in the SalesSemanticViewsLH and TargetsSemanticViewsLH lakehouses would be to create a semantic view with shortcuts to the SalesStarSchemaSemanticView and TargetsStarSchemaSemanticView semantic views and then, because creating a shortcut to a folder containing other shortcuts duplicates those shortcuts in the destination, you can then delete one of the shortcuts to the EmployeeSemanticView semantic view – for example the one underneath TargetsStarSchemaSemanticView:

But again, you have to know what to delete to make the final semantic view work – not easy, even if there is an error message about duplicate dataset names to help.

Anyway, as the functionality evolves towards GA and everyone has a chance to play with it I’m sure the actual best practices will emerge. I hope this post will provoke more debate and get us all closer to the point of knowing what we ought to be doing.

Leave a Reply

Turn on JavaScript to leave a comment.