Forum Discussion

Kopek's avatar
Kopek
Icon for Helper IV rankHelper IV
1 year ago

Variable changes the outcome

Hi!

 

I have built the following measure, and wanted to otimize it with variable, but ufortunately it does not work as expected.
When variable is used i am missing totals from the table, and the outcomes is different, looks like it misses the summarize step, can you help me in finding a mistake, please ?

Also - if we can add additional filter for Calendar[Today]= true would be great, i am having so many isuuses with meeting this requirement as it is true/false column and i ma live connected and do not have access to adding conditional column, changing model etc.

 

Measure =

CALCULATE(

    -1 * SUMX(

        FILTER(

            SUMMARIZE( Query, Query[Flag],

                Products[ID],

                "Total",

                IF(

                    [Current]

                        - [Demand] * 7

                        + [Average] * 7 >= 0,

                    0,

                    (

                        [Current]

                            - [Demand] * 7

                            + [Average] * 7

                    ) * AVERAGE(Table[Price])

                )

            ),

            [Total] <> BLANK()

        ),

        [Total]

    )

 

with variable:

 

 

MeasureVar =

VarCalc =  [Current]- [Demand] * + [Average] * 7

Return

CALCULATE(

    -1 * SUMX(

        FILTER(

            SUMMARIZE( Query, Query[Flag],

                Products[ID],

                "Total",

                IF(

                   Calc >= 0,

                    0,

                    ( Calc

                    ) * AVERAGE(Table[Price])

                )

            ),

            [Total] <> BLANK()

        ),

        [Total]

    )

4 Replies

  • Hi Kopek ,

     

    The issue with your optimized measure using variables is likely related to how Power BI handles evaluation contexts and the SUMMARIZE function. When using variables inside SUMMARIZE, their context can become detached from the rows you’re trying to calculate totals for. Shown below is a solution that optimizes the logic while addressing the missing totals and adding the additional filter you mentioned.

     

    MeasureVar = 
    VAR Calc = [Current] - [Demand] * 7 + [Average] * 7
    VAR PriceAvg = AVERAGE(Table[Price])
    
    RETURN
    CALCULATE(
        -1 * SUMX(
            FILTER(
                SUMMARIZE(
                    Query, 
                    Query[Flag], 
                    Products[ID], 
                    "Total", 
                    IF(
                        Calc >= 0, 
                        0, 
                        Calc * PriceAvg
                    )
                ), 
                [Total] <> BLANK()
            ), 
            [Total]
        ),
        Calendar[Today] = TRUE
    )
    

    By calculating variables outside SUMMARIZE, we ensure the correct context is passed. Additionally, SUMMARIZE now only focuses on grouping and returning the necessary values, while CALCULATE manages the filtering logic effectively.

     

    Best regards,

     

  • Hi DataNinja777 , thanks for trying to help, but unfortunately your solution makes the same error as mine -no total in the table and different outcomes per ID than original measure.