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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
Help_me
Frequent Visitor

VAR and SWITCH Output issues

Hello All,

 

I have a table with called snaphot with the columns: date (date type), action (text values that can be A, B, X, Y), project_Num (text values such as H00789).

All of these values can repeat. I want to write DAX statement that reclassifies the action values as

A -> Cheese
B -> Crackers
X -> Water
Y -> Cookie 

BUT, if the date and the project_num are the same for A and B -> Cheese/Crackers

 

I have written the DAX statement below, but the output rewrites the Cheese/Crackers with conditions in lines 1 & 2. Not sure how to take into account for this.


New Action =

VAR CurrentProject = snapshot[project_Num]

VAR CurrentDate = snapshot[date]

 

VAR HasA =

    CALCULATE(

        COUNTROWS(snapshot),

        snapshot[project_Num] = CurrentProject,

        snapshot[date] = CurrentDate,

        snapshot[action] = "A"

    ) > 0

 

VAR HasB =

    CALCULATE(

        COUNTROWS(snapshot),

        snapshot[project_Num] = CurrentProject,

        snapshot[date] = CurrentDate,

        snapshot[action] = "B"

    ) > 0

 

RETURN

SWITCH(

    TRUE(),

   

    -- If both A and B exist on same date

    snapshot[action] IN {"A","B"} && HasA && HasB, "Cheese/Crackers",

   

    -- Individual mappings

    snapshot[action] = "A", "Cheese",

    snapshot[action] = "B", "Crackers",

    snapshot[action] = "X", "Water",

    snapshot[action] = "Y", "Cookie",

   

    BLANK()

)

1 ACCEPTED SOLUTION
mizan2390
Resolver III
Resolver III

Hi @Help_me 

The logic inside your SWITCH statement is actually perfectly structured. The reason your code is failing and skipping down to the individual mappings is due to a hidden DAX mechanic inside your HasA and HasB variables called Context Transition.

Because you are writing a Calculated Column, DAX evaluates the formula row by row (a Row Context). Whenever you use the CALCULATE function inside a Row Context, DAX performs a "context transition," which automatically converts the current row into a filter. This means CALCULATE silently applies a filter for every single column in that specific row. If the current row has an action of "B", DAX restricts the table to that specific row and then tries to apply your snapshot[action] = "A" filter. Because a row cannot be both "A" and "B" at the same time, COUNTROWS returns 0, meaning HasA evaluates to FALSE.

 

Try out this DAX

New Action = 
VAR CurrentProject = snapshot[project_Num]
VAR CurrentDate = snapshot[date]

VAR HasA =
    CALCULATE(
        COUNTROWS(snapshot),
        ALL(snapshot), -- Overrides context transition to scan the whole table
        snapshot[project_Num] = CurrentProject,
        snapshot[date] = CurrentDate,
        snapshot[action] = "A"
    ) > 0

VAR HasB =
    CALCULATE(
        COUNTROWS(snapshot),
        ALL(snapshot), -- Overrides context transition to scan the whole table
        snapshot[project_Num] = CurrentProject,
        snapshot[date] = CurrentDate,
        snapshot[action] = "B"
    ) > 0

RETURN
    SWITCH(
        TRUE(),
        
        -- If both A and B exist on same date for the same project
        snapshot[action] IN {"A","B"} && HasA && HasB, "Cheese/Crackers",
        
        -- Individual mappings
        snapshot[action] = "A", "Cheese",
        snapshot[action] = "B", "Crackers",
        snapshot[action] = "X", "Water",
        snapshot[action] = "Y", "Cookie",
        
        BLANK()
    )

mizan2390_0-1776925285157.png

If this solves your problem, please mark this as solved and give a kudos.
@meso that i don't lose this thread.

 

View solution in original post

4 REPLIES 4
FBergamaschi
Super User
Super User

Hi @Help_me 

you need to remove the filters injected by CALCULATE in these variables

 

VAR HasA =

    CALCULATE(

        COUNTROWS(snapshot),

        snapshot[project_Num] = CurrentProject,

        snapshot[date] = CurrentDate,

        snapshot[action] = "A",
       REMOVEFILTERS ( snapshot )

    ) > 0

 

VAR HasB =

    CALCULATE(

        COUNTROWS(snapshot),

        snapshot[project_Num] = CurrentProject,

        snapshot[date] = CurrentDate,

        snapshot[action] = "B",
       REMOVEFILTERS ( snapshot )

    ) > 0

 

rest of the code seems ok to me

 

If this helped, please consider giving kudos and mark as a solution

@me in replies or I'll lose your thread

Want to check your DAX skills? Answer my biweekly DAX challenges on the kubisco Linkedin page

Consider voting this Power BI idea

Francesco Bergamaschi

MBA, M.Eng, M.Econ, Professor of BI

