Merging and Linking Your Data
In this section, we are going to build from the previous steps and focus on merging our data and linking our data tables in the Data Model.
Scenario Background
Your research supervisor is impressed with how quickly and thoroughly you are handing the data. But just when we think that we have everything covered, they tell you that there is another team that they are supervising that has been working on another portion of the survey. It is dealing with the same survey respondents as we have been analysing over the past seven years but this portion of the survey captures the environmental attitudes of the respondents. Your research supervisor wants you to connect this data to the rest of the survey data somehow.
This new dataset is in an Excel file not a CSV file. When we click on it, we see that it records opinions from the same people in our survey but this time we have all 7 years’ worth of data in 1 file as opposed to seven separate files.
Again, note the data structure of this file is similar to the one in the field surveys: headers at the top, no empty columns or rows, each column only has one data type, no totals or subtotals, just a series of records. We have the respondents field, and the results of how they scored on three different metrics ranging from a theoretical 1 all the way to 100. Your research supervisor thinks that the three different metrics are too noisy and they want to simply see a differentiation between people who have High and Low environmental engagement.
Scenario Activity 1
Let’s start working on our new data. We need to use the three index outputs to create a differentiation between people who have High and Low environmental engagement just like our research supervisor asked for. Our Research Supervisor didn’t really set any parameters for this so let’s just create them for now. If they are not what our supervisor is looking for, we can always tweak them afterwards quite easily. So let’s assume that to be considered as having high environmental engagement, a survey respondent needs to achieve a 75 or more in all three categories.
We can try to create a conditional column to do this but we quickly realise that we can’t do it that way because the conditional column does not allow us to add an “AND” condition. So we have to use the M language to do this. Again, we're doing this to show you how to use the M language, and to gain a little familiarity with its structure, not necessarily for you to remember this exact formula.
So first, let’s click on the Custom Column button in the Add Column tab ribbon. What do we want to call this column? Let’s call it OverallEngagement
Now we need to type in our code. It’s not super intuitive but once you see it, you can quickly understand how it works:
each if [EnvConcernIndex] >= 75 and [PolicySupportIndex] >= 75 and [PersonalResponsibilityIndex] >= 75 then "High" else "Low"
We can now press the OK button.
A couple of things to keep in mind. Note that Power Query completes the M language code from our query.
It added the formula name to add a column: Table.AddColumn(). In fact, if we wanted to see more about this, we could go back in the Added Custom step, pressing on the gear icon and pressing on the link at the bottom.
It brings us to a website with all the Power Query functions grouped by category. If we pick the Table functions, we have whole host of functions (table construction, conversions, information, row operations, column operations, transformation). When we click on Table.AddColumn(), we see its syntax, a blurb about it and an example.
So we note that it starts with the table, then the new column name then a column generator or what’s going to create the column (in this case, the formula we inputted). There’s also an optional column type. The formula returns the data as a table.
The first argument (table) can be a bit puzzling. What table? In our case, it says #“Changed Type” in the first argument, or the table argument.
So what’s happening here is that Power Query is using the end of the previous step as its source for the table. In fact, many of the M language functions use the previous step as the beginning for their formula. It’s a little like a chain of events where the steps are tied together within the formulas themselves.
So we figured out the table issue, and the new column name is the next argument which we assigned as OverallEngagement. Then we have our formula and we have the option of putting in the data type if we want. We note that the data type has not yet been identified in this column. We could use the detect data type button in the Transform tab ribbon or click on the data type icon of the column and change it to text but that would create an extra step.
Let’s keep it clean and instead simply fold that in to our custom formula by typing in ", Text.Type". Note that the data type is now changed and no extra step has been created.
Suppose we wanted to make this a little more sophisticated, and impress our research supervisor, we could add a Medium component say, from 50 to 74. How could we do that? Let’s click on the gear icon of the Added Custom step. Let’s copy the conditions for High and paste them underneath it. Let’s make sure there is an "else if" after the “High”. Now we can change this to 50 on all three counts, and have then “Medium” and still finish with else “Low”.
Now we have three categories instead of two. We could continue to do this to add as much granularity as we thought was necessary. Also note that because we changed the formula, we need to go back and rewrite ", Text.Type" at the end of the M code to change the data type.
Now that we have our data for OverallEngagement, we could delete the three index columns. That’s one of the nice things about Power Query – it’s not reliant on the source data once something has been executed, unlike Excel. In Excel, if you delete the source cells, the formulas will no longer work.
That’s looking pretty good now. Let’s rename our query EnvironmentalAttitudes.
Ok, now we can begin to merge the two pieces of the survey together. This is very similar to the example in the previous step where we merged the different province outputs together using a lookup table. In this case, we will merge the FieldSurveyAll table with the EnvironmentalAttitudes table. Remember, we do this by pressing on the “Merge Queries” button in the Home tab ribbon. Let’s highlight the FieldSurveyAll query and press the button. We get the choice between Merge Queries and Merge Queries as New. This time, let’s click on the latter since we want a brand new query in this case. So we will select the field that the lookup table will lookup, so in this case, the Respondent_ID, then we will select the table we want to work with (EnvironmentalAttitudes) and select the lookup field in that, also Respondent_ID. We can pick the kind of join we want, and again this time we will leave it as the left join which akin to doing an XLOOKUP() in Excel. We see that 70,000 of 70,000 entries have a match which is great.
When we press ok, we have the Environmental_Attitudes table merged onto our FieldSurveyAll table. That’s a good start but not quite what we want. Let’s press on the double arrow icon and pick what we want to retain. We just want the Overall Engagement field and we don’t want to include the original column prefix.
We now have that data field merged with the FieldSurveyAll dataset. Let’s rename this query as FieldSurveyAll+Engagement.
We could keep working on this dataset to try to find new insights with the combined data. Just an FYI, this query was created to show you how to merge queries together but we won’t be using it going forward as we already have the information we need in the Data Model which we will work with in the next video.
Scenario Background 2
After showing this to our Research Supervisor, they tell us that they would like us to combine all of the data sources together, the Field survey data, the Environmental Attitudes survey data and the City Program data.
We could try to merge all of this together and that would work but another way to combine the data is to use the Data Model which is much more scalable. We discussed the data model a little bit during our introduction to Power BI series where we linked our data tables. Let’s dig into it a little more here. The data model (which is confusingly also called Power Pivot) is not necessarily part of Power Query but Power Query allows us to export our datasets into the Data Model in order to link them together. So let’s start by doing that, let’s send our data to the data model.
And now that query should be in the data model. Just before we are ready to start working on the Data Model, our Research Supervisor comes in and tells us they think that applying a 75 for HIGH engagement in the environmental attitudes table is a bit too high and they recommend going down to 70. Otherwise, everything is perfect.
Scenario Activity 2
This is a pretty easy fix. Let’s double click on the query. We can either press on the gear icon of the Added Custom step and replace the 75s with 70s or do it directly in the formula bar. If we do it in the applied steps, we’ll need to re-enter the ,Text.Type at the end of the formula again. By doing it directly in the formula bar, that part of the formula remains intact so let’s do it that way.
Ok, let’s close and load this. Since this was saved as a connection and in the data model originally, that’s how it’s going to be loaded in again but this time with the updated data.
Now that all our data is ready, we can access the Data Model. We can do this by pressing on the data model button in the Data tab ribbon or by going to the Power Pivot tab and pressing on the Data Model button in that ribbon.
When we go to Diagram View, we can see our three tables there – matching our three queries that we put in the Data Model. Right now they are independent tables but we can link them via a common field.
Let’s take our FieldSurveyAll table and link it to the EnvironmentalAttitudes table via the RespondentID field. We can do this by picking one of the common fields and dragging it onto the other corresponding field. This line denotes a relationship and if we click on it, it highlights the fields that form that connection. The 1 means that there is only 1 instance of RespondentID in that table so every RespondentID in that table is unique. The asterisk means that there are many instances of the same RespondentIDs in that table.
This is known as a 1 to many relationship. But because we know our data, we know that this should be a 1:1 relationship since RespondentID is unique in both tables. Power Pivot doesn’t capture the 1:1 relationships so it assigns a many to one of the tables. In fact, since this is really a 1:1 relationship, it would be optimal to use the merged table instead. I simply did it this way to explain the point.
So let’s go back and remove both the FieldSurveyAll and the EnvironmentalAttitudes tables from the data model and instead let’s put in the FieldSurveyAll + Engagement in the Data Model since it captures all that data in one table.
Now if we go back to the data model, we can link the FieldSurveyAll + Engagement table with the City Program table via the common field of City. We will remember that there is only 1 of every city in the City Program Table and there are multiple examples of the same city in the Field Survey All Table. For example, Halifax shows up once in the City Program but many times in Field Survey All since it will appear anytime there is a survey respondent from that city. So with that, let’s create a link between these two by picking the City Field in the FieldSurveyAll + Engagement table onto the City Program table to be precise about the direction of the relationship.
Now we can see that there is a 1 to many relationship between these two tables as well via the two City fields.
There we go. We’ve now completed our Data Model with two linked tables giving us a clean and efficient structure to analyse our data across both tables simultaneously. If we were to receive more data tables that fit into this framework, we could include them in the Data Model as well.