Forum Discussion

pelowski's avatar
pelowski
Helper III
6 years ago
Solved

DAX Modeling/Granularity Problem

In our model we have a traditional order and order detail fact table relationship. However, some order detail line items in the fact table are really two different products sold together. Unfortunate...
  • DirkFrazier's avatar
    DirkFrazier
    6 years ago

    Walter and I refactored the solution and eliminated some confusion and redundancy in the code (the SWITCH() and IF() statements based on [Product and Bundle] values are essentially the same). Here's a cleaner version that has the same result: 

     

    Sales =
    IF (
        HASONEVALUE ( 'Products and Bundles'[Product and Bundle] ),
        --If only one [Product and Bundle] value has been selected in a slicer or filter, then
        SWITCH (
            TRUE (),
            --Show the specific sum of sales for either Product X +Bundle or sum the [Order Detail Sales Amount] for all other [Product and Bundle] values
            MAX ( 'Products and Bundles'[Product and Bundle] ) = "ProductA +Bundle", SUM ( Fct_OrderLines[Product A Bundle Sales Amount] ),
            MAX ( 'Products and Bundles'[Product and Bundle] ) = "ProductB +Bundle", SUM ( Fct_OrderLines[Product B Bundle Sales Amount] ),
            SUM ( 'Fct_OrderLines'[Order Detail Sales Amount] )
        ),
        IF (
            ISFILTERED ( 'Products and Bundles'[Product and Bundle] ),
            --If there are multiple [Product and Bundle] values selected in a slicer or filter, then create a temp table with SUMMARIZE()
            VAR ___ProductBundleTable =
                SUMMARIZE (
                    'Products and Bundles',
                    'Products and Bundles'[Product and Bundle],
                    --Based on the 'Products and Bundles' table, for every [Product and Bundle] value in the slicer/filter, add Columns:
                    "ProductBundle", FILTERS ( 'Products and Bundles'[Product and Bundle] ),
                    --"ProductBundle" column contains the filtered [Product and Bundle] values
                    "Amount", --"Amount" column contains the appropriate [Order Detail Sales Amount] based on the filtered [Product and Bundle] value
                    SWITCH (
                        TRUE (),
                        --Show the specific sum of sales for either Product X +Bundle or sum the [Order Detail Sales Amount] for all other [Product and Bundle] values
                        MAX ( 'Products and Bundles'[Product and Bundle] ) = "ProductA +Bundle", SUM ( Fct_OrderLines[Product A Bundle Sales Amount] ),
                        MAX ( 'Products and Bundles'[Product and Bundle] ) = "ProductB +Bundle", SUM ( Fct_OrderLines[Product B Bundle Sales Amount] ),
                        SUM ( 'Fct_OrderLines'[Order Detail Sales Amount] )
                    )
                ) --Calculate the Row Total by SUMX() across the temp table above.
            VAR ___TOTALAMOUNT =
                SUMX ( ___ProductBundleTable, [Amount] )
            RETURN
                ___TOTALAMOUNT,
            --Display the calculated Row Total when there are multiple [Product and Bundle] values in the slicer or filter.
            SUM ( 'Fct_OrderLines'[Order Detail Sales Amount] )
        )
    )