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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
suman1985
Helper I
Helper I

Dax code help on Switch statement

Hi All,

 

I am new to DAX, can you please verify my dax .I am trying to create measure which should group by year, and make SFDB value to null and then add sfdb value to Null value 

 

DAX:

Measure =
switch(
                  SELECTEDVALUE(SOURCECODE[NAME])
                  ,BLANK(),
                  CALCULATE(sum(SOURCECODE[AMOUNT]),filter(allselected(SOURCECODE),SOURCECODE[NAME] in {"SFDB",BLANK()}))
                )
 
The above dax is not working when year is added to the table (i.e., not grouping by year)other wise it is working fine. Please help.

 

suman1985_0-1706795145694.png

suman1985_1-1706795805594.png

 

I would really appreciate the help in advance.

 

 

Thanks 

3 ACCEPTED SOLUTIONS
talespin
Solution Sage
Solution Sage

hi @suman1985 

 

Measure 2 =
VAR _Year = SELECTEDVALUE(TestTbl3[YEAR])
VAR _Name = SELECTEDVALUE(TestTbl3[NAME])
RETURN
SWITCH( _Name,
        "SFDB", "",
        "", CALCULATE( SUM(TestTbl3[AMOUNT]) , REMOVEFILTERS(TestTbl3), TestTbl3[YEAR] = _Year, TestTbl3[NAME] = "" || TestTbl3[NAME] = "SFDB"),
        SUM(TestTbl3[AMOUNT])
)
 

talespin_0-1706812908633.png

 

View solution in original post

Anonymous
Not applicable

Hi @suman1985 ,

@talespin  provides a good solution for you, while you can also try to follow my steps below:
Add two measures:

1&0 = 
IF (
    SELECTEDVALUE ( 'Table2'[NAME] ) = ""
        || SELECTEDVALUE ( 'Table2'[NAME] ) = "SFDB",
    1,
    0
)
Measure_Expected Result = 
VAR _1 =
    CALCULATE (
        SUM ( 'Table2'[AMOUNT] ),
        FILTER (
            ALL ( 'Table2' ),
            'Table2'[YEAR] = SELECTEDVALUE ( 'Table2'[YEAR] )
                && 'Table2'[1&0] = 1
        )
    )
VAR _2 =
    IF (
        SELECTEDVALUE ( 'Table2'[NAME] ) <> "",
        SELECTEDVALUE ( 'Table2'[AMOUNT] ),
        _1
    )
RETURN
    IF ( SELECTEDVALUE ( 'Table2'[NAME] ) = "SFDB", "", _2 )

Final output:

vyifanwmsft_0-1707122333383.png

How to Get Your Question Answered Quickly - Microsoft Fabric Community

If it does not help, please provide more details with your desired out put and pbix file without privacy information.

 

Best Regards,

Ada Wang

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

Thank you so much it worked..Great help .

 

View solution in original post

10 REPLIES 10
h1629
Frequent Visitor

Hi,

 

In my scenario the year and year-mon columns are coming from different table.I used the same measure which you have provided but it is not giving correct values 

The table schema looks like 

h1629_2-1707222786151.png

 

and the table structure and the result which we need for measure is highlighted in Yellow.

h1629_1-1707222696562.png

 

 

hi @h1629 ,

 

Please use this measure, it will SUM at either Year or Year-Month. Please note that the data I created doesn't have N/A for all months, so that is why you are not seeing N/A for certain months.

 

MResult =
VAR _SelVal = SELECTEDVALUE(DimMaterial[MaterialCode])
VAR _Sales = CALCULATE(SUM(FactTable[Sales]), FactTable[IsRelevant] = "Y", FactTable[IndexRange] > 2)

VAR _Val =
SWITCH(_SelVal,
"Soft", _Sales,
"Hard", _Sales,
"Raw", 0,
"N/A", CALCULATE( SUM(FactTable[Sales]) , (DimMaterial[MaterialCode] = "N/A" || DimMaterial[MaterialCode] = "Raw"), FactTable[IsRelevant] = "Y", FactTable[IndexRange] > 2 ),
BLANK()
)

RETURN IF( _Sales > 0, _Val, BLANK())

 

talespin_1-1707368518377.png

 

Anonymous
Not applicable

Hi @suman1985 ,

@talespin  provides a good solution for you, while you can also try to follow my steps below:
Add two measures:

1&0 = 
IF (
    SELECTEDVALUE ( 'Table2'[NAME] ) = ""
        || SELECTEDVALUE ( 'Table2'[NAME] ) = "SFDB",
    1,
    0
)
Measure_Expected Result = 
VAR _1 =
    CALCULATE (
        SUM ( 'Table2'[AMOUNT] ),
        FILTER (
            ALL ( 'Table2' ),
            'Table2'[YEAR] = SELECTEDVALUE ( 'Table2'[YEAR] )
                && 'Table2'[1&0] = 1
        )
    )
VAR _2 =
    IF (
        SELECTEDVALUE ( 'Table2'[NAME] ) <> "",
        SELECTEDVALUE ( 'Table2'[AMOUNT] ),
        _1
    )
RETURN
    IF ( SELECTEDVALUE ( 'Table2'[NAME] ) = "SFDB", "", _2 )

Final output:

vyifanwmsft_0-1707122333383.png

How to Get Your Question Answered Quickly - Microsoft Fabric Community

If it does not help, please provide more details with your desired out put and pbix file without privacy information.

 

Best Regards,

Ada Wang

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

Even this solution worked for me. Appreciate the help..

talespin
Solution Sage
Solution Sage

hi @suman1985 

 

Measure 2 =
VAR _Year = SELECTEDVALUE(TestTbl3[YEAR])
VAR _Name = SELECTEDVALUE(TestTbl3[NAME])
RETURN
SWITCH( _Name,
        "SFDB", "",
        "", CALCULATE( SUM(TestTbl3[AMOUNT]) , REMOVEFILTERS(TestTbl3), TestTbl3[YEAR] = _Year, TestTbl3[NAME] = "" || TestTbl3[NAME] = "SFDB"),
        SUM(TestTbl3[AMOUNT])
)
 

talespin_0-1706812908633.png

 

Thank you it worked for me. Great work.

hi @h1629 

 

You're welcome.

I have a similar case where I am creating measure for Power BI Data model.  Where I need to filter the fact tables and dim tables in the measure. This measure should work if end users wants to see year in the table or year month.

h1629_1-1707158231493.png

 

 

hi @h1629 ,

 

You can use this, replace with your table name and column names.

 

MSales =
VAR _Name = SELECTEDVALUE(DimProductCategory[EnglishProductCategoryName])
RETURN
SWITCH( _Name,
        "Bikes", 0,
        "Accessories",
                        CALCULATE(
                                    [MSalesAmount] ,
                                    REMOVEFILTERS(DimProductCategory[EnglishProductCategoryName]),
                                    DimProductCategory[EnglishProductCategoryName] = "Bikes" || DimProductCategory[EnglishProductCategoryName] = "Accessories",
                                    Isrelevant = "Y", Indexragge > 2
                                    ),
        [MSalesAmount]
)

Thank you so much it worked..Great help .

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.