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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Can someone figure out what is wrong with my code?

Can someone figure out what is wrong with my code?
Purpose of the code:
If the column Brand is filtered and SKU is not --> value sales of 2022 of the whole brand with all SKUs
If  the column SKU is filtered  --> value sales of 2022 of a specific SKU
If no filters --> value sales of 2022 of all the brands & all the SKUs

Total Sales
2022 III =
VAR SelectedBrand = SELECTEDVALUE('DATA'[Brand])
VAR SelectedSKU = SELECTEDVALUE('DATA'[SKU])
RETURN
CALCULATE(
    IF(
        ISFILTERED('DATA'[Brand]) && ISFILTERED('DATA'[SKU]),
        SUM('DATA'[Sales (€)])),
        IF(
            ISFILTERED('DATA'[Brand]),
            CALCULATE(
                SUM('DATA'[Sales (€)])),
                FILTER(
                    ALL('DATA'),
                    YEAR('DATA'[Time period]) = 2022 &&
                    'DATA'[Brand] = SelectedBrand
                )
            ),
            IF(
                ISFILTERED('DATA'[SKU]),
                CALCULATE(
                    SUM('DATA'[Sales (€)])),
                    FILTER(
                        ALL('DATA'),
                        YEAR('DATA'[Time period]) = 2022 &&
                        'DATA'[SKU] = SelectedSKU
                    )
                ),
                CALCULATE(
                    SUM(DATA[Sales (€)])),
                    FILTER(
                        ALL('DATA'),
                        YEAR('DATA'[Time period]) = 2022
                    )
                )
           
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi  @Anonymous ,

I created some data:

vyangliumsft_0-1711597303951.png

You can use the following dax:

Total Sales 2022 III =
VAR SelectedBrand = SELECTEDVALUE('DATA'[Brand])
VAR SelectedSKU = SELECTEDVALUE('DATA'[SKU])
return
IF(
    ISFILTERED('DATA'[Brand])&&NOT(ISFILTERED('DATA'[SKU])),
    SUMX(
        FILTER(ALL('DATA'),
        'DATA'[Brand]=SelectedBrand &&'DATA'[Time period]=2022),[Sales (€)])
    ,
IF(
    ISFILTERED('DATA'[SKU]),
      SUMX(
       FILTER(ALL('DATA'),
      'DATA'[Time period]=2022&&'DATA'[SKU]=SelectedSKU),[Sales (€)])
    ,
IF(
    NOT(ISFILTERED('DATA'[Brand]))&&NOT(ISFILTERED('DATA'[SKU])),
         SUMX(
        FILTER(ALL('DATA'),
     'DATA'[Time period]=2022),[Sales (€)])
)))

vyangliumsft_1-1711597303952.png

 

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi  @Anonymous ,

I created some data:

vyangliumsft_0-1711597303951.png

You can use the following dax:

Total Sales 2022 III =
VAR SelectedBrand = SELECTEDVALUE('DATA'[Brand])
VAR SelectedSKU = SELECTEDVALUE('DATA'[SKU])
return
IF(
    ISFILTERED('DATA'[Brand])&&NOT(ISFILTERED('DATA'[SKU])),
    SUMX(
        FILTER(ALL('DATA'),
        'DATA'[Brand]=SelectedBrand &&'DATA'[Time period]=2022),[Sales (€)])
    ,
IF(
    ISFILTERED('DATA'[SKU]),
      SUMX(
       FILTER(ALL('DATA'),
      'DATA'[Time period]=2022&&'DATA'[SKU]=SelectedSKU),[Sales (€)])
    ,
IF(
    NOT(ISFILTERED('DATA'[Brand]))&&NOT(ISFILTERED('DATA'[SKU])),
         SUMX(
        FILTER(ALL('DATA'),
     'DATA'[Time period]=2022),[Sales (€)])
)))

vyangliumsft_1-1711597303952.png

 

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.