DAX,PowerBI中的RANKX()问题 [英] RANKX() issues in DAX, PowerBI

查看:69
本文介绍了DAX,PowerBI中的RANKX()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习DAX,并且对PowerBI中的RANKX()感到困惑。
这是我的数据:

I am learning DAX and confused about the RANKX() in PowerBI. Here is my data:

这是我的度量标准:

Rank = RANKX(
    ALL(RankDemo[Sub Category]),
    CALCULATE(SUM(RankDemo[My Value])))

这是我的视觉效果:

RANKX()可以正常工作,但是该字段必须在PowerBI字段设置中[strong]总结[我的值] :

The RANKX() works fine, but the field [My Value] has to be summed in the PowerBI field setting:

如果我选择不要汇总,则排名将都是1.有人可以解释吗? Sum与DAX中的RANKX()或CALCULATE()有什么关系。谢谢。

If I choose Don't Summarize, the rank will be all 1. Anyone could explain this? What does the Sum have to do with the RANKX() or CALCULATE() in DAX. Thanks.

推荐答案

您遇到的问题与RANKX无关。问题出在所谓的隐含措施上-这是Power BI和Power Pivot中的一种(不幸的)常见不良做法。

The problem you are experiencing has nothing to do with RANKX. The issue is with what's called "Implicit measure" - an (unfortunately) common bad practice in Power BI and Power Pivot.

Power BI中的数值字段可以担任两个角色:

Numeric fields in Power BI can serve 2 roles:


  • 它们可以是DAX度量的输入(例如SUM()等)

  • ,或者它们可以是过滤器(即,与可视化中的子类别具有相同的功能)。

在不进行任何计算的情况下将我的值拖放到表中时(不汇总),您正在告诉Power BI您想要我的 值用作过滤器。在Excel数据透视表中,这等效于将我的值而不是值放到行区域。因此,表中的每一行现在都按子类别+我的值分组,而不仅仅是子类别(换句话说,您已经将我的价值作为过滤条件的一部分)。由于子类别+我的价值的每种组合都是唯一的,因此您实际上是在对包含1条记录(这就是为什么它总是返回1)的原因。

When you drop 'My Value" into a table without any calculation ("Don't summarize"), you are telling Power BI that you want "My Value" to serve as a filter. In Excel pivot tables, it would be equivalent to dropping "My Value" into "Rows" area instead of "Values". So each row in your table is now grouped by "Subcategory + My Value", not just "Subcategory" (in other words, you've made "My Value" a part of your filter context). Since each combination of "Subcategory + My Value" is unique, you are essentially ranking tables consisting of 1 record (that's why it always returns 1).

当您为我的值选择总和时,它不再是行过滤器-现在是度量值,因此您进行过滤现在我不是子类别 +我的价值,而是子类别,您的RANKX公式可以正常工作。您可以通过从表格中删除汇总的我的价值来轻松地看到这一点-RANKX度量仍将以相同的方式工作。

When you choose "SUM" for "My Value", it's no longer a row filter - it's now a measure. So you filter context now is not "Subcategory" + "My Value", but just "Subcategory", and your RANKX formula works properly. You can easily see this by removing summed "My Value" from the table - RANKX measure will still work the same way.

当您将此 SUM聚合用于我的价值时,是在告诉Power BI为您隐式创建DAX度量(这就是为什么它被称为隐式度量 )。每当将数字字段直接放入视觉对象时,都会发生这种情况。出于多种原因,这样的隐式度量被认为是经验丰富的设计师的不良做法,例如:

When you use this "SUM" aggregation for "My Value", you are telling Power BI to create a DAX measure for you implicitly (that's why it's called "implicit measure"). It happens whenever you drop a numeric field into a visual directly. Such implicit measures are considered a bad practice among experienced designers, for a number of reasons, for example:


  • 这很令人困惑(RANKX带来的麻烦)是一个典型的示例);

  • 您不能重复使用隐式度量(不能在其他DAX度量中引用它们)。

一个解决方案是:


  • 从不,请将数字字段直接放入视觉效果中。

  • 相反,请始终编写DAX度量,然后将其放入可视化对象。

在您的示例中,我将创建一个显式的DAX度量:

In your example, I would create an explicit DAX measure:

Total Value = SUM(RankDemo[My Value])

现在您可以在模型中的任何地方使用它。您可以将其放到视觉效果中查看我的价值总和。或者,您也可以在RANKX度量中使用它:

Now you can use it everywhere in your model. You can drop it into a visual to see sum of My Value. Or you can use it in your RANKX measure:

Rank = RANKX( ALL(RankDemo[Sub Category]), [Total Value])

这种设计的好处是:


  • 没有隐藏效果(您确切地知道[Total Value]的作用)

  • 您可以在许多其他公式中使用[Total Value],而无需编写一次又一次地求和。

  • 如果您在[总值]中更改DAX(例如,添加舍入),它将自动更新所有其他使用公式的地方。

  • 重新使用DAX度量可使公式更简洁,更易于理解。

  • No hidden effects (you know exactly what [Total Value] does)
  • You can use [Total Value] in many other formulas without need to write summation again and again.
  • If you change DAX in [Total Value] (for example, add rounding), it will automatically update all other formulas where it's used.
  • Re-using DAX measures makes formulas cleaner, easier to understand.

这篇关于DAX,PowerBI中的RANKX()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