Importing an external relational database into Common Data Service and recreating the relationships.
In this post, I have used data from the Northwind database that comes with MS Access. It contains a number of tables with relationships. I selected the Customers and Orders table for the example. Following the example, I will explain how to bring a more complex database into the Common Data Service. The first step was to bring in the tables from MS Access by exporting them to Excel and saving them in my OneDrive for Business. In the folloiwng example, I am referring to the Customers table as the Parent and the Orders table as the Child. The Primary key from the Parent is the Foreign key in the Child table. The relationship is One to Many as one customer can have many orders but each order can have only one customer.
- Start with the Parent table first by opening it in Excel. Then with the cursor inside of the table, select the Data tab. Then select the From Table/Range
- Once this option is selected Power Query will open and the table can be edited. In this case, I have used the choose columns to eliminate some columns.
- After using the Power Query functions to transform the table to exactly those fields that I want in my Customer entity, I changed the name of the table under Query Settings to Customers and saved the table using Close and Load. It is important not to remove the Primary Key from the table, in this case ID.
- This will close Power Query and return to Excel with a new sheet named Customers.
- The File should then be saved to OneDrive for Business and closed. The next step is to create the Entity for the modified table. Open Dataflows in Powerapps and select New Dataflow from the upper left side of the screen. You will then name the dataflow and proceed to the next screen where you will choose the data source, in this case Excel
- Proceed to the next screen and select the Excel file from the OneDrive for Business. Then select the table modified and renamed in Power Query. Table will open and before proceeding, you can make sure you have the right one. Then choose Next in the lower right hand side of the screen. At this point, check the columns, all of them will likely have ABC123 next to the name. Change the column type by clicking on this icon and selecting the proper type. This is important for the primary key field and dates. The text columns can be left alone. After all transformations are done, select Next
- The next screen gives the options for creating the entity, naming it and mapping fields. Select create a new entity and in the upper right side, give the entity a name, use the Primary key as the key field and select a descriptive column as the Primary name field. All text fields are defaulted to multiline text but the name field must be single text. I used the dropdown and changed the Company field to text. Adjust the mapping appropriate to the data and proceed to the next step
- Follow the prompts and create the new Entity. Depending on the amount of data in the table, this can take anywhere from a few minutes to hours for very large tables. You will be notified when the process is completed and the new entity can be found in PowerApps.
- Open the Child (Orders) file in Excel and then open Power Query. Note that it contains the Foreign key field CustomerID. Use the Data tab and select from table/range to open Power Query.
The merged query will appear with the merge column on the right There will be a double arrow icon at the upper right.
The Customer GUID is now in the Orders query. Close and load it. The Excel file if it is in OneDrive for Business should be autosaved. If this option is not selected, then select it.- Now create the Child entity from the Orders data using exactly the same steps as taken for creating the Parent entity (customers). Start with Excel and use Power Query to choose the fields for the Entity. Then create the Orders entity the same way the Customer entity was created using the Dataflow option in PowerApps. I have not reproduced this as the steps are the same for creating the Parent entity.
- Once the entity is created, open it and add a Lookup Field and point it to the Customer entity. This will automatically create the Many to One relationship between the entities.
The process for recreating a more complex database using CDS from external sources uses the steps above. First, examine the external database for One to Many relationships. Start with Tables that only include the One side and import these first. These will be Lookup Tables for the most part. Then select tables that have lookup fields but are Parent tables to other Child tables. Finally bring in the lowest level Child tables. In my experience, this has been a very cumbersome process but appears to be the only way for importing a database with relationships as CDS uses its own GUIDs to define the relationships between entities.
Comments
-
Importing an external relational database into Common Data Service and recreating the relationships.
I actually found a better method.
Since CDS is the backbone to Dynamics, all of the data import/manipulation features of Dynamics can actually be used.
- Go to admin.powerplatform.microsoft.com
- Click on Environments on the left menu, select the environment you want to build in and click the "More Environment Actions" elipsis. From there you can select:
- Open Environment. When the environment opens, click the gear in the top right. Select Advanced Settings. From the top menu, select the arrow next to Settings and then Customizations. Then select Customize the System. This will put you in the standard Dynamics customization panel, which is much more robust and reliable for building out entities, fields and relationships.
- Settings. From here you can use the Dynamics Data Import Wizard, as well as bulk deletion, Duplicate detection and other features. The import wizard from here in Dynamics allows you to map lookup fields more explicitly and map option set values. It can also handle empty values here, which the CDS for PowerApps version can't do.
Basically if you want to use CDS, follow these steps and sneak back into the Dynamics side to do all your work. It is much more reliable.
*This post is locked for comments