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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Ilias_PAB
Regular Visitor

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
v-yangliu-msft
Community Support
Community Support

Hi  @Ilias_PAB ,

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
v-yangliu-msft
Community Support
Community Support

Hi  @Ilias_PAB ,

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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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