简体   繁体   English

Power BI DAX 度量:考虑视觉对象的筛选上下文,计算列中值的出现次数

[英]Power BI DAX measure: Count occurences of a value in a column considering the filter context of the visual

I want to count the occurrences of values in a column.我想计算列中值的出现次数。 In my case the value I want to count is TRUE().在我的情况下,我想要计算的值是 TRUE()。 Lets say my table is called Table and has two columns:假设我的表称为 Table 并且有两列:

boolean value
TRUE()  A
FALSE() B
TRUE()  A
TRUE()  B

All solutions I found so far are like this:到目前为止我找到的所有解决方案都是这样的:

count_true = COUNTROWS(FILTER(Table, Table[boolean] = TRUE()))

The problem is that I still want the visual (card), that displays the measure, to consider the filters (coming from the slicers) to reduce the table.问题是我仍然希望显示度量的视觉(卡片)考虑过滤器(来自切片器)以减少表格。 So if I have a slicer that is set to value = A, the card with the count_true measure should show 2 and not 3.因此,如果我有一个设置为 value = A 的切片器,则具有 count_true 度量的卡片应该显示 2 而不是 3。

As far as I understand the FILTER function always overwrites the visuals filter context.据我了解,过滤器 function 总是覆盖视觉过滤器上下文。

To further explain my intent: At an earlier point the TRUE/FALSE column had the values 1/0 and I could achieve my goal by just using the SUM function that does not specify a filter context and just acts within the visuals filter context.为了进一步解释我的意图:在较早的时候,TRUE/FALSE 列的值是 1/0,我可以通过使用 SUM function 来实现我的目标,它没有指定过滤器上下文,只是在视觉过滤器上下文中起作用。

I do not know for sure if this will fix your problem but it is more efficient dax in my opinion:我不确定这是否会解决您的问题,但在我看来它是更有效的 dax:

count_true = CALCULATE(COUNTROWS(Table), Table[boolean]) count_true = 计算(COUNTROWS(表),表[布尔])

If you still have the issue after changing your measure to use this format, you may have an underlying issue with the model.如果在更改度量以使用此格式后仍然存在问题,则可能是 model 存在潜在问题。 There is also the function KEEPFILTERS that may apply here but I think using KEEPFILTERS is overcomplicating your case.还有可能适用于此处的 function KEEPFILTERS,但我认为使用 KEEPFILTERS 会使您的情况过于复杂。

I think the DAX you gave should work as long as it's a measure, not a calculated column.我认为你给出的 DAX 应该可以工作,只要它是一个度量,而不是一个计算列。 (Calculated columns cannot read filter context from the report.) (计算列无法从报告中读取过滤器上下文。)

When evaluating the measure,在评估措施时,

count_true = COUNTROWS ( FILTER ( Table, Table[boolean] = TRUE() ) )

the first argument inside FILTER is not necessarily the full table but that table already filtered by the local filter context (including report/page/visual filters along with slicer selections and local context from eg rows/column a matrix visual). FILTER 中的第一个参数不一定是完整表,但该表已经被本地过滤器上下文过滤(包括报告/页面/视觉过滤器以及切片器选择和本地上下文,例如行/列矩阵视觉)。

So if you select Value = "A" via slicer, then the table in FILTER is already filtered to only include "A" values.因此,如果您通过切片器 select Value = "A" ,则 FILTER 中的表已被过滤为仅包含"A"值。

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

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