Power Query – Automation and Parameters: Difference between revisions
No edit summary |
|||
Line 2: | Line 2: | ||
== Objective == | == Objective == | ||
Use parameters to automate queries and build dynamic data loading processes. | Use parameters to automate queries and build dynamic, reusable data loading processes in Power Query. | ||
== Files Used == | == Files Used == | ||
Line 8: | Line 8: | ||
== Creating a Parameter == | == Creating a Parameter == | ||
'''Goal:''' Create a user-defined value that can be reused throughout queries. | |||
'''Steps:''' | '''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''' | # Click '''OK''' | ||
== Using a Parameter in Filtering == | == Using a Parameter in Filtering == | ||
'''Goal:''' Filter a column dynamically using a parameter. | |||
'''Steps:''' | '''Steps:''' | ||
# | # In Excel → go to the '''Data''' tab → click '''Get Data''' → '''From File''' → '''From Workbook''' | ||
# | # Select and load '''ExchangeRates.xlsx''' | ||
# Go to '''Home''' tab → '''Keep Rows''' → '''Keep Rows Where...''' | # In the Navigator → select the worksheet → click '''Transform Data''' | ||
# | # In Power Query Editor: | ||
#* Select the column named `Currency` | |||
#* Go to the '''Home''' tab → click '''Keep Rows''' → choose '''Keep Rows Where...''' | |||
# In the dialog: | |||
#* Select the column `Currency` | |||
#* Condition: equals | |||
#* Select the value type as: '''Parameter''' | |||
#* Choose the parameter: `TargetCurrency` | |||
# Click '''OK''' | |||
== Using Parameters in M Code == | == Using Parameters in M Code == | ||
'''Goal:''' Apply parameter logic manually in the formula bar or Advanced Editor. | |||
'''Steps:''' | |||
# Make sure the parameter `TargetCurrency` exists | |||
# Click on the filtering step in the Applied Steps pane | |||
# Go to the formula bar and ensure the logic looks like: | |||
<syntaxhighlight lang="m"> | |||
= Table.SelectRows(Source, each [Currency] = TargetCurrency) | |||
</syntaxhighlight> | |||
This will dynamically filter based on the parameter value. | |||
== Parameter for File Path == | == Parameter for File Path == | ||
'''Scenario:''' | '''Scenario:''' Allow the user to select the file path dynamically instead of hardcoding it. | ||
'''Steps:''' | '''Steps:''' | ||
# | # In Power Query Editor → go to '''Home''' tab → click '''Manage Parameters''' → select '''New Parameter''' | ||
# Go to '''Home''' tab → '''New Source''' → '''Excel Workbook''' | # 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: | |||
<syntaxhighlight lang="m"> | |||
= Excel.Workbook(File.Contents(FilePath), null, true) | |||
</syntaxhighlight> | |||
== Automating Folder Imports == | == Automating Folder Imports == | ||
'''Goal:''' Use folder-based queries to dynamically import and combine files. | |||
'''Steps:''' | '''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 '''Transform Data''' | # Click '''OK''' and then '''Transform Data''' | ||
# In Power Query Editor | # In Power Query Editor → you’ll see a table with metadata for each file | ||
# Use a parameter | # 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: | |||
<syntaxhighlight lang="m"> | |||
= Folder.Files(FolderPath) | |||
</syntaxhighlight> | |||
== Additional Exercise: Parameterized Currency Filter == | == Additional Exercise: Parameterized Currency Filter == | ||
'''Goal:''' Build a flexible report | '''Goal:''' Build a flexible report showing exchange rates for one selected currency. | ||
'''Steps:''' | '''Steps:''' | ||
# Load '''ExchangeRates.xlsx''' | # Load '''ExchangeRates.xlsx''' into Power Query | ||
# | # Go to '''Home''' → '''Manage Parameters''' → '''New Parameter''' | ||
# | #* Name: `TargetCurrency` | ||
# Load the result | #* Type: Text | ||
#* Current Value: e.g. `USD` | |||
# Apply a filter to the `Currency` column using this parameter: | |||
#* Select `Currency` → '''Home''' → '''Keep Rows''' → '''Keep Rows Where...''' | |||
#* Condition: equals parameter `TargetCurrency` | |||
# Load the result: | |||
#* Go to '''Home''' → '''Close & Load To...''' → select output destination (table, worksheet, etc.) | |||
== Extension: Combine Parameters and Folder Query == | == Extension: Combine Parameters and Folder Query == | ||
'''Goal:''' Allow | '''Goal:''' Allow user to control which folder is imported dynamically. | ||
'''Steps:''' | '''Steps:''' | ||
# Create parameter `FolderPath` | # Create a parameter named `FolderPath` (type: Text) | ||
# | # Set its value to the full folder location where your files are stored | ||
# | # In Power Query: | ||
# Load | #* Go to '''Data''' tab → '''Get Data''' → '''From File''' → '''From Folder''' | ||
#* In the folder path dialog → paste the value from `FolderPath` | |||
# OR in Advanced Editor, replace the path with: | |||
<syntaxhighlight lang="m"> | |||
= Folder.Files(FolderPath) | |||
</syntaxhighlight> | |||
# Power Query will combine the files | |||
# Load the final result to Excel via '''Close & Load''' | |||
---- | ---- |
Revision as of 10:43, 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: Filter a column dynamically using a parameter.
Steps:
- In Excel → go to the Data tab → click Get Data → From File → From Workbook
- Select and load ExchangeRates.xlsx
- In the Navigator → select the worksheet → click Transform Data
- In Power Query Editor:
- Select the column named `Currency`
- Go to the Home tab → click Keep Rows → choose Keep Rows Where...
- In the dialog:
- Select the column `Currency`
- Condition: equals
- Select the value type as: Parameter
- Choose the parameter: `TargetCurrency`
- Click OK
Using Parameters in M Code
Goal: Apply parameter logic manually in the formula bar or Advanced Editor.
Steps:
- Make sure the parameter `TargetCurrency` exists
- Click on the filtering step in the Applied Steps pane
- Go to the formula bar and ensure the logic looks like:
= Table.SelectRows(Source, each [Currency] = TargetCurrency)
This will dynamically filter based on the parameter value.
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)
Additional Exercise: Parameterized Currency Filter
Goal: Build a flexible report showing exchange rates for one selected currency.
Steps:
- Load ExchangeRates.xlsx into Power Query
- Go to Home → Manage Parameters → New Parameter
- Name: `TargetCurrency`
- Type: Text
- Current Value: e.g. `USD`
- Apply a filter to the `Currency` column using this parameter:
- Select `Currency` → Home → Keep Rows → Keep Rows Where...
- Condition: equals parameter `TargetCurrency`
- Load the result:
- Go to Home → Close & Load To... → select output destination (table, worksheet, etc.)
Extension: Combine Parameters and Folder Query
Goal: Allow user to control which folder is imported dynamically.
Steps:
- Create a parameter named `FolderPath` (type: Text)
- Set its value to the full folder location where your files are stored
- In Power Query:
- Go to Data tab → Get Data → From File → From Folder
- In the folder path dialog → paste the value from `FolderPath`
- OR in Advanced Editor, replace the path with:
= Folder.Files(FolderPath)
- Power Query will combine the files
- Load the final result to Excel via Close & Load
→ Continue to Module 5: Power Pivot Overview
Return to Main Page