There’s a new M function in the latest release of Power BI Desktop that I spotted: Tables.GetRelationships(). Here’s the help page for it:
Basically, what it does is return all the relationships between all of the tables in a data source such as SQL Server. The documentation is a bit cryptic but I worked out what it does.
When you use a function like Sql.Database(), you get a table that contains a list of all of the tables in a database. For example, if I use the expression:
Sql.Database("localhost", "adventure works dw")
On my local instance of the Adventure Works DW database, this is the table that is returned:
This, it turns out, is the “navigation table” that the Tables.GetRelationships() function needs for its first parameter. The column called “Data” in this table, which contains links to the actual tables in the database, is what Tables.GetRelationships() needs for its second parameter. Put the two functions together in a query like this:
let
Source = Sql.Database("localhost", "adventure works dw"),
Rels = Tables.GetRelationships(Source, "Data")
in
Rels
And you get the following table:
Expand all the columns here and you get a table with one row for every relationship detected between every table in the database:
Useful if you need to report on the structure of a database, I guess. It’s a shame that this isn’t available in Power Query in Excel yet (it isn’t as of September 2015, I assume it’s coming soon) because it would be cool to use this data with NodeXL.