Forum Discussion
How to pass selected Field Parameter to Query M SQL Query
- 1 year ago
Hi Que_Ry_Sa
Passing a dynamic Field Parameter to a native SQL query isn't straightforward because SQL expects fixed column references, while Field Parameters are evaluated at runtime on the Power BI side.Unfortunately, Field Parameters can't directly influence the structure of a SQL query, because M/Power Query (and SQL behind it) doesn't interpret dynamic column names from slicers or field parameters the way a DAX visual would.
Here’s a possible approach you can try (I've used this successfully in similar setups):
- Create a parameterized SQL view in your SQL Server (or underlying source). This view takes a string input for the column name and applies a CASE or dynamic SQL inside the view logic itself.
- In Power BI:
- Keep using your Field Parameter slicer to let users pick the column.
- Create a mapping table that translates your slicer choices (e.g., "Brand", "Country") into the actual SQL-safe column names.
- Use a disconnected table and capture the selected column using a measure like:
SelectedCol = SELECTEDVALUE(ParamTable[Param Field])- Pass that value into a M parameter using Power Query (this requires breaking query folding and turning it into a native query), like:
SELECT CASE WHEN @SelectedColumn = 'Brand' THEN Brand WHEN @SelectedColumn = 'Country' THEN Country WHEN @SelectedColumn = 'PartnerStatus' THEN [Brand Partner Status] END AS DynamicColumn FROM SalesTable- Connect your slicer to the parameter via a what-if parameter or use Table.FirstValue() carefully with a properly filtered table not the field parameter table.
Selected values from field parameters can't be passed into M SQL queries. While M influences the semantic model, anything done in the report designer stays within the designer - it doesn't flow back into the query layer. Only parameter values in the query editor can be passed on to a M SQL query.