How do I create a Power Query function that dynamically adjusts based on input parameters

0 votes

How do I create a Power Query function that dynamically adjusts based on input parameters?
I need a Power Query function that modifies its transformations based on input parameters. How can I design a dynamic function that adapts its behavior based on user-defined inputs to ensure flexibility and reusability?

1 day ago in Power BI by Evanjalin
• 19,330 points
21 views

1 answer to this question.

0 votes

If you want to build a Power Query function that can dynamically respond to input parameters, you must define a function that takes parameters to modify its transformation logic conditionally. Other approaches are given as follows:

1. Very Basic Dynamic Function for Filtering Data

You could build a function that filters a table according to some chosen column and value defined by the user:

(SourceTable as table, ColumnName as text, FilterValue as any) =>  
let  
    FilteredTable = Table.SelectRows(SourceTable, each Record.Field(_, ColumnName) = FilterValue)  
in  
    FilteredTable  

 FilteredTable  
Usage: DynamicFilter(Data, "Category", "Electronics") will return only rows where the "Category" column is "Electronics."


2. Function to Select Specific Columns Dynamically
This function allows users to choose which columns to keep:

(SourceTable as table, ColumnsToKeep as list) =>  
let  
    SelectedColumns = Table.SelectColumns(SourceTable, ColumnsToKeep, MissingField.Ignore)  
in  
    SelectedColumns  

Usage:

SelectColumnsDynamic(Data, {"Product", "Sales"}) will return only the "Product" and "Sales" columns.

3. Dynamic Transformation Function Based on User Inputs

This function allows users to apply different transformations (e.g., changing case, replacing values) dynamically

(SourceTable as table, ColumnName as text, Operation as text) =>  
let  
    TransformedTable =  
        if Operation = "Uppercase" then  
            Table.TransformColumns(SourceTable, {{ColumnName, Text.Upper}})  
        else if Operation = "Lowercase" then  
            Table.TransformColumns(SourceTable, {{ColumnName, Text.Lower}})  
        else  
            SourceTable  
in  
    TransformedTable  

Usage:

DynamicTransform(Data, "CustomerName", "Uppercase") converts all values in "CustomerName" to uppercase.

answered 1 day ago by anonymous
• 19,330 points

Related Questions In Power BI

0 votes
0 answers
0 votes
2 answers

How do I create a Power BI visual that dynamically adjusts based on user-selected filters and slicers?

The application of slicers and filters in ...READ MORE

answered Jan 23 in Power BI by anonymous
• 16,840 points
136 views
0 votes
2 answers

How do I create custom tooltips that display different information based on the visual or data point in Power BI?

Custom tooltips: Create a report page dedicated to detailed information and link that to visuals. For ...READ MORE

answered Jan 23 in Power BI by anonymous
• 16,840 points
165 views
0 votes
1 answer
0 votes
1 answer

Install Power BI Desktop

It’s a pretty simple process. All you ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,197 views
0 votes
1 answer

Few tips before I start creating Power BI dashboard

It’s always advisable to begin with the data ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,013 views
0 votes
1 answer

How do I format the KPI in Power BI

format the KPI by selecting the paint ...READ MORE

answered Oct 9, 2018 in Power BI by Kalgi
• 52,350 points
1,358 views
+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer

How can I create a measure that calculates the weighted average of a column dynamically based on slicer selections?

Using SUMX and DIVIDE in DAX, a ...READ MORE

answered 4 days ago in Power BI by anonymous
• 19,330 points
24 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP