Cập nhật nội dung chi tiết về Conditional Merge In Power Query mới nhất trên website Beiqthatgioi.com. Hy vọng thông tin trong bài viết sẽ đáp ứng được nhu cầu ngoài mong đợi của bạn, chúng tôi sẽ làm việc thường xuyên để cập nhật nội dung mới nhằm giúp bạn nhận được thông tin nhanh chóng và chính xác nhất.
In this example I will use the WideWorldImportersDW sample database Microsoft offers. You should have a copy of SQL Server installed to play with for Power BI. The Developer version of SQL Server is 100% functional and free forever. You just cannot use it in production.
I am going to use two tables for this example, Fact.Sales and Dim.Customer. I only want my Fact Sales table to have customer info for the Buying Group Tailspin Toys. That info is in the Dim Customer table. I can do this several ways.
Do the merge, expand the Buying Group column from the Customer table, then filter to only show those rows.
Pre-filter the Customer table for Tailspin Toys in the Buying Group column, then do the merge. It would need to be an Inner Join, otherwise you will get nulls in the Sales table when you expand, and then you have to filter those out. Which you can do, but it is more steps.
You can bypass all of that and do it in one step.
I’m not going to bore you with how to do methods 1 and 2. Let’s do method 3, a conditional join!
First, create the merge as you normally would. In the Sales table, select Merge Queries from the ribbon, then select the Customer table. Select the Customer Key fields in both. Also make sure this is an inner join so it will remove all records from the Sale Table that are not Tailspin Toys. Your Merge dialog box will look like this:
Table.NestedJoin has the following parameters per MS documentation:
We want to tweak 3rd parameter – table2, which is our Customer table, We don’t want the full table2, we only want table2, filtered for Topspin Toys in the Buying Group field.
So in the code above, we need to replace the 3rd parameter which is just a reference to #”Dimension Customer” – the name of the Customer Table. (It is just Dimension Customer – the #”name here” syntax is just how Power Query references objects with spaces and special characters.)
We want to replace that with this code:
Table.SelectRows(#”Dimension Customer”, each [Buying Group] = “Tailspin Toys”)
Since 100% of the work is done on the SQL Server, it will be very fast compared to how it would run if the Power Query mashup engine on your PC or On-Premise Gateway would process it.
If you are using some other source, like text files or Excel files, folding won’t happen of course, but it should still perform well as this is single filter applied, not a filter applied for every row of either table.
I cannot say that this method is any faster than doing it the longer ways (methods #1 and #2 above) but it is shorter code. Plus, you can get fancier with the Table.SelectRows() function we used by filtering on multiple fields using and/or criteria. You may break folding if you get too fancy and the Power Query engine cannot figure the SQL out, so be careful. If performance is a must, methods #1 or #2 are more likely to fold with many filters in the condition, but you won’t know until you try. Happy Querying!
How To Install Power Query
Power query is a great tool built by Microsoft that will help you work with data in Excel. This tool is great for connecting to various external data sources, querying and transforming data, or cleaning and parsing data.
Web pages, Facebook
Excel, CSV, XML, Text or Hadoop (HDFS) Files
Various databases like MS Access, SQL Server, MySQL, Microsoft Azure SQL, Oracle, IBM DB2, PostgreSQL, Sybase, Teradata, OData etc…
This is available as an add-in for excel 2010 professional plus or 2013 and comes already built in for Excel 2016.
You can download Excel Power Query here from Microsoft.
Unfortunately, if you’re not running Excel 2010 professional plus or 2013, then you will need to upgrade to Excel 2016 in order to use this feature as it’s not available for previous versions of Excel. Mac user are also out of luck.
There are both a 32-bit and 64-bit versions and which one you choose will depend on the version of Excel which you have installed.
To check what version you have:
Go to the “File” tab.
Go to the “Help” section.
Here you will see the product version, if it says professional plus 2010, then you’re in luck.
Here you will either see 32-bit or 64-bit. Take note and download the correct Power Query add-in version accordingly.
To check what version you have:
Go to the “File” tab.
Go to the “Account” section.
Here you will see the product version.
In the screen that pops up, at the top you will either see 32-bit or 64-bit. Take note and download the correct Power Query add-in version accordingly.
Power query comes pre-installed in Excel 2016 but has been renamed to “Get & Transform” and is under the Data tab in the ribbon. If you have Excel 2016, then you don’t need to do anything to use it.
Download The Add-In
Go to the Microsoft website:
Select your preferred language.
Select The Correct Version
Select either the 32-bit or 64-bit version depending on your version of Excel.
Run The Setup Wizard
Follow The Setup
Follow the steps in the Setup Wizard.
Power Query Is Now Ready To Use
Now the next time you open up Excel, Power Query will be available to use under its own tab.
The Complete Guide To Power Query
What is power query?
Power Query is a business intelligence tool available in Excel that allows you to import data from many different sources and then clean, transform and reshape your data as needed.
It allows you to set up a query once and then reuse it with a simple refresh. It’s also pretty powerful. Power Query can import and clean millions of rows into the data model for analysis after. The user interface is intuitive and well laid out so it’s really easy to pick up. It’s an incredibly short learning curve when compared to other Excel tools like formulas or VBA.
The best part about it, is you don’t need to learn or use any code to do any of it. The power query editor records all your transformations step by step and converts them into the M code for you, similar to how the Macro recorder with VBA.
If you want to edit or write your own M code, you certainly can, but you definitely don’t need to.
Get the data used in this post to follow along.
What Can Power Query Do?
Every month you need to go to the folder where the file is uploaded and open the file and copy the contents into Excel.
You then use the text to column feature to split out the data into new columns.
The system only outputs the sales person’s ID, so you need to add a new column to the data and use a VLOOKUP to get the salesperson associated with each ID. Then you need to summarize the sales by salesperson and calculate the commission to pay out.
You also need to link the product ID to the product category but only the first 4 digits of the product code relate to the product category. You create another column using the LEFT function to get the first 4 digits of the product code, then use a VLOOKUP on this to get the product category. Now you can summarize the data by category.
Maybe it only takes an hour a month to do, but it’s pretty mindless work that’s not enjoyable and takes away from time you can actually spend analyzing the data and producing meaningful insight.
Where is Power Query?
Power Query is available as an add-in to download and install for Excel 2010 and 2013 and will appear as a new tab in the ribbon labelled Power Query. In 2016 it was renamed to Get & Transform and appears in the Data tab without the need to install any add-in.
Importing Your Data with Power Query
Get data from a single file such as an Excel workbook, Text or CSV file, XML and JSON files. You can also import multiple files from within a given folder.
Get data from various databases such as SQL Server, Microsoft Access, Analysis Services, SQL Server Analysis Server, Oracle, IBM DB2, MySQL, PostgreSQL, Sybase, Teradata and SAP HANA databases.
Get data from Microsoft Azure
Get data from online services like Sharepoint, Microsoft Exchange, Dynamics 365, Facebook and Salesforce.
Get data from other sources like a table or range inside the current workbook, from the web, a Microsoft Query, Hadoop, OData feed, ODBC and OLEDB.
We can merge two queries together similar to joining two queries in SQL.
We can append a query to another query similar to a union of two queries in SQL.
Note: The available data connection options will depend on your version of Excel.
Depending on which type of data connection you choose, Excel will guide you through the connection set up and there might be several options to select during the process.
A Simple Example of Importing Data in an Excel File
This will open a file picker menu where you can navigate to the file you want to import. Select the file and press the Import button.
When you’re satisfied that you’ve got everything you need from the workbook, you can either press the Edit or Load buttons. The edit button will take you to the query editor where you can transform your data before loading it. Pressing the load button will load the data into tables in new sheets in the workbook.
In this simple example, we will bypass the editor and go straight to loading the data into Excel. Press the small arrow next to the Load button to access the Load To options. This will give you a few more loading options.
The Query Editor
The Ribbon – The user interface for the editor is quite similar to Excel and uses a visual ribbon style command center. It organizes data transformation commands and other power query options into 5 main tabs.
Query List – This area lists all the queries in the current workbook. You can navigate to any query from this area to begin editing it.
Formula Bar – This is where you can see and edit the M code of the current transformation step. Each transformation you make on your data is recorded and appears as a step in the applied steps area.
Properties – This is where you can name your query. When you close and load the query to an Excel table, power query will create a table with the same name as its source query if the table name isn’t already taken. The query name is also how the M code will reference this query if we need to query it in another query.
Applied Steps – This area is a chronological list of all the transformation steps that have been applied to the data. You can move through the steps here and view the changes in the data preview area. You can also delete, modify or reorder any steps in the query here.
The Query List
The Query List has other abilities other than just listing out all the current workbook’s queries.
When you do eventually exit the editor with the close and load button, changes in all the queries you edited will be saved.
Copy and Paste – Copy and paste a query to make another copy of it.
Delete – Delete the query. If you accidentally delete a query, there’s no undo button, but you can exit the query editor without saving via close and load to restore your query.
Rename – Rename your query. This is the same as renaming it from the properties section on the left hand side of the editor.
Duplicate – Make another copy of the query. This is the same as copy and paste but turns the process into one step.
Move To Group – Place your queries into a folder like structure to keep them organised when the list gets large.
Move Up and Move Down – Rearrange the order your queries appear in the list or within the folder groups to add to your organisational efforts. This can also be done by dragging and dropping the query to a new location.
Create Function – Turn your query into a query function. They allow you to pass a parameter to the query and return results based on the parameter passed.
Convert To Parameter – Allows you to convert parameters to queries or queries to parameters.
Properties – Allows you to change the query name, add a description text and enable Fast Data Load option for the query.
The Data Preview
The main job of the data preview area is to apply transformation steps to your data and show a preview of these steps you’re applying.
You can choose from decimal numbers, currency, whole numbers, percentages, date and time, dates, times, timezone, duration, text, Boolean, and binary.
The Applied Steps
You can insert new steps into the query at any point by selecting the previous step and then creating the transformation in the data preview. Power Query will then ask if you want to insert this new step. Careful though, as this may break the following steps that refer to something you changed.
Edit Settings – This allows you to edit the settings of the step similar to using the gear icon on the right hand side of the step.
Rename – This allows you to rename the steps label. Instead of the displaying the generic name like “Filtered Rows“, you could have this display something like “Filtered Product Rows on Pens” so you can easily identify what the step is doing.
Delete – This deletes the current step similar to the X on the left hand side of the step.
Delete Until End – This allows you to delete the current step plus all steps up until the end. Since steps can depend on previous steps, deleting all steps after a step is a good way to avoid any errors.
Insert Step After – This allows you to insert a new step after the current step.
Move Up and Move Down – This allows you to rearrange the query steps similar to the dragging and dropping method.
Extract Previous – This can be a really useful option. It allows you to create a new copy of the query up to the selected step.
The Formula Bar
The File Tab
Close & Load – This will save your queries and load your current query into an Excel table in the workbook.
Close & Load To – This will open the Import Data menu with various data loading options to choose from.
Discard & Close – This will discard any changes you made to the queries during your session in the editor and close the editor.
Note, you will still need to save the workbook in the regular way to keep any changes to queries if you close the workbook.
Data Loading Options
You can choose to load the query to a table, pivot table, pivot chart or only create a connection for the query. The connection only option will mean there is no data output to the workbook, but you can still use this query in other queries. This is a good option if the query is an intermediate step in a data transformation process.
You’ll also be able to select the location to load to in your workbook if you selected either a table, pivot table or pivot chart in the previous section. You can choose a cell in an existing worksheet or load it to a new sheet that Excel will create for you automatically.
The other option you get is the Add this data to the Data Model. This will allow you to use the data output in Power Pivot and use other Data Model functionality like building relationships between tables. The Data Model Excel’s new efficient way of storing and using large amounts of data.
The Queries & Connections Window
Data Preview – This is a live preview of the data similar to when first setting up a query.
Last Refreshed – This will tell you when the data was last refreshed.
Load Status – This displays whether the data is loaded to a table, pivot table, pivot chart or is a connection only.
Data Sources – This will show you the source of the data along with a count of the number of files if you’re it’s a from folder query.
The Home Tab
Close – You can access the Close & Load and Close & Load To options from here. These are also available in the File tab menu.
Manage Columns – You can navigate to specific columns and choose to keep or remove columns.
Reduce Rows – You can manage the rows of data from this section. There are lots of options to either keep certain rows or remove certain rows. Keep or remove the top N rows, the bottom N rows, a particular range of rows, alternating rows, duplicate rows or rows with errors. One option only available for removing rows is to remove blank rows.
Sort – You can sort any column in either ascending or descending order.
Transform – This section contains a mix of useful transformation options.
Split Columns – This allows you to split the data in a column based on a delimiter or character length.
Group By – This allows you to group and summarize your data similar to a Group By in SQL.
Data Type – This allows you to change the data type of any column.
Use First Row as Headers – This allows you to promote the first row of data to column headings or demote the column headings to a row of data.
Replace Values – This allows you to find and replace any value from a column.
Combine – This sections contains all the commands for joining your query to with other queries. You can merge, append queries or combine files when working with a from folder query.
Parameters – Power Query allows you to create parameters for your queries. For example when setting up a from folder query, you may want the folder path to be a parameter as so you can easily change the location. You can create and manage existing parameters from this section.
Data Sources – This section contains the data source settings including permissions management for any data sources that require passwords to access.
New Query – You can create new queries from new data sources or previously used data sources from this section.
The Difference Between the Transform and Add Column Tabs
The bulk of all transformations available in power query can be accessed through either the Transform tab or the Add Column tab.
You might think there is a lot of duplication between these two tabs. For example, both tabs contain a From Text section with a lot of the same commands. It’s not really the case, there is a subtle difference!
When you use a command from the Add Column tab that is found in both tabs, it will create a new column with the transformed data and the original column will stay intact. Whereas using the equivalent command from the Transform tab will change the original column and no new column is created.
This is a critical point to be aware of!
The Transform Tab
Table – This section contains commands that will transform the entire table. You can group and aggregate your query, promote rows to headers, demote headers to rows, transpose your data, reverse row order, and count rows.
Any Column – This section contains commands that will work on any column of data regardless of data type. You can change the data type, automatically detect and change the data type, rename the column heading, find and replace values, fill values down (or up) a column to replace any blanks or nulls with the value above it (or below it), pivot or unpivot columns, move columns to a new location or convert a column to a list.
Text Column – This section contains commands for text data. You can split columns with a delimiter, format the case, trim and clean, merge two or more columns together, extract text, and parse XML or JSON objects.
Number Column – This section contains commands for numerical data. You can perform various aggregations like sums and averages, perform standard algebra operations or trigonometry and round numbers up or down.
Date & Time Column – This section contains commands for date and time data. You can extract information from your dates, times and duration data.
Structured Column – This section contains commands for working with nested data structures such as when your column contains tables.
The Add Column Tab
General – This section allows you to add new columns based on formulas or custom functions. You can also add index colummns or duplicate a column from here.
From Text – Very similar to the From Text section in the Transform tab, but these commands will create a new column with the transformation.
From Number – Very similar to the From Number section in the Transform tab, but these commands will create a new column with the transformation.
From Date & Time – Very similar to the From Date & Time section in the Transform tab, but these commands will create a new column with the transformation.
The View Tab
Layout – This section allows you to either show or hide the Query Setting pane (which contain the properties and applied steps) and the Formula Bar.
Data Preview – This section allows you to show or hide whitespace characters or turn the font into a monospace font in the data preview area. This is handy when dealing with data delimited by a certain number of characters.
Columns – This allows you to go to and select a certain column in the data preview. This command is also available in the Home tab.
Parameters – This allows you to enable parameterization in data sources and transformation steps.
Dependencies – This will open a diagram view of the query dependencies in the workbook.
In particular, the Query Dependencies view is a useful resource that allows you to see a visual representation of the data transformation process flow.
Power Query can seem overwhelming at first to someone new to it all, but the UI is very well laid out and easy to catch on to. While it might be new to a user, a lot of the concepts should be familiar to an Excel user already.
Getting familiar with all the parts of the editor and the layout of the ribbon tabs is an essential first step in exploring Power Query and incorporating it into your everyday work.
While there is a lot to learn about Power Query, it is worth putting in the time to learn. There is massive potential to save time in repetitive data cleaning and formatting tasks with it. It’s one of the most powerful and useful tools that has been added to Excel since pivot tables.
Want even more power query goodness? Then check out these Amazing Power Query Tips to help you get the most out of it!
Power Query Version Compatibility And Installation
I’ve put together a comparison table to help you identify Power Query version compatibility.
Power Query Version Compatibility Table
How to check what version of Excel you have
Discovering which version of Excel you have is slightly different depending on the version.
Excel 2013 and 2016
How to Install Power Query
Now you know which version of Excel you have and whether you can get Power Query you’re either annoyed you don’t have the correct version (I’ll tell you the cheapest way to solve that in a moment*), or you’re ready to go and install it.
Installing Power Query for Excel 2010 and 2013
In Excel 2010 and Excel 2013 Power Query is available as a free add-in. You can find out more about the system requirements and download it here.
Note: make sure you choose the correct version to match the bitness (32-bit or 64-bit) for your installation of Excel.
Once you’ve downloaded it simply run the MSI installer and follow the steps.
When the install is complete you’ll find a new tab for Power Query:
Note: if you’ve installed Power Query and it’s gone missing you can usually re-enable it by going to the COM add-ins.
Excel 2016 Onward and Office 365
In Excel 2016 onward and Office 365 Power Query is integrated and is included in the Data tab, which means you don’t need to download or install it. You’ll also find that in Excel 2016 Power Query took on a new name: Get and Transform (personally I preferred Power Query, but anyway):
*Power Query not available in your version of Excel
If you find you don’t have a version of Excel that has Power Query then the cheapest and easiest solution is to purchase the Standalone version of Excel 2013, or 2016 for around US$100 (price varies based on your local currency).
Go to chúng tôi and in the Search field type ‘Standalone Excel’. If you only need it for home use then you can choose the ‘non-commercial’ licence, which is cheaper.
The great thing about this version of Excel is that it comes with EVERYTHING. That means full Power Query functionality AND full Power Pivot functionality, as well as all the other standard Excel features.
Power Query in Action
So, now that you’ve installed Power Query here are some examples of ways you can use it:
Combine Excel Worksheets with Power Query Split text with Power Query Transpose Data with Power Query
Power Query Training
If you want to get up and running with Power Query fast then check out my Power Query course. I’ll have you up to speed in just 3.5 hours.
More Power Query Posts
Bạn đang đọc nội dung bài viết Conditional Merge In Power Query trên website Beiqthatgioi.com. Hy vọng một phần nào đó những thông tin mà chúng tôi đã cung cấp là rất hữu ích với bạn. Nếu nội dung bài viết hay, ý nghĩa bạn hãy chia sẻ với bạn bè của mình và luôn theo dõi, ủng hộ chúng tôi để cập nhật những thông tin mới nhất. Chúc bạn một ngày tốt lành!