Forum Discussion

Pfoster's avatar
Pfoster
Resolver I
8 months ago
Solved

Performance Issues with Formula

Hello, I have made a report where Sales Managers can see their Net Turnover in comparison to an other Scenario. This Delta is split into FX-, Price-, Volume- und Mix-Effects. With FX- and Mix-Effec...
  • danextian's avatar
    8 months ago

    Hi Pfoster 

    For the first measure, filter the table first before summarizing to reduce the number of rows. The measures in the ADDCOLUMNS don't need calculate as itis implicit if a measure is referenced.

    Price Effect =
    VAR FilteredSales =
        FILTER (
            'Sales Data',
            NOT ( 'Sales Data'[Company Code] IN { "PF3091", "PF3HGS", "PF3262", "PF3267" } ) &&
            'Sales Data'[Material] <> "100222"
        )
    
    VAR Grain =
        SUMMARIZE (
            FilteredSales,
            'Sales Data'[Company Code],
            'Sales Data'[Material],
            'Sales Data'[Posting period],
            'Sales Data'[Ship To],
            'Sales Data'[Sold To],
            'Sales Data'[Country Code of ship to],
            FocusScenario[Focus Scenario] -- this works if FocusScenario is a related table on the one side of a relationship with Sales Data on the many side.
        )
    
    VAR Base =
        ADDCOLUMNS (
            Grain,
            "NSPComp",  [NSPComp_LC],
            "NSPFocus", [NSPFocus_LC],
            "FX",       [FX_Rate_CompPeriod],
            "FocusMT",  [FocusMT]
        )
    
    RETURN
    SUMX (
        FILTER (
            Base,
            [NSPComp] > 0 &&
            [NSPFocus] > 0
        ),
        ( [NSPFocus] - [NSPComp] ) * [FX] * [FocusMT] * 1000
    )
    

     

    For the second measure, try this:

    VolEffect =
    VAR BaseTable =
        ADDCOLUMNS (
            SUMMARIZECOLUMNS (
                'Sales Data'[Company Code],
                'Sales Data'[Material],
                'Sales Data'[Posting period],
                'Sales Data'[Ship To],
                'Sales Data'[Sold To],
                'Sales Data'[Country Code of ship to],
                FocusScenario[Focus Scenario]
            ),
            "__RowVolEffect",
                VAR Focus    = [FocusMT]
                VAR CompCalc = [CompMT] + 0
                VAR BothZero =
                    ( ISBLANK(Focus) || Focus = 0 ) &&
                    CompCalc = 0
                RETURN
                    IF (
                        NOT BothZero,
                        IF (
                            CompCalc = 0,
                            ( Focus - CompCalc ) * [NSPFocus] * 1000,
                            ( Focus - CompCalc ) * [NSPComp] * 1000
                        )
                    )
        )
    RETURN
    SUMX(BaseTable, [__RowVolEffect])
    

    Note: DAX optimization isn’t a one-size-fits-all solution and usually requires testing. If this approach doesn’t achieve the desired performance, consider pre-computing or aggregating the results in a calculated table. While this may increase memory usage, it can help the visual render more quickly.