简体   繁体   English

SQL 服务器表格 Z20F35E630DAF3494DBFA4C3F6 (F28)

[英]Unexpected Excel Pivot behaviour with SQL Server Tabular model (v2)

We use Excel from Office 365 with a SQL Server 2019 Tabular model我们使用 Office 365 中的 Excel 和 SQL 服务器 2019 表格 model

I have a "strange" behaviour of Excel Pivot regarding the level at which calculations are requested from DAX.关于从 DAX 请求计算的级别,我有 Excel Pivot 的“奇怪”行为。

I create 3 simple (SQL Server) tables, MyDim, DimColour and MyFact我创建了 3 个简单 (SQL Server) 表,MyDim、DimColour 和 MyFact

基表

There is an implicit hierachy (not enforced) between the BrandName and ItemName in the MyDim table. MyDim 表中的 BrandName 和 ItemName 之间存在隐式层次结构(未强制执行)。

I could not enforce this because nothing says that an Item can only belong to one brand.我无法强制执行这一点,因为没有任何东西可以说一件物品只能属于一个品牌。

I create a very basic measure to aggregate the fact although I don't really need it here我创建了一个非常基本的度量来汇总事实,尽管我在这里并不需要它

FactTotal := SUM(MyFact[FactValue])

And I also create 2 additional measures that are more interesting我还创建了 2 个更有趣的附加措施

LowestLevel :=
SWITCH (
    TRUE (),
    ISFILTERED ( 'MyDim'[ItemName] ), "Item",
    ISFILTERED ( 'MyDim'[BrandName] ), "Brand",
    "Neither Brand nor Item"
)

Lowest Level ALWAYS return a value, regardless of whether there are rows in the fact table.最低级别总是返回一个值,无论事实表中是否有行。

LowestLevelButItem :=
SWITCH (
    TRUE (),
    ISFILTERED ( 'MyDim'[ItemName] ), BLANK (),
    ISFILTERED ( 'MyDim'[BrandName] ), "Brand",
    "Neither Brand nor Item"
)

LowestLevelButItem returns a value as long as we are not filtering on ItemName只要我们不过滤 ItemName,LowestLevelButItem 就会返回一个值

Now, this is the behaviour which bothers us...现在,这是困扰我们的行为......

Open the cube in an Excel Pivot table, filter on colours Blue and Yellow在 Excel Pivot 表中打开立方体,过滤颜色蓝色和黄色

Result 1, ok结果1,好的

结果1,好的

This is the right result.这是正确的结果。 No problem here.这里没问题。

Add the measure LowestLevelButItem...添加度量 LowestLevelButItem...

Result 2, unexpected结果 2,出乎意料

结果 2,意外

Now, this is not the result we would like... I would not expect to see "Brand A" at all in this result现在,这不是我们想要的结果……我根本不希望在这个结果中看到“品牌 A”

It shows that the measure displayed in D4 has been evaluated at a different level than D5 & D6.它表明 D4 中显示的度量已在与 D5 和 D6 不同的级别进行评估。

D5 & D6 are evaluated at the Item level, as expected while D4 is evaluated at a higher level because there is no matching row at Item level... D5 和 D6 在项目级别进行评估,正如预期的那样,而 D4 在更高级别进行评估,因为在项目级别没有匹配的行......

Now, add the measure LowestLevel现在,添加度量 LowestLevel

Result 3, ok结果3,好的

结果3,好的

Now, I understand this result.现在,我明白了这个结果。

Because LowestLevel ALWAYS returns a value, it makes Brand A / Item 1 visible, which forces evaluation of the measure LowestLevelButItem at the Item level, hence blank().因为 LowestLevel 总是返回一个值,所以它使 Brand A / Item 1 可见,这会强制在 Item 级别评估度量 LowestLevelButItem,因此为 blank()。

The problem I have is with the second result...我遇到的问题是第二个结果......

Question问题

How could I force the Pivot table to NOT return the Brand if there is no suitable Item underneath?如果下面没有合适的物品,我怎么能强制 Pivot 桌子不退回品牌?

SSMS Behaviour SSMS 行为

If I browse my cube in SSMS, I get the results I would expect, which is that my measures are always evaluated at the leaf level only and rows are not returned unless they have leaf level members.如果我在 SSMS 中浏览我的多维数据集,我会得到我期望的结果,即我的度量始终只在叶级别进行评估,并且除非它们具有叶级别成员,否则不会返回行。

SSMS Result 1, same SSMS 结果 1,相同

SSMS 结果 1

SSMS Result 2 SSMS 结果 2

SSMS 结果 2,与 Excel 不同

SSMS did not return a row for "Brand A" with no Item member SSMS 没有返回没有项目成员的“品牌 A”行

SSMS Result 3 SSMS 结果 3

SSMS 结果 3

SQL Script to reproduce SQL 脚本重现

CREATE TABLE MyDim(MyDimId      INT NOT NULL PRIMARY KEY
                  ,BrandName    VARCHAR(100) NOT NULL
                  ,ItemName     VARCHAR(100) NOT NULL
                  );

GO

-- TRUNCATE TABLE MyDim;
INSERT INTO MyDim(MyDimId, BrandName, ItemName)
  VALUES(1, 'Brand A', 'Item 1')
       ,(2, 'Brand B', 'Item 2')
       ,(3, 'Brand B', 'Item 3');
GO

CREATE TABLE DimColour(Colour VARCHAR(100) NOT NULL PRIMARY KEY);
GO

INSERT INTO DimColour(Colour)
  VALUES('Red')
      , ('Blue')
      , ('Yellow');

CREATE TABLE MyFact(MyDimId   INT NOT NULL
                   ,Colour    VARCHAR(100) NOT NULL
                   ,FactValue INT NOT NULL
                   ,PRIMARY KEY(MyDimId, Colour)
                   );
GO

-- TRUNCATE TABLE MyFact;
INSERT INTO MyFact(MyDimId, Colour, FactValue)
  VALUES(1, 'Red',    1)
      , (2, 'Blue',   2)
      , (3, 'Blue',   4)
      , (3, 'Yellow', 8);

MDX generated for Result 2 queries为结果 2 查询生成的 MDX

  • By SSMS (producing the result I want)通过 SSMS(产生我想要的结果)

SSMS MDX

  • Excel MDX Excel MDX

Excel MDX

Just add the following IF clause to your measure:只需将以下 IF 子句添加到您的度量中:

LowestLevelButItem :=
IF(
  NOT(ISBLANK([FactTotal])),
  SWITCH (
    TRUE (),
    ISFILTERED ( 'MyDim'[ItemName] ), BLANK (),
    ISFILTERED ( 'MyDim'[BrandName] ), "Brand",
    "Neither Brand nor Item"
  )
)

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

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