为 ssas 表格建模正确的星型模式

modeling correct star schema for ssas tabular

我正在使用 ssas 表格 (powerpivot),需要设计数据模型并编写一些 DAX。 我的关系数据库模型中有 4 个 table:

订单(order_id, order_name, order_type)
斑点 (spot_id,order_id,spot_name,spot_time,spot_price)
SpotDiscount (spot_id, discount_id, discount_value)
折扣 (discount_id, discount_name)

一个订单可以包含多个点,但是一个点(spot_id1)只能属于一个订单。
一个点可以包含不同的折扣,每个折扣都有一个 discount_value。
例如:
Order_1spot_1 (spot_price 10), spot_2 (spot_price 20)
Spot_1discount_name_1(discount_value 10)和 discount_name_2 (discount_value 20)
Spot_2discount_name_1(discount_value 15)和 discount_name_3 (discount_value 30)

我需要写两个度量:price(sum) 和 discount_value(average)

如何使用事实 table(或者两个事实 table 正确设计星型模式,以便我在我的 powerpivot 多维数据集中可以获得:

如果我选择 discount_name_1 我应该得到 order_1 与 spot_1 和 spot_2 以及 order_1 级别的价格将具有值 50 和 discount_value = 12,5
如果我选择 discount_name_3 我应该得到 order_1 只有 spot_2 并且订单级别的价格 = 20 并且 discount_value = 30

你说

One order can include multiple spots but one spot (spot_id 1) can only belong to one order.

您在该声明上方给出的 table 定义不支持该内容。在 table 定义中,一个订单只有一个点,但(除非您已将唯一索引添加到 spot_id 上的订单)每个点可以有多个订单。每个Spot还可以有多重折扣。

如果你想用你的话描述关系,table定义应该是:

Orders(order_id, order_name, order_type)
OrderSpot(order_id, spot_id) -- with a Unique index on spot_id)
Spots (spot_id, spot_name, spot_time, price)

或:

Orders(order_id, order_name, order_type)
Spots (spot_id, spot_name, spot_time, order_id, price)

您可以创建以 Order 作为事实 table,在 Spot Table 中具有一维的 ssas 多维数据集。如果您随后添加 SpotDiscount 和 Discount tables 以及它们的关系(SpotDiscount 到 Spot,Discount 到 SpotDiscount),您有一个 1 维。

根据评论编辑

嗯,事实 table 会有 order_id、order_name、order_type

维度将由其他 3 个 table 组成,并且包含您感兴趣的列:可能是 spot_name、spot_time、spot_price、 discount_name, discount_value.

事实(OrderKey、SpotKey、DiscountKey、DateKey、TimeKey Spot_Price、Discount_Value、...)

DimOrder、DimSpot、DimDiscount 等....

TotalPrice:=
SUMX(
    SUMMARIZE(
        Fact
        ,Fact[OrderKey]
        ,Fact[SpotKey]
        ,Fact[Spot_Price]
    )
    ,Fact[Spot_Price]
)

AverageDiscount:=
AVERAGE(Fact[Discount_Value])

事实 table 是非规范化的,您最终会得到最简单的星型模式。

第一个措施值得一些解释。 [Spot_Price] 对于具有多个折扣的任何点都是重复的,我们会用简单的 SUM() 得到错误的结果。 SUMMARIZE() 对传递给它的所有列进行分组,遵循关系(如有必要,我们在这里查看单个 table,因此无需遵循)。

SUMX() 迭代此 table 并在其第二个参数中累积表达式的值。 SUMMARIZE() 删除了重复的 [Spot_Price],因此我们将唯一的(根据 [OrderKey] 和 [SpotKey] 的唯一组合)累加到总和中。