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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.