updating measure calculation to apply OR logic in DAX

扶醉桌前 提交于 2019-12-25 02:55:36

问题


I'm trying to have my dimensions filter a calculation using the OR logic. I have 3 tables: 1 Fact and 2 Dimensions.

 SalesFact: SalesID, ProductID, StoreID
 ProductDim: ProductID, ProductName
 StoreDim: StoreID, StoreName

This is the measure:

       Total SalesId =    
             CALCULATE(DISTINCTCOUNT(SalesFact[SalesID])          
       ) 

This works good, but I want it to filter on either dimension selected, something like this:

EVALUATE
    SUMMARIZECOLUMNS (
    FILTER(
            SalesFact,
            SalesFact[ProductId]=15257 
            || SalesFacts[StoreId]=19732
            || SalesFacts[StoreId]=19738
            ),      
    "Total Sale IDs", [Total SalesId]

)

How can I put this directly into the measure definition so when ProductA is selected and StoreB, I get the total count of SaleIds with either ProductA or StoreB?

Thank you!


回答1:


First, create a measure for the number of records in the sales table:

Sale Count = COUNTRIOWS(SalesFact)

Since all records in the sales table have unique Ids, it gives the same result as counting distinct IDs, but faster and simpler.

Second, to filter on multiple dimensions with OR condition, you need to create a cross-join (all possible combinations) of the values you plan to use in the filters:

EVALUATE
SUMMARIZECOLUMNS('Product'[Product_ID], 'Store'[Store_ID])

The above formula will give you all possible combinations of product_ID and Store_ID. However, we only need those combinations that exist in Sales Fact table. This can be accomplished by adding sales table as a filter:

EVALUATE
SUMMARIZECOLUMNS('Product'[Product_ID], 'Store'[Store_ID], 'SalesFact')

Finally, we need only those products and stores that you want to see. We accomplish this by wrapping the formula in FILTER function:

EVALUATE
FILTER(
   SUMMARIZECOLUMNS('Product'[Product_ID], 'Store'[Store_ID], 'SalesFact'),
     Product[Product_ID] IN {15257} || 
     Store[Store_ID] IN {19732, 19738}
)

This formula takes our Product-Store combinations and filters them by the desired products OR stores. You can now use this formula in measures to re-calculate sale count for the selected products or stores:

Selected Product or Store Sales Count = 
VAR 
   Selected_Combinations = 
   FILTER(
      SUMMARIZECOLUMNS('Product'[Product_ID], 'Store'[Store_ID], 'SalesFact'),
         Product[Product_ID] IN {15257} || 
         Store[Store_ID] IN {19732, 19738}
)
RETURN 
   CALCULATE( [Sale Count], Selected_Combinations)

Edit:

If instead of specific product and store IDs you plan to use visuals such as slicers as inputs, you will need to modify the approach. Let's say, you have 2 slicers:

  • ProductName
  • StoreName

Normally, slicers work using AND logic, but you want to count sales for the selected product names OR selected store names. Modify the previous formula as follows:

Selected Product or Store Sales Count =
VAR Selected_Combinations =
    UNION (
        CROSSJOIN ( VALUES ( 'Product'[ProductName] ), ALL ( Store[StoreName] ) ),
        CROSSJOIN ( ALL ( 'Product'[ProductName] ), VALUES ( Store[StoreName] ) )
    )
RETURN
    CALCULATE ( [Sale Count], Selected_Combinations )

There are other methods available; you can learn more here:
Using OR conditions between slicers in DAX



来源:https://stackoverflow.com/questions/55544566/updating-measure-calculation-to-apply-or-logic-in-dax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!