mizan2390
Resolver III
Resolver III

Hi @Help_me 

The logic inside your SWITCH statement is actually perfectly structured. The reason your code is failing and skipping down to the individual mappings is due to a hidden DAX mechanic inside your HasA and HasB variables called Context Transition.

Because you are writing a Calculated Column, DAX evaluates the formula row by row (a Row Context). Whenever you use the CALCULATE function inside a Row Context, DAX performs a "context transition," which automatically converts the current row into a filter. This means CALCULATE silently applies a filter for every single column in that specific row. If the current row has an action of "B", DAX restricts the table to that specific row and then tries to apply your snapshot[action] = "A" filter. Because a row cannot be both "A" and "B" at the same time, COUNTROWS returns 0, meaning HasA evaluates to FALSE.

 

Try out this DAX

New Action = 
VAR CurrentProject = snapshot[project_Num]
VAR CurrentDate = snapshot[date]

VAR HasA =
    CALCULATE(
        COUNTROWS(snapshot),
        ALL(snapshot), -- Overrides context transition to scan the whole table
        snapshot[project_Num] = CurrentProject,
        snapshot[date] = CurrentDate,
        snapshot[action] = "A"
    ) > 0

VAR HasB =
    CALCULATE(
        COUNTROWS(snapshot),
        ALL(snapshot), -- Overrides context transition to scan the whole table
        snapshot[project_Num] = CurrentProject,
        snapshot[date] = CurrentDate,
        snapshot[action] = "B"
    ) > 0

RETURN
    SWITCH(
        TRUE(),
        
        -- If both A and B exist on same date for the same project
        snapshot[action] IN {"A","B"} && HasA && HasB, "Cheese/Crackers",
        
        -- Individual mappings
        snapshot[action] = "A", "Cheese",
        snapshot[action] = "B", "Crackers",
        snapshot[action] = "X", "Water",
        snapshot[action] = "Y", "Cookie",
        
        BLANK()
    )

mizan2390_0-1776925285157.png

If this solves your problem, please mark this as solved and give a kudos.
@meso that i don't lose this thread.

 

Zanqueta
Super User
Super User

Hi , when you use CALCULATE(), DAX performs a context transition and automatically applies the filter from the current row.
When you write:
DAXsnapshot[project_Num] = CurrentProject,snapshot[date] = CurrentDate,

snapshot[action] = "A"


Power BI is adding filters on top of the current row, including the action value of that same row.
For example, if the current row is "A", the HasB calculation is trying to check:

there is a row where
action = "A" (from the current row)
AND action = "B"

This is not possible, so it always returns FALSE.
As a result, the SWITCH applies the individual rules and replaces the expected "Cheese/Crackers" result.

 

Try this:

New Action =
VAR HasA =
    CALCULATE(
        COUNTROWS(snapshot),
        ALLEXCEPT(snapshot, snapshot[project_Num], snapshot[date]),
        snapshot[action] = "A"
    ) > 0

 

VAR HasB =
    CALCULATE(
        COUNTROWS(snapshot),
        ALLEXCEPT(snapshot, snapshot[project_Num], snapshot[date]),
        snapshot[action] = "B"
    ) > 0

 

RETURN
SWITCH(
    TRUE(),

 

    snapshot[action] IN {"A","B"} && HasA && HasB, "Cheese/Crackers",

 

    snapshot[action] = "A", "Cheese",
    snapshot[action] = "B", "Crackers",
    snapshot[action] = "X", "Water",
    snapshot[action] = "Y", "Cookie",

 

    BLANK()
)

 




 

 

@Help_me

If this response was helpful in any way, I’d gladly accept a kudo.
Please mark it as the correct solution. It helps other community members find their way faster.
Connect with me on LinkedIn

krishnakanth240
Resident Rockstar
Resident Rockstar

Hi @Help_me 

Issue is context transition inside CALCULATE. HasA and HasB are not evaluating within current row context so they can override intended condition. 

Can you try using ALLEXCEPT to fix filter context so the check is done only for the same project_Num and date.

New Action =

VAR HasA =CALCULATE(COUNTROWS(snapshot),ALLEXCEPT(snapshot, snapshot[project_Num], snapshot[date]),snapshot[action] = "A") > 0

 

VAR HasB = CALCULATE(COUNTROWS(snapshot), ALLEXCEPT(snapshot, snapshot[project_Num], snapshot[date]),snapshot[action] = "B") > 0

 

RETURN

SWITCH(TRUE(),snapshot[action] IN {"A","B"} && HasA && HasB, "Cheese/Crackers",snapshot[action] = "A", "Cheese",snapshot[action] = "B", "Crackers",snapshot[action] = "X", "Water",snapshot[action] = "Y", "Cookie", BLANK())

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.