Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
HamidRezaSajjad
Frequent Visitor

Optimized DAX code for calculating on different Filter Context

Hello. I've encountered a Sencario in Which I have to calculate each Value in different FC(Filter Context) based on predefiend Scenarios of A and B.

here is my Data Structure in Excel.

DAX.jpg

 

in above image, [OldCost] and [NewCost] are two Calculated Columns and are the result of [Last Purchase Rate]*[Standard Consuption Rate] and A and B are two Measures calculated by DAX by the following Code snippet for B:

=
VAR test10 =
    IF(
        MAX( BothBOM[ItemCode] ) <> "0501-2020",
        0,
        SUMX(
            SUMMARIZE(
                FILTER(
                    ALLEXCEPT( BothBOM, BothBOM[ProductCode] ),
                    BothBOM[ItemCode] = "0501-2020"
                        || BothBOM[ItemCode] = "0501-2014"
                ),
                "total",
                    CALCULATE(
                        SUMX( BothBOM, BothBOM[NewCost] ),
                        BothBOM[ItemCode] = "0501-2020"
                    )
                        - CALCULATE(
                            SUMX( BothBOM, BothBOM[OldCost] ),
                            BothBOM[ItemCode] = "0501-2014"
                        )
            ),
            [total]
        )
    )
VAR test9 =
    CALCULATE(
        SUMX( BothBOM, BothBOM[OldCost] ),
        KEEPFILTERS( LEFT( BothBOM[ItemCode], 4 ) = "0502" )
    )
VAR test11 =
    SUMX(
        SUMMARIZE(
            FILTER(
                ALLEXCEPT( BothBOM, BothBOM[ProductCode] ),
                BothBOM[ItemCode] = "0501-2020"
                    || BothBOM[ItemCode] = "0501-2014"
            ),
            "total2",
                CALCULATE(
                    SUMX( BothBOM, BothBOM[NewCost] ),
                    BothBOM[ItemCode] = "0501-2020"
                )
                    - CALCULATE(
                        SUMX( BothBOM, BothBOM[OldCost] ),
                        BothBOM[ItemCode] = "0501-2014"
                    )
        ),
        [total2]
    )
RETURN
    IF( NOT ( HASONEVALUE( BothBOM[ItemCode] ) ), test11 + test9, test9 + test10 )

 

in summary, in senario B (VAR=test10), I have to  calculate  for ([ItemCode]="0502-2020") as :

([ItemCode]="0502-2020") minus ([ItemCode]="0501-2014") e.i (433.000-146.000=287.000)

 and VAR=test11 stands for calculating subtotals in different context.

the outcome of these DAX is as expected, but I am wondering if there might be a better solution and better DAX coding?Additionally , I'm curious about the efficeny of this code. specially for VAR=test11 by which I've handeled subtotals for the Measure.

1 REPLY 1
HamidRezaSajjad
Frequent Visitor

Any Idea?

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors