Forum Discussion
Dax code help on Switch statement
- 2 years ago
hi suman1985
Measure 2 =VAR _Year = SELECTEDVALUE(TestTbl3[YEAR])VAR _Name = SELECTEDVALUE(TestTbl3[NAME])RETURNSWITCH( _Name,"SFDB", "","", CALCULATE( SUM(TestTbl3[AMOUNT]) , REMOVEFILTERS(TestTbl3), TestTbl3[YEAR] = _Year, TestTbl3[NAME] = "" || TestTbl3[NAME] = "SFDB"),SUM(TestTbl3[AMOUNT])) - Anonymous2 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.
- 2 years ago
Thank you so much it worked..Great help .
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.
- talespin2 years agoSolution 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())