DAX-高级产品分组/细分问题

时间:2018-11-29 19:33:49

标签: dax tabular

我使用AdventureWorksDW数据库创建了SSAS表格模型。

我使用以下帖子帮助我构建报告。

https://blog.gbrueckl.at/2014/02/applied-basket-analysis-in-power-pivot-using-dax/

Sold in same Order:=
CALCULATE (
COUNTROWS ( 'Internet Sales' ),
CALCULATETABLE ( 
SUMMARIZE ( 
'Internet Sales',
'Internet Sales'[SalesOrderNumber]
),
ALL ( 'Product' ) ,
USERELATIONSHIP( 'Internet Sales'[ProductKey],FilteredProduct[ProductKey])
)
)

我已经验证了公式的结果正确。 Touring Tire Tube售出了1,381张订单,并向我显示了与其他物品一起售出的订单数量(例如,在1,381张订单中,有170张还包含产品密钥214-Sport-100头盔,红色)。

enter image description here 这是我遇到的问题。我想总结一下我的数据,方法是显示多少订单中仅包含我的过滤商品与其他产品一起出售的订单。这必须是动态的,因为用户可以选择任何产品...最终结果应如下所示:

enter image description here

我是DAX的新手,已经为此苦了几个小时。感谢您的帮助。

这是表关系:

enter image description here

1 个答案:

答案 0 :(得分:1)

此DAX应该适用于我的博客中的示例数据集:

Orders with only the filtered products = 
--VAR vFilteredProducts = VALUES('Filtered Product'[ProductKey])
VAR vFilteredProducts = FILTER('Filtered Product', [ProductKey] = 530 || [ProductKey] = 541)
VAR vCountFilteredProducts = COUNTROWS(vFilteredProducts)
VAR vSales = CALCULATETABLE('Internet Sales',  -- get the Sales for the filtered Products
    vFilteredProducts,
    USERELATIONSHIP('Filtered Product'[ProductKey], 'Internet Sales'[ProductKey]), 
    ALL('Product'))
VAR vOrders = SUMMARIZE( -- Summarize the filtered product sales by Sales Order Number
    vSales, 
    [Sales Order Number],
    -- calucate the distinct filtered products in the filtered orders
    "CountFilteredProductsInOrder", CALCULATE(DISTINCTCOUNT('Internet Sales'[ProductKey])),
    -- calculate the all distinct products for the filtered orders 
    "CountTotalProductsInOrder", CALCULATE(DISTINCTCOUNT('Internet Sales'[ProductKey]), 
                ALLEXCEPT('Internet Sales', 'Internet Sales'[Sales Order Number]))
    )
RETURN COUNTROWS(
    FILTER(
        vOrders, 
        -- the total product count has to match the filtered product count --> no other products except filtered ones in order
        [CountFilteredProductsInOrder] = [CountTotalProductsInOrder]
    )
)

要获得除过滤产品以外的其他产品也已出售的订单,请将最后一个FILTER()从'='更改为'<'