Map database schema in Power BI











up vote
6
down vote

favorite












I've come across a video on youtube that describes How to Easily Map Your Database Schema in Power BI using the AdventureWorks database from Microsoft. Now I'm trying to replicate that example using another database. The problem is that many of my columns have got similar content, but different column names with prefixes such as pk_ or fk_ depending on which tables they are located in. And that causes the following query to fail:



SELECT
c.TABLE_NAME
,c.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS c
INNER JOIN
(SELECT
COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY COLUMN_NAME
HAVING COUNT(*) > 1
) dupes
ON dupes.COLUMN_NAME = c.COLUMN_NAME


Does anyone know if it's possible to fuzzy match column names or taking different prefixes into account to make this work? The very same question has been asked directly to the youtube OP. It can also be found on reddit.com, but the question remains unanswered.



I'm trying to wrap my head around some more advanced Power BI features and at the same time learn some much needed SQL, and I thought this would be a cool place to start, so any help is much appreciated!










share|improve this question




























    up vote
    6
    down vote

    favorite












    I've come across a video on youtube that describes How to Easily Map Your Database Schema in Power BI using the AdventureWorks database from Microsoft. Now I'm trying to replicate that example using another database. The problem is that many of my columns have got similar content, but different column names with prefixes such as pk_ or fk_ depending on which tables they are located in. And that causes the following query to fail:



    SELECT
    c.TABLE_NAME
    ,c.COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS c
    INNER JOIN
    (SELECT
    COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS
    GROUP BY COLUMN_NAME
    HAVING COUNT(*) > 1
    ) dupes
    ON dupes.COLUMN_NAME = c.COLUMN_NAME


    Does anyone know if it's possible to fuzzy match column names or taking different prefixes into account to make this work? The very same question has been asked directly to the youtube OP. It can also be found on reddit.com, but the question remains unanswered.



    I'm trying to wrap my head around some more advanced Power BI features and at the same time learn some much needed SQL, and I thought this would be a cool place to start, so any help is much appreciated!










    share|improve this question


























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I've come across a video on youtube that describes How to Easily Map Your Database Schema in Power BI using the AdventureWorks database from Microsoft. Now I'm trying to replicate that example using another database. The problem is that many of my columns have got similar content, but different column names with prefixes such as pk_ or fk_ depending on which tables they are located in. And that causes the following query to fail:



      SELECT
      c.TABLE_NAME
      ,c.COLUMN_NAME
      FROM INFORMATION_SCHEMA.COLUMNS c
      INNER JOIN
      (SELECT
      COLUMN_NAME
      FROM INFORMATION_SCHEMA.COLUMNS
      GROUP BY COLUMN_NAME
      HAVING COUNT(*) > 1
      ) dupes
      ON dupes.COLUMN_NAME = c.COLUMN_NAME


      Does anyone know if it's possible to fuzzy match column names or taking different prefixes into account to make this work? The very same question has been asked directly to the youtube OP. It can also be found on reddit.com, but the question remains unanswered.



      I'm trying to wrap my head around some more advanced Power BI features and at the same time learn some much needed SQL, and I thought this would be a cool place to start, so any help is much appreciated!










      share|improve this question















      I've come across a video on youtube that describes How to Easily Map Your Database Schema in Power BI using the AdventureWorks database from Microsoft. Now I'm trying to replicate that example using another database. The problem is that many of my columns have got similar content, but different column names with prefixes such as pk_ or fk_ depending on which tables they are located in. And that causes the following query to fail:



      SELECT
      c.TABLE_NAME
      ,c.COLUMN_NAME
      FROM INFORMATION_SCHEMA.COLUMNS c
      INNER JOIN
      (SELECT
      COLUMN_NAME
      FROM INFORMATION_SCHEMA.COLUMNS
      GROUP BY COLUMN_NAME
      HAVING COUNT(*) > 1
      ) dupes
      ON dupes.COLUMN_NAME = c.COLUMN_NAME


      Does anyone know if it's possible to fuzzy match column names or taking different prefixes into account to make this work? The very same question has been asked directly to the youtube OP. It can also be found on reddit.com, but the question remains unanswered.



      I'm trying to wrap my head around some more advanced Power BI features and at the same time learn some much needed SQL, and I thought this would be a cool place to start, so any help is much appreciated!







      sql sql-server tsql powerbi






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 21:35









      Lukasz Szozda

      76.5k1059101




      76.5k1059101










      asked Nov 5 at 13:44









      vestland

      3,21531942




      3,21531942
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted
          +50










          If you want to show relationships between tables then using common column names between two tables is not best idea.



          For example:



          CREATE TABLE tab(id INT PRIMARY KEY, name INT);
          CREATE TABLE tab2(id2 INT PRIMARY KEY, name INT);
          -- completely unrelated tables

          SELECT
          c.TABLE_NAME
          ,c.COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS c
          INNER JOIN
          (SELECT
          COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS
          GROUP BY COLUMN_NAME
          HAVING COUNT(*) > 1
          ) dupes
          ON dupes.COLUMN_NAME = c.COLUMN_NAME


          +-------------+-------------+
          | TABLE_NAME | COLUMN_NAME |
          +-------------+-------------+
          | tab | name |
          | tab2 | name |
          +-------------+-------------+


          db<>fiddle demo





          I would propose to use proper metadata views i.e. sys.foreign_key_columns:



          SELECT [table] = tab1.name,
          [column] = col1.name,
          [referenced_table] = tab2.name,
          [referenced_column] = col2.name
          FROM sys.foreign_key_columns fkc
          JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id
          JOIN sys.tables tab1 ON tab1.object_id = fkc.parent_object_id
          JOIN sys.schemas sch ON tab1.schema_id = sch.schema_id
          JOIN sys.columns col1 ON col1.column_id = parent_column_id
          AND col1.object_id = tab1.object_id
          JOIN sys.tables tab2 ON tab2.object_id = fkc.referenced_object_id
          JOIN sys.columns col2 ON col2.column_id = referenced_column_id
          AND col2.object_id = tab2.object_id;


          db<>fiddle demo2



          Then you need to choose appropriate visualisation method in PowerBI.






          share|improve this answer























          • Thank you for answering! Is your suggestion limited to a certain number of tables? Or will the query somehow map all tables and references from INFORMATION_SCHEMA.COLUMNS . Or something like that?
            – vestland
            Nov 12 at 8:02






          • 1




            @vestland It will return all tables that have FK relationships for particular database.
            – Lukasz Szozda
            Nov 12 at 16:18













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53155621%2fmap-database-schema-in-power-bi%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted
          +50










          If you want to show relationships between tables then using common column names between two tables is not best idea.



          For example:



          CREATE TABLE tab(id INT PRIMARY KEY, name INT);
          CREATE TABLE tab2(id2 INT PRIMARY KEY, name INT);
          -- completely unrelated tables

          SELECT
          c.TABLE_NAME
          ,c.COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS c
          INNER JOIN
          (SELECT
          COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS
          GROUP BY COLUMN_NAME
          HAVING COUNT(*) > 1
          ) dupes
          ON dupes.COLUMN_NAME = c.COLUMN_NAME


          +-------------+-------------+
          | TABLE_NAME | COLUMN_NAME |
          +-------------+-------------+
          | tab | name |
          | tab2 | name |
          +-------------+-------------+


          db<>fiddle demo





          I would propose to use proper metadata views i.e. sys.foreign_key_columns:



          SELECT [table] = tab1.name,
          [column] = col1.name,
          [referenced_table] = tab2.name,
          [referenced_column] = col2.name
          FROM sys.foreign_key_columns fkc
          JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id
          JOIN sys.tables tab1 ON tab1.object_id = fkc.parent_object_id
          JOIN sys.schemas sch ON tab1.schema_id = sch.schema_id
          JOIN sys.columns col1 ON col1.column_id = parent_column_id
          AND col1.object_id = tab1.object_id
          JOIN sys.tables tab2 ON tab2.object_id = fkc.referenced_object_id
          JOIN sys.columns col2 ON col2.column_id = referenced_column_id
          AND col2.object_id = tab2.object_id;


          db<>fiddle demo2



          Then you need to choose appropriate visualisation method in PowerBI.






          share|improve this answer























          • Thank you for answering! Is your suggestion limited to a certain number of tables? Or will the query somehow map all tables and references from INFORMATION_SCHEMA.COLUMNS . Or something like that?
            – vestland
            Nov 12 at 8:02






          • 1




            @vestland It will return all tables that have FK relationships for particular database.
            – Lukasz Szozda
            Nov 12 at 16:18

















          up vote
          4
          down vote



          accepted
          +50










          If you want to show relationships between tables then using common column names between two tables is not best idea.



          For example:



          CREATE TABLE tab(id INT PRIMARY KEY, name INT);
          CREATE TABLE tab2(id2 INT PRIMARY KEY, name INT);
          -- completely unrelated tables

          SELECT
          c.TABLE_NAME
          ,c.COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS c
          INNER JOIN
          (SELECT
          COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS
          GROUP BY COLUMN_NAME
          HAVING COUNT(*) > 1
          ) dupes
          ON dupes.COLUMN_NAME = c.COLUMN_NAME


          +-------------+-------------+
          | TABLE_NAME | COLUMN_NAME |
          +-------------+-------------+
          | tab | name |
          | tab2 | name |
          +-------------+-------------+


          db<>fiddle demo





          I would propose to use proper metadata views i.e. sys.foreign_key_columns:



          SELECT [table] = tab1.name,
          [column] = col1.name,
          [referenced_table] = tab2.name,
          [referenced_column] = col2.name
          FROM sys.foreign_key_columns fkc
          JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id
          JOIN sys.tables tab1 ON tab1.object_id = fkc.parent_object_id
          JOIN sys.schemas sch ON tab1.schema_id = sch.schema_id
          JOIN sys.columns col1 ON col1.column_id = parent_column_id
          AND col1.object_id = tab1.object_id
          JOIN sys.tables tab2 ON tab2.object_id = fkc.referenced_object_id
          JOIN sys.columns col2 ON col2.column_id = referenced_column_id
          AND col2.object_id = tab2.object_id;


          db<>fiddle demo2



          Then you need to choose appropriate visualisation method in PowerBI.






          share|improve this answer























          • Thank you for answering! Is your suggestion limited to a certain number of tables? Or will the query somehow map all tables and references from INFORMATION_SCHEMA.COLUMNS . Or something like that?
            – vestland
            Nov 12 at 8:02






          • 1




            @vestland It will return all tables that have FK relationships for particular database.
            – Lukasz Szozda
            Nov 12 at 16:18















          up vote
          4
          down vote



          accepted
          +50







          up vote
          4
          down vote



          accepted
          +50




          +50




          If you want to show relationships between tables then using common column names between two tables is not best idea.



          For example:



          CREATE TABLE tab(id INT PRIMARY KEY, name INT);
          CREATE TABLE tab2(id2 INT PRIMARY KEY, name INT);
          -- completely unrelated tables

          SELECT
          c.TABLE_NAME
          ,c.COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS c
          INNER JOIN
          (SELECT
          COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS
          GROUP BY COLUMN_NAME
          HAVING COUNT(*) > 1
          ) dupes
          ON dupes.COLUMN_NAME = c.COLUMN_NAME


          +-------------+-------------+
          | TABLE_NAME | COLUMN_NAME |
          +-------------+-------------+
          | tab | name |
          | tab2 | name |
          +-------------+-------------+


          db<>fiddle demo





          I would propose to use proper metadata views i.e. sys.foreign_key_columns:



          SELECT [table] = tab1.name,
          [column] = col1.name,
          [referenced_table] = tab2.name,
          [referenced_column] = col2.name
          FROM sys.foreign_key_columns fkc
          JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id
          JOIN sys.tables tab1 ON tab1.object_id = fkc.parent_object_id
          JOIN sys.schemas sch ON tab1.schema_id = sch.schema_id
          JOIN sys.columns col1 ON col1.column_id = parent_column_id
          AND col1.object_id = tab1.object_id
          JOIN sys.tables tab2 ON tab2.object_id = fkc.referenced_object_id
          JOIN sys.columns col2 ON col2.column_id = referenced_column_id
          AND col2.object_id = tab2.object_id;


          db<>fiddle demo2



          Then you need to choose appropriate visualisation method in PowerBI.






          share|improve this answer














          If you want to show relationships between tables then using common column names between two tables is not best idea.



          For example:



          CREATE TABLE tab(id INT PRIMARY KEY, name INT);
          CREATE TABLE tab2(id2 INT PRIMARY KEY, name INT);
          -- completely unrelated tables

          SELECT
          c.TABLE_NAME
          ,c.COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS c
          INNER JOIN
          (SELECT
          COLUMN_NAME
          FROM INFORMATION_SCHEMA.COLUMNS
          GROUP BY COLUMN_NAME
          HAVING COUNT(*) > 1
          ) dupes
          ON dupes.COLUMN_NAME = c.COLUMN_NAME


          +-------------+-------------+
          | TABLE_NAME | COLUMN_NAME |
          +-------------+-------------+
          | tab | name |
          | tab2 | name |
          +-------------+-------------+


          db<>fiddle demo





          I would propose to use proper metadata views i.e. sys.foreign_key_columns:



          SELECT [table] = tab1.name,
          [column] = col1.name,
          [referenced_table] = tab2.name,
          [referenced_column] = col2.name
          FROM sys.foreign_key_columns fkc
          JOIN sys.objects obj ON obj.object_id = fkc.constraint_object_id
          JOIN sys.tables tab1 ON tab1.object_id = fkc.parent_object_id
          JOIN sys.schemas sch ON tab1.schema_id = sch.schema_id
          JOIN sys.columns col1 ON col1.column_id = parent_column_id
          AND col1.object_id = tab1.object_id
          JOIN sys.tables tab2 ON tab2.object_id = fkc.referenced_object_id
          JOIN sys.columns col2 ON col2.column_id = referenced_column_id
          AND col2.object_id = tab2.object_id;


          db<>fiddle demo2



          Then you need to choose appropriate visualisation method in PowerBI.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 7 at 21:38

























          answered Nov 7 at 21:31









          Lukasz Szozda

          76.5k1059101




          76.5k1059101












          • Thank you for answering! Is your suggestion limited to a certain number of tables? Or will the query somehow map all tables and references from INFORMATION_SCHEMA.COLUMNS . Or something like that?
            – vestland
            Nov 12 at 8:02






          • 1




            @vestland It will return all tables that have FK relationships for particular database.
            – Lukasz Szozda
            Nov 12 at 16:18




















          • Thank you for answering! Is your suggestion limited to a certain number of tables? Or will the query somehow map all tables and references from INFORMATION_SCHEMA.COLUMNS . Or something like that?
            – vestland
            Nov 12 at 8:02






          • 1




            @vestland It will return all tables that have FK relationships for particular database.
            – Lukasz Szozda
            Nov 12 at 16:18


















          Thank you for answering! Is your suggestion limited to a certain number of tables? Or will the query somehow map all tables and references from INFORMATION_SCHEMA.COLUMNS . Or something like that?
          – vestland
          Nov 12 at 8:02




          Thank you for answering! Is your suggestion limited to a certain number of tables? Or will the query somehow map all tables and references from INFORMATION_SCHEMA.COLUMNS . Or something like that?
          – vestland
          Nov 12 at 8:02




          1




          1




          @vestland It will return all tables that have FK relationships for particular database.
          – Lukasz Szozda
          Nov 12 at 16:18






          @vestland It will return all tables that have FK relationships for particular database.
          – Lukasz Szozda
          Nov 12 at 16:18




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53155621%2fmap-database-schema-in-power-bi%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()