Forum Discussion

suman1985's avatar
suman1985
Helper I
2 years ago
Solved

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.

 

 

I would really appreciate the help in advance.

 

 

Thanks 

  • 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])
    )
     

     

  • Anonymous's avatar
    Anonymous
    2 years ago

    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:

    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.

10 Replies

  • talespin's avatar
    talespin
    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])
    )
     

     

    • suman1985's avatar
      suman1985
      Helper I

      Thank you so much it worked..Great help .

       

    • h1629's avatar
      h1629
      Frequent Visitor

      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.

       

       

      • talespin's avatar
        talespin
        Solution Sage

        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]
        )
    • h1629's avatar
      h1629
      Frequent Visitor

      Thank you it worked for me. Great work.

  • Anonymous's avatar
    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:

    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.

    • suman1985's avatar
      suman1985
      Helper I

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

  • h1629's avatar
    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 

     

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

     

     

    • talespin's avatar
      talespin
      Solution Sage

      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())