Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Dynamic Parameter is not working with the slicer which have single quotes in the items like: ABC'S

Hi Team,

I am using dynamic parameter by following the microsoft doc. Dynamic M query parameters in Power BI Desktop - Power BI | Microsoft Docs

 

Using the document I have bing the parameter with the slicer that contains the values like: ABC, XYZ, PQR and also like: ABC's, XYZ's, PQR's.

I have successfully created the report and it is working fine with values like: ABC or PQR or XYZ

but when I am selecting XYZ's  from slicer the SQL statement is not working properly.

 

Below is the Power Query script: Here the  Para_ColumnName is a Parameter created to Bind the slicer.

 

let
//===================================================================================PART 1=======================================================//
selected_ColumnName = if Type.Is(Value.Type(Para_ColumnName), List.Type) then
Text.Combine({"'", Text.Combine( Para_ColumnName , "','") , "'"})
else
Text.Combine({"'" , Para_ColumnName , "'"}),

selectAll_ColumnName = if Type.Is(Value.Type(Para_ColumnName), List.Type) then
List.Contains(Para_ColumnName, "__SelectAll__")
else
false,
//===========================================================================================================================================//
All_ColumnName = if selectAll_ColumnName or selected_ColumnName="'ABC'" then "A" else "B",

//===========================================================================================================================================//
FinalQuery = if All_ColumnName ="A" then Source else Source1,
//=====================================================================================PART 2========================================================//
Source = Value.NativeQuery(Snowflake.Databases("","",[Role=""]){[Name=""]}[Data],
"
SELECT TOP 1000

List of Columns


FROM DBNAME.SCHEMANAME.TABLENAME
", null, [EnableFolding=true]),

//============================================================================================================================================//

Source1 = Source = Value.NativeQuery(Snowflake.Databases("","",[Role=""]){[Name=""]}[Data],
"
SELECT

List of Columns

FROM DBNAME.SCHEMANAME.TABLENAME

WHERE COALESCE (TRIM(UPPER(ColumnNAME)),'NA') IN ("&selected_ColumnName&")

", null, [EnableFolding=true]),

#"Removed Duplicates" = Table.Distinct(FinalResult)
in
#"Removed Duplicates"

 

 

1. When I Apply the slicer with values like:ABC or XYZ then this query works fine.

but when I use values like: XYZ's or ABC's then it gives me error

 

2. I have find the issue also, that the where clause is failed to escape the single quotes 

WHERE COALESCE (TRIM(UPPER(ColumnNAME)),'NA') IN ("&selected_ColumnName&")

 

3. can you please guide me on how can i make changes  in PART1 that can help to escpae the single quotes.

 

4. I have written below script to escape but its not working and giving error: which states, "Can not Conver the TYPE LIST to TYPE TEXT"

 

Single_quotes = if Text.Contains(Para_ColumnName,"'") then Text.Replace(Para_ColumnName,"'","\'") else Para_ColumnName,

 

But I dont know How to use Single_quotes in PART ONE.

 

 

 

 

 

  • Hi Anonymous ,

    Glad you solved the problem by yourself! Would you like accept your workaroud as solution? Then more people who have the same requirment will find the solution quickly and benefit here, thank you!

     

    Best Regards,
    Community Support Team _ kalyj

3 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    I have figure out the suggestion as below:

    selected_ColumnName = if Type.Is(Value.Type(Para_ColumnName), List.Type) then
    Text.Combine({"'", Text.Combine( List.Transform(Para_ColumnName,  each Text.Replace(_,"'","\'")   , "','") , "'"})
    else
    Text.Combine({"'" , Text.Replace(Para_ColumnName ,"'","\'") , "'"}),

    • v-yanjiang-msft's avatar
      v-yanjiang-msft
      Icon for Community Support rankCommunity Support

      Hi Anonymous ,

      Glad you solved the problem by yourself! Would you like accept your workaroud as solution? Then more people who have the same requirment will find the solution quickly and benefit here, thank you!

       

      Best Regards,
      Community Support Team _ kalyj