简体   繁体   English

将参数传递给 DAX 查询报表生成器

[英]Pass a Parameter to DAX Query Report Builder

I need some help here.我需要一些帮助。 Below is a DAX query that I have copied over from Power BI into Power BI Report Builder.下面是我从 Power BI 复制到 Power BI Report Builder 的 DAX 查询。 I'm looking to pass a parameter into this query for 'edw dim_paycom_amcs_location_xref'[Paycom_Location_Desc].我希望将参数传递到此查询中以获取“edw dim_paycom_amcs_location_xref”[Paycom_Location_Desc]。 However, I'm not sure where to place it within the query.但是,我不确定将其放在查询中的哪个位置。 I've researched the heck out of this and no matter where I try to place it I receive errors.我已经对此进行了研究,无论我尝试将它放在哪里,我都会收到错误。 Can anyone help with this?有人能帮忙吗? Thank you very much.非常感谢。

      // DAX Query
DEFINE
  VAR __DS0FilterTable = 
    TREATAS({"2021"}, 'edw dimDate'[Year])

  VAR __DS0FilterTable2 = 
    TREATAS({"August"}, 'edw dimDate'[MonthName])

  VAR __DS0Core = 
    SUMMARIZECOLUMNS(
      'edw dimDate'[MonthYear],
      'edw dimDate'[Month],
      'edw dim_paycom_amcs_location_xref'[Paycom_Location_Desc],
      __DS0FilterTable,
      __DS0FilterTable2,
      "SumOvertime_Hours_by_Day", CALCULATE(SUM('PaycomHours'[Overtime_Hours_by_Day])),
      "SumReg_Hours_by_Day", CALCULATE(SUM('PaycomHours'[Reg_Hours_by_Day])),
      "Transportation", 'PaycomHours'[Transportation],
      "Total_Inbound_Tons", 'PaycomHours'[Total Inbound Tons],
      "Total_Inbound_Tons__excl_Yakima_", 'PaycomHours'[Total Inbound Tons (excl Yakima)],
      "No_Operating_Days", 'edw dimDate'[No.Operating Days],
      "Tonnage_Inbound__3rd_Party", 'PaycomHours'[Tonnage Inbound- 3rd Party],
      "Tonnage_Inbound__Intercompany", 'PaycomHours'[Tonnage Inbound- Intercompany],
      "Tonnage_Inbound___3rd_Party_Metal", 'PaycomHours'[Tonnage Inbound - 3rd Party Metal],
      "Tonnage___Intercompany_Metal", 'PaycomHours'[Tonnage - Intercompany Metal],
      "Tonnage___Intercompany_Hog_Fuel", 'PaycomHours'[Tonnage - Intercompany Hog Fuel],
      "Tonnage___3rd_Party_Hog_Fuel", 'PaycomHours'[Tonnage - 3rd Party Hog Fuel],
      "Total_Commodities_Volume_Sold", 'PaycomHours'[Total Commodities Volume Sold],
      "Tonnage___Intercompany_Cardboard", 'PaycomHours'[Tonnage - Intercompany Cardboard],
      "Tonnage___Intercompany_ALL", 'PaycomHours'[Tonnage - Intercompany ALL],
      "Tonnage___3rd_Party_ALL", 'PaycomHours'[Tonnage - 3rd Party ALL]
    )

  VAR __DS0PrimaryWindowed = 
    TOPN(
      501,
      __DS0Core,
      'edw dimDate'[Month],
      1,
      'edw dimDate'[MonthYear],
      1,
      'edw dim_paycom_amcs_location_xref'[Paycom_Location_Desc],
      1
    )

EVALUATE
  __DS0PrimaryWindowed

ORDER BY
  'edw dimDate'[Month],
  'edw dimDate'[MonthYear],
  'edw dim_paycom_amcs_location_xref'[Paycom_Location_Desc]

I've struggled a lot with parameters with DAX in Report Builder.我在报表生成器中使用 DAX 的参数时遇到了很多困难。 There are a few different approaches depending on what your needs are.根据您的需求,有几种不同的方法。

The first question, do you need users to be able to change the value of the parameter?第一个问题,你需要用户能够改变参数的值吗? If so, you will need to define it under "Parameters" in Report Data.如果是这样,您需要在报告数据的“参数”下定义它。

Next regardless of your previous answer, you need to define the parameter in your dataset.接下来,无论您之前的答案如何,您都需要在数据集中定义参数。 数据集属性窗口的屏幕截图显示名为 MonthDS 的参数映射到 [@Month] 的值和名为 CustomerDS 的参数映射到 [@Customer] 的值。 If you are using a user-selectable parameter, then use the notation shown in the screenshot of [@User_Parameter_Name].如果您使用的是用户可选择的参数,请使用 [@User_Parameter_Name] 的屏幕截图中显示的符号。 If you click into the function editor, you will see this corresponds to the VB.Net expression =Parameters!User_Parameter_Name.Value如果您点击进入函数编辑器,您将看到这对应于 VB.Net 表达式=Parameters!User_Parameter_Name.Value

The second question is if you are entering the query directly into the Query box in the Dataset Properties.第二个问题是您是否将查询直接输入到数据集属性的查询框中。 If you are just pasting your DAX from PBI into that box, then you should be good to go.如果您只是将 PBI 中的 DAX 粘贴到该框中,那么您应该很高兴。

However, if you are using the Query Designer (and I can't blame you if you don't), then you need to do as you have already discovered and re-declare the parameter within the Query Designer.但是,如果您使用的是查询设计器(如果您不使用,我不能怪您),那么您需要按照您已经发现的那样做,并在查询设计器中重新声明参数。 查询设计器查询参数窗口的屏幕截图,其中声明了一个名为 MonthQP 的参数 The problem with the Query Parameters here is that they really struggle with defaulting to empty values... Also, when I have a parameter, Query Designer always seems to forget that I'm using DAX and not MDX every time I opened it.这里的查询参数的问题是,他们真的很难默认为空值......此外,当我有一个参数时,查询设计器似乎总是忘记我每次打开它时使用的是 DAX 而不是 MDX。

But anyways, once you declare it here, then you can refer to them in the query itself.但无论如何,一旦您在此处声明它,您就可以在查询本身中引用它们。

Finally, if you need to pass a list of values instead of a single value, the only way I've gotten this to work is to use this strange, little-documented function RSCustomDaxFilter to create a filter table:最后,如果您需要传递一个值列表而不是单个值,我让它工作的唯一方法是使用这个奇怪的、文档很少的函数 RSCustomDaxFilter 来创建一个过滤表:

 VAR MonthFilter= 
    SUMMARIZECOLUMNS (
        'Month'[Month],
        RSCustomDaxFilter(@MonthQP,EqualToCondition, [Month].[Month], String
        )

I can make some inferences on how this function works;我可以对这个函数的工作原理做出一些推断; the Table.Field syntax and specifying the object type make me think at least modeled after VB.Net, but aside from examples of how to use it in pretty much only this exact scenario, I have yet to find any official documentation on how it works, and the best explanation is from Chris Webb's blog, back in 2019 . Table.Field 语法和指定对象类型让我认为至少是在 VB.Net 之后建模的,但是除了在几乎只有这种确切情况下如何使用它的示例之外,我还没有找到任何关于它如何工作的官方文档,最好的解释来自Chris Webb 的博客,早在 2019 年 Also, it loves to freeze when performing query preparation if you don't use the Query Designer!此外,如果您不使用查询设计器,它喜欢在执行查询准备时冻结!

I'd suggest the following:我建议如下:

DEFINE
    VAR __DS0FilterTable =
        TREATAS ( { "2021" }, 'edw dimDate'[Year] )
    VAR __DS0FilterTable2 =
        TREATAS ( { "August" }, 'edw dimDate'[MonthName] )
    VAR __DS0FilterTable3 =
        TREATAS ( { @Location }, 'edw dim_paycom_amcs_location_xref'[Paycom_Location_Desc] )
    VAR __DS0Core =
        SUMMARIZECOLUMNS (
            'edw dimDate'[MonthYear],
            'edw dimDate'[Month],
            'edw dim_paycom_amcs_location_xref'[Paycom_Location_Desc],
            __DS0FilterTable,
            __DS0FilterTable2,
            __DS0FilterTable3,
            [... Remainder of query the same ...]

Be sure to map @Location to your report parameter here:请务必在此处将@Location映射到您的报告参数:

数据集属性屏幕截图

DAX pattern schematic that I use.我使用的 DAX 模式示意图。 No need to use RSCustomDaxFilter function and thus you can use DaxStudio to work on your querys.无需使用 RSCustomDaxFilter 函数,因此您可以使用 DaxStudio 处理您的查询。 Hallelujah :}哈利路亚:}

/*  
History
    yyyymmdd  aa  build

Datasets
    abbrev = workspace.dataset.table

Notes    
    + etc
*/

EVALUATE

-- parse the parameter value lists ready for the main querys
VAR ParameterA = SUBSTITUTE (TRIM (SUBSTITUTE (@ParameterA, ",", "|")), "| ", "|" )


-- build the lists
VAR TableA =
    FILTER (
        SELECTCOLUMNS (
            'table',
            "column a", 'table '[column a],
            "column b", 'table '[column b],
            etc
        ),

        -- column a,  ParameterA
        SWITCH (
            TRUE (),
            LEN (ParameterA) = 0,             TRUE (),   -- ignore
            PATHCONTAINS (ParameterA, "All"), TRUE (),   -- ignore   

            IF ( PATHCONTAINS ( ParameterA, "NA" ),   -- NA parameter selection
                IF ( [column a] IN 
                         {"NA",
                          "0",
                          "Not Applicable",
                          "Select",
                          " ", 
                          "",  
                          BLANK()}, 
                     TRUE (),   -- NA value found
                     IF ( PATHCONTAINS ( ParameterA, [column a] ), 
                          TRUE (),   -- direct match found
                          FALSE ()   -- out of scope condition
                     ) 
                ),
                FALSE ()   
            ),
            TRUE (),

            PATHCONTAINS ( ParameterA, [column a] ), TRUE (),
            FALSE ()   
        )  && 

    etc
    )

VAR TableB =
    etc


-- join the lists
VAR Result =
    NATURALINNERJOIN (TableA, TableB)

RETURN 
    Result
    --ROW ( "ParameterA", """"& ParameterA &"""" )   -- parameter checker  

ORDER BY   
    [column a] ASC,
    [column b] ASC


/* testing framework daxstudio */
<Parameters etc
  <Parameter>
    <Name></Name>
    <Value xsi:type="xsd:string"></Value>   
  </Parameter>
etc
</Parameters>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM