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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Tricky Switch and AND Function - combination

Hi Experts

 

I am trying to get the following out come based on the slicer selection criteria....

1. If the user selects Gross Margin (first slicer) and then Reported (second slicer) then return the Top 10 Products based on the Reported measure...

 

2. If the user selects Gross Margin (first slicer) and then NOE (second slicer) then return the Top 10 Products based on the NOE measure...

 

else

 

3. If the user selects Gross Margin (first slicer) and nothing on second slicer then return the Top 10 Products based on the Gross Margin measure.....

 

Cannot get the formula to work....

 

Top Products Reported:- Gross Margin / Net Sales = 
VAR RankingContext = VALUES('Product'[Category])
VAR ProfitbabilityMeasure = SELECTEDVALUE(ProfitabilityMeasures[MeasureName])
VAR ReportingMeasure = SELECTEDVALUE(Reporting[Reporting])
RETURN

CALCULATE(SWITCH(TRUE(),AND(
    ProfitbabilityMeasure = "Gross Margin",
    ReportingMeasure = "Reported"), [GMvPY1% (Reported PY)],
    TOPN(10, ALL('Product'[Category]),[GMvPY1% (Reported PY)]),
                        AND(
    ProfitbabilityMeasure = "Gross Margin",
    ReportingMeasure = "NOE"), [GMvPY3% (NOE @ PY Rate)],
    TOPN(10, ALL('Product'[Category]),[GMvPY3% (NOE @ PY Rate)]),
    
    ProfitbabilityMeasure = "Gross Margin",[GMvPY1% (Reported PY)],
    TOPN(10, ALL('Product'[Category]),[GMvPY1% (Reported PY)]),
    RankingContext,BLANK(),0)
   )
1 ACCEPTED SOLUTION
LaurentCouartou
Solution Supplier
Solution Supplier

SWITCH, just like IF, cannot return a table expression.

 

Have you tried something like this?

SWITCH(TRUE(),
    ProfitbabilityMeasure = "Gross Margin" && ReportingMeasure = "Reported"
       , CALCULATE( [GMvPY1% (Reported PY)]
            , TOPN(10, ALL('Product'[Category]),[GMvPY1% (Reported PY)])
)
, ...
)

 

View solution in original post

4 REPLIES 4
LaurentCouartou
Solution Supplier
Solution Supplier

SWITCH, just like IF, cannot return a table expression.

 

Have you tried something like this?

SWITCH(TRUE(),
    ProfitbabilityMeasure = "Gross Margin" && ReportingMeasure = "Reported"
       , CALCULATE( [GMvPY1% (Reported PY)]
            , TOPN(10, ALL('Product'[Category]),[GMvPY1% (Reported PY)])
)
, ...
)

 

Anonymous
Not applicable

Thanks for the excellent feedback,,,

 

I am guess we need to add RankingContext
 ))

 

to th eend of the statement...

Anonymous
Not applicable

got it to work....thanks

Anonymous
Not applicable

Hi this is my full measure....cannot see my error...

Top Products Reported:- Gross Margin / Net Sales = 
VAR RankingContext = VALUES('Product'[Category])
VAR ProfitbabilityMeasure = SELECTEDVALUE(ProfitabilityMeasures[MeasureName])
VAR ReportingMeasure = SELECTEDVALUE(Reporting[Reporting])
RETURN

SWITCH(TRUE(),
    ProfitbabilityMeasure = "Gross Margin" && ReportingMeasure = "Reported"
       , CALCULATE( [GMvPY1% (Reported PY)]
            , TOPN(10, ALL('Product'[Category]),[GMvPY1% (Reported PY)])),
            RankingContext,
            
    ProfitbabilityMeasure = "Gross Margin" && ReportingMeasure = "NOE"
       , CALCULATE( [GMvPY3% (NOE @ PY Rate)]
            , TOPN(10, ALL('Product'[Category]),[GMvPY3% (NOE @ PY Rate)]),
            RankingContext,
        
    ProfitbabilityMeasure = "Gross Margin" 
       , CALCULATE( [GMvPY1% (Reported PY)]
            , TOPN(10, ALL('Product'[Category]),[GMvPY1% (Reported PY)]),
            RankingContext)))

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.