Forum Discussion
Power Bi Report Builder - DAX Query from Performance analyzer multiple parameter values issue
Hi All,
I have created a report using PowerBI Report builder and the dataset query is coming from Performance Analyzer.
I define a Year and Month parameter but if I choose a multiple value on year or month i'm getting an error or no data displayed on the report. If i choose single value on the parameter the report is working.
Another thing, if i choose multiple value on year parameter, i'm also getting an error.
cannot covert value <pii>2019|2020</pii> of type tp type integer while processing column <pii>[static column]->datetable[Received Year]</pii>
Parameter: set expression value
=Join(Parameters!Year.Value,"|")
=Join(Parameters!Month.Value,"|")
Below is the DAX query.
// DAX Query
DEFINE
VAR __DS0FilterTable =
TREATAS({@Year}, 'DateTable'[Received Year])
VAR __DS0FilterTable1 =
TREATAS({@Month}, 'DateTable'[MonthName])
VAR __DS0FilterTable2 =
TREATAS({"Phone"}, 'CSST'[Product Group])
VAR __DS0FilterTable3 =
TREATAS({"AER"}, 'CCST'[Category])
EVALUATE
TOPN(
501,
SUMMARIZECOLUMNS(
ROLLUPADDISSUBTOTAL(
'CSST'[Category], "IsGrandTotalRowTotal",
'CSST'[Client], "IsDM0Total",
I believe the parameters are just not working correctly or the DAX query, likely, something wrong I did, but I have no idea how to fix this. I have tried innumerous things! Please help!
Thank you!
So the basic order of operations for building a query as an expression is as follows:
1) you create a new data set and link it to a connection.
2) then you click the fx button to open the expression editor
3) then you start the expression with an equals and an opening double quote character eg. ="
4) then I normally open a text editor paste my query in there and do a global search for any double quote characters " and replace them with 2 double quote characters ""5) then you paste in your query and at the end you add a closing double quote character "
="DEFINE VAR __DS0FilterTable = TREATAS({2021}, 'DateTable'[Received]) VAR __DS0FilterTable2 = TREATAS({""November""}, 'CCST'[MonthName]) VAR __DS0FilterTable3 = TREATAS({""LEGACY""}, 'CCST'[Model]) VAR __DS0FilterTable4 = TREATAS({""DIY""}, 'CCST'[Type]) VAR __DS0FilterTable5 = TREATAS({""Normal""}, 'CCST'[Status]) VAR __DS0FilterTable6 = TREATAS({""OTHERS""}, 'CCST'[Category]) EVALUATE TOPN( 501, SUMMARIZECOLUMNS( ROLLUPADDISSUBTOTAL( 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[Year], ""IsGrandTotalRowTotal"", ROLLUPGROUP( 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[Month], 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[MonthNo] ), ""IsDM0Total"" ), __DS0FilterTable, __DS0FilterTable2, __DS0FilterTable3, __DS0FilterTable4, __DS0FilterTable5, __DS0FilterTable6, ""SumReceived"", CALCULATE(SUM('CCST'[Qty])), ""Good"", 'CCST'[Good], ""Good__"", 'CCST'[Good %] ), [IsGrandTotalRowTotal], 1, 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[Year], 1, [IsDM0Total], 1, 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[MonthNo], 1, 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[Month], 1 ) ORDER BY [IsGrandTotalRowTotal], 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[Year], [IsDM0Total], 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[MonthNo], 'LocalDateTable_9a516b7e-ac4d-49d6-a22c-5318bf076cd8'[Month] "So the above is just a hard coded expression, to incorporate parameters in you would take the exising hard coded filters and replace them. So the following month filter
TREATAS({""November""}, 'CCST'[MonthName])would become something like the following to manually join in the value from a parameter called Month
TREATAS({""" + Parameters!Month.Value + """}, 'CCST'[MonthName])
Anonymous wrote:
I'm not experiencing an error. so far i encountered no data displayed on my report. Imap the query field to field by copying the one from the query code.
This will be the cause of your problem. Because of the way Report Builder executes dax queries certain characters in the fields names get replaced with underscores. So if you have not mapped using the correctly encoded names you will just get an empty string in your fields. So if you have asked for the field "[Sales Amount]" but the query engine in Report Builder returns it as "__Sales_Amount__" then your dataset will contain rows, but all the fields will be blank.
It is much easier to use the approach I suggested in my previous reply and paste in the raw query first and let Report Builder generate the field mappings before you turn your query into an expression.
25 Replies
- d_gosbellSuper User
So the issue here is that when you mark a parameter as accepting multiple values Report Builder sets it as an array data type and DAX does not support array parameters. By using the JOIN() function you are converting the array to a string, but you query is then comparing the concatenated string to the values in the column which will not work.
You have 3 options here:
- you could use the RSCustomDaxFilter() function, note this is not a DAX function, but is something that the report builder engine pre-processes (see https://blog.crossjoin.co.uk/2019/11/03/power-bi-report-builder-and-rscustomdaxfilter/) This is what the query builder in Report Builder will generate for multi-value parameters and is probably the simplest approach.
- You could construct your entire query as a Report Builder expression and manually concatenate the parameter values in to the query, but this can get tricky to debug as you have to do a lot of escaping of quotes.
- You could use a PATHCONTAINS approach (see https://blog.crossjoin.co.uk/2018/05/16/a-new-approach-to-handling-ssrs-multi-valued-parameters-in-dax-queries/)
- AnonymousNot applicable
Thank you for the reply. By the way, How can I change the code for TREATAS to accomodate multiple values.
- d_gosbellSuper User
Anonymous wrote:
Thank you for the reply. By the way, How can I change the code for TREATAS to accomodate multiple values.
Treatas will accomodate multiple values, the problem is mapping the parameter from Report Builder to the parameter in the query.
If we take the top 3 lines of your query as an example
// DAX Query DEFINE VAR __DS0FilterTable = TREATAS({@Year}, 'DateTable'[Received Year])Currently with the JOIN in your parameter expression this will expand to the following with a single string value:
// DAX Query DEFINE VAR __DS0FilterTable = TREATAS({"2019|2020"}, 'DateTable'[Received Year])What it needs to be is something like the following (assuming your Year is an integer column)
// DAX Query DEFINE VAR __DS0FilterTable = TREATAS({2019,2020}, 'DateTable'[Received Year])The only way I know to use TREATAS is to click the fx button on the query and build the query as a dynamic expression using something like the following (option 2 from my first post)
You will noticed the red doubling up of quotes that is required in order to embed a quote inside a quoted string expression. This is the tricky bit of building your query this way. If you don't escape all the quotes correctly the report will not run and it can be tricky to find mistakes. But once you have it working it functions well. I have one report I build like this which is has about 15 optional parameters so I actually include/exclude entire sections of the query based on whether the parameters have a value or not.
="
// DAX Query
DEFINE
VAR __DS0FilterTable =
TREATAS({" + Join(Parameters!Year.Value,",") + "}, 'DateTable'[Received Year])
VAR __DS0FilterTable1 =
TREATAS({""" + Join(Parameters!Month.Value,""",""") + """}, 'DateTable'[MonthName])
VAR __DS0FilterTable2 =
TREATAS({""Phone""}, 'CSST'[Product Group])VAR __DS0FilterTable3 =
TREATAS({""AER""}, 'CCST'[Category])
EVALUATE
TOPN(
501,
SUMMARIZECOLUMNS(
ROLLUPADDISSUBTOTAL(
'CSST'[Category], ""IsGrandTotalRowTotal"",
'CSST'[Client], ""IsDM0Total"","- AnonymousNot applicable
Thank you d_gosbell Yes its a bit tricky on building this codeI will try this approach. I have 10 parameter to be created. Hopefully I can build this report which i'm a little bit late on my deadlines. This is my first time to create a report in PBI report builder using DAX Query Performance Analyzer.