简体   繁体   English

数据透视表分组如何与 DAX 中的计算过滤器交互? 我该如何覆盖它们?

[英]How do pivot table groupings interact with calculated filters in DAX? And how can I override them?

I have a table in my data model that has on-hand product counts, dates, and inventory source table names.我的数据模型中有一个表,其中包含现有产品计数、日期和库存源表名称。 I want to create a pivot table that shows the total product counts for each source but only on the last recorded date in the table.我想创建一个数据透视表,显示每个来源的总产品计数,但仅在表中的最后记录日期。

Here's a simplified version:这是一个简化版本:

事实表

I have a pivot built that uses the following measure:我有一个使用以下措施构建的枢轴:

=VAR LastDay = MAX(FactTable[Date]) 
Return 

CALCULATE(
    sum(FactTable[On Hand Quantity]),
    FILTER(FactTable,FactTable[Date] = LastDay)
)

The ideas is that it looks up the last date in the FactTable then filters the table to only include rows with a date that equals LastDay, then provides the sum of the on hand quantity for the item in the pivot filter.这个想法是它在 FactTable 中查找最后一个日期,然后过滤表以仅包含日期等于 LastDay 的行,然后在数据透视过滤器中提供该项目的现有数量的总和。

Something like this:像这样的东西:

在此处输入图片说明

Instead here's the real pivot (filtered for a specific item):相反,这是真正的枢轴(针对特定项目过滤):

数据透视表

The dates in Manual_Inv are all old and do not contain the LastDay. Manual_Inv 中的日期都是旧的,不包含 LastDay。 The pivot is showing the sum(FactTable[On Hand Quantity]) for the max date where the source table is Manual_Inv.数据透视表显示源表为 Manual_Inv 的最大日期的 sum(FactTable[On Hand Quantity])。 The weirder thing though is that it seems to understand that the value it's showing in the table is not what I'm asking for so it does not include it in the grand total.但更奇怪的是,它似乎明白它在表中显示的值不是我所要求的,因此它不包括在总计中。 The grand total is the actual total count for the last date available.总计是最后一个可用日期的实际总数。

I'm super confused as to how the pivot table groupings interact with my DAX code.我对数据透视表分组如何与我的 DAX 代码交互感到非常困惑。 From what I can tell the pivot groups (Source Table) are applied to the FactTable, and then the DAX code is run independently on each grouping and providing a result as though each group was its own table.据我所知,数据透视组(源表)应用于 FactTable,然后 DAX 代码在每个分组上独立运行并提供结果,就好像每个组都是自己的表一样。 And then afterwards it looks at the result, checks which ones have dates that match LastDay and only include those values in the grand total.然后它查看结果,检查哪些日期与 LastDay 匹配,并且只将这些值包含在总计中。

Can someone offer some insight into what is going on here and how I can go about fixing it?有人可以对这里发生的事情以及我如何解决它提供一些见解吗? My expected pivot looks the same as above, but the Current On Hand cell for Manual_Inv should be blank.我预期的枢轴看起来与上面相同,但 Manual_Inv 的 Current On Hand 单元格应该是空白的。

Will be away for a few days but I'll respond when I can.将离开几天,但我会尽快回复。

The maximum you are calculating for the LastDay variable is evaluated within the local filter context .您为LastDay变量计算的LastDay在本地过滤器上下文中计算的 This means that for Manual_Inv , it's looking up the last date only for the rows from the Manual_Inv source.这意味着对于Manual_Inv ,它只查找来自Manual_Inv源的行的最后日期。

To fix this, you can define LastDay to consider everything selected, not just rows that correspond to the current row label.要解决此问题,您可以定义LastDay以考虑选择的所有内容,而不仅仅是与当前行标签对应的行。 If there aren't any other tables involved (eg a calendar table with a relationship to your fact table) then you can probably write your variable like this instead:如果没有涉及任何其他表(例如与事实表有关系的日历表),那么您可能可以像这样编写变量:

VAR LastDay = CALCULATE ( MAX ( FactTable[Date] ), ALLSELECTED ( FactTable ) )

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

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