Power Query – Automation and Parameters: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= Module 4: Automation and Parameters = | |||
== Objective == | |||
Use parameters to automate queries and build dynamic, reusable data loading processes in Power Query. | |||
== Files Used == | |||
* [[Media:ExchangeRates.xlsx|Download ExchangeRates.xlsx]] | |||
== Creating a Parameter == | |||
'''Goal:''' Create a user-defined value that can be reused throughout queries. | |||
'''Steps:''' | |||
# Open Power Query Editor | |||
# In the ribbon → go to the '''Home''' tab → click '''Manage Parameters''' → select '''New Parameter''' | |||
# In the New Parameter dialog: | |||
#* Name: `TargetCurrency` | |||
#* Description: (optional) | |||
#* Required: Checked | |||
#* Type: '''Text''' | |||
#* Suggested Values: Any value | |||
#* Current Value: `EUR` (or any other currency from the dataset) | |||
# Click '''OK''' | |||
== Using a Parameter in Filtering == | |||
'''Goal:''' Allow the user to control the filtered currency by typing a value directly in an Excel cell. | |||
'''Steps:''' | |||
# In Excel: | |||
#* Type a value like `USD` in cell `A2` | |||
#* In cell `A1`, enter the header name: `Currency` | |||
#* Select cells `A1:A2` and press '''Ctrl+T''' to create a table | |||
#* Name the table (e.g. `ParameterTable`) via the '''Table Design''' tab | |||
# Go to the '''Data''' tab → click '''Get Data''' → '''From Table/Range''' | |||
# In Power Query Editor: | |||
#* Ensure the table has one column (e.g. `Currency`) and one row (e.g. `USD`) | |||
#* Right-click on the `USD` value → select '''Drill Down''' | |||
# This will create a query returning just the single value | |||
# Click '''Home → Close & Load To...''' → choose '''Only Create Connection''' | |||
# Rename the query to: `CurrencyParameter` | |||
# Next, load the main dataset (e.g. ExchangeRates.xlsx) | |||
# In Power Query Editor: | |||
#* Select the `Currency` column | |||
#* Click the filter icon → select a sample value like `USD` | |||
#* In the formula bar, replace the hardcoded value with your parameter query | |||
'''Example:''' | |||
Replace: | |||
= Table.SelectRows(Source, each [Currency] = "USD") | |||
With: | |||
= Table.SelectRows(Source, each [Currency] = CurrencyParameter) | |||
# Click '''Close & Load''' to return the filtered data to Excel | |||
'''Result:''' | |||
Whenever the value in the Excel cell is changed (e.g. from `USD` to `EUR`), clicking **Refresh All** will automatically update the result. | |||
== Parameter for File Path == | |||
'''Scenario:''' Allow the user to select the file path dynamically instead of hardcoding it. | |||
'''Steps:''' | |||
# In Power Query Editor → go to '''Home''' tab → click '''Manage Parameters''' → select '''New Parameter''' | |||
# In the New Parameter dialog: | |||
#* Name: `FilePath` | |||
#* Type: Text | |||
#* Current Value: Paste the full path to an Excel file (e.g. C:\\Data\\Sales_Q1.xlsx) | |||
# Click '''OK''' | |||
# Then, to load the file using this path: | |||
# Go to '''Home''' tab → click '''New Source''' → choose '''Excel Workbook''' | |||
# When prompted for the file path: | |||
#* Instead of browsing, paste the value from the `FilePath` parameter directly | |||
# OR: go to '''Advanced Editor''' and replace the hardcoded path: | |||
= Excel.Workbook(File.Contents(FilePath), null, true) | |||
== Automating Folder Imports == | |||
'''Goal:''' Use folder-based queries to dynamically import and combine files. | |||
'''Steps:''' | |||
# In Excel → go to the '''Data''' tab → click '''Get Data''' → '''From File''' → '''From Folder''' | |||
# In the folder selection dialog → paste or browse to a folder with multiple Excel/CSV files | |||
# Click '''OK''' and then '''Transform Data''' | |||
# In Power Query Editor → you’ll see a table with metadata for each file | |||
# Click the button in the '''Content''' column → select '''Combine''' → choose '''Combine & Transform Data''' | |||
# Power Query creates a function and imports a sample file structure | |||
'''Optional: Use parameter for folder path''' | |||
# Create a parameter named `FolderPath` (type: Text) | |||
# In the source step → click the gear icon on '''Source''' in the Applied Steps | |||
# Replace the folder path with `FolderPath` | |||
# OR go to Advanced Editor and replace: | |||
= Folder.Files(FolderPath) | |||
---- | |||
→ Continue to [[Power Query – Power Pivot Overview|Module 5: Power Pivot Overview]] | |||
Return to [[Power_Query_–_Basic|Main Page]] | Return to [[Power_Query_–_Basic|Main Page]] | ||
Latest revision as of 12:44, 22 June 2025
Module 4: Automation and Parameters
Objective
Use parameters to automate queries and build dynamic, reusable data loading processes in Power Query.
Files Used
Creating a Parameter
Goal: Create a user-defined value that can be reused throughout queries.
Steps:
- Open Power Query Editor
- In the ribbon → go to the Home tab → click Manage Parameters → select New Parameter
- In the New Parameter dialog:
- Name: `TargetCurrency`
- Description: (optional)
- Required: Checked
- Type: Text
- Suggested Values: Any value
- Current Value: `EUR` (or any other currency from the dataset)
- Click OK
Using a Parameter in Filtering
Goal: Allow the user to control the filtered currency by typing a value directly in an Excel cell.
Steps:
- In Excel:
- Type a value like `USD` in cell `A2`
- In cell `A1`, enter the header name: `Currency`
- Select cells `A1:A2` and press Ctrl+T to create a table
- Name the table (e.g. `ParameterTable`) via the Table Design tab
- Go to the Data tab → click Get Data → From Table/Range
- In Power Query Editor:
- Ensure the table has one column (e.g. `Currency`) and one row (e.g. `USD`)
- Right-click on the `USD` value → select Drill Down
- This will create a query returning just the single value
- Click Home → Close & Load To... → choose Only Create Connection
- Rename the query to: `CurrencyParameter`
- Next, load the main dataset (e.g. ExchangeRates.xlsx)
- In Power Query Editor:
- Select the `Currency` column
- Click the filter icon → select a sample value like `USD`
- In the formula bar, replace the hardcoded value with your parameter query
Example: Replace:
= Table.SelectRows(Source, each [Currency] = "USD")
With:
= Table.SelectRows(Source, each [Currency] = CurrencyParameter)
- Click Close & Load to return the filtered data to Excel
Result: Whenever the value in the Excel cell is changed (e.g. from `USD` to `EUR`), clicking **Refresh All** will automatically update the result.
Parameter for File Path
Scenario: Allow the user to select the file path dynamically instead of hardcoding it.
Steps:
- In Power Query Editor → go to Home tab → click Manage Parameters → select New Parameter
- In the New Parameter dialog:
- Name: `FilePath`
- Type: Text
- Current Value: Paste the full path to an Excel file (e.g. C:\\Data\\Sales_Q1.xlsx)
- Click OK
- Then, to load the file using this path:
- Go to Home tab → click New Source → choose Excel Workbook
- When prompted for the file path:
- Instead of browsing, paste the value from the `FilePath` parameter directly
- OR: go to Advanced Editor and replace the hardcoded path:
= Excel.Workbook(File.Contents(FilePath), null, true)
Automating Folder Imports
Goal: Use folder-based queries to dynamically import and combine files.
Steps:
- In Excel → go to the Data tab → click Get Data → From File → From Folder
- In the folder selection dialog → paste or browse to a folder with multiple Excel/CSV files
- Click OK and then Transform Data
- In Power Query Editor → you’ll see a table with metadata for each file
- Click the button in the Content column → select Combine → choose Combine & Transform Data
- Power Query creates a function and imports a sample file structure
Optional: Use parameter for folder path
- Create a parameter named `FolderPath` (type: Text)
- In the source step → click the gear icon on Source in the Applied Steps
- Replace the folder path with `FolderPath`
- OR go to Advanced Editor and replace:
= Folder.Files(FolderPath)
→ Continue to Module 5: Power Pivot Overview
Return to Main Page