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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Lexi
Frequent Visitor

Nested if statements/Switch function?

Howdy! 

 

I need some direction from you nice guys 😊

 

I am new to Powerbi and I am trying to get to a solution to my issue. I have a dataset and I am assigning groups to our customers 

Customer 1001 = US Postal Code

Customer 1002 = Europe Postal Code

Customer 1003 = Asia Postal Code.

An issue appears when the customer moves or if they order for another customer. So I want to build in a function or create a custom column (that can be used in a filter). I am using the switch function for the normal customers but I need away to include the abnormal cases. 

1 ACCEPTED SOLUTION
smpa01
Community Champion
Community Champion

@Lexi  can you try this

Column =
VAR _home =
    SELECTCOLUMNS (
        SUMMARIZE (
            CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[CustID] ) ),
            'Table'[CustID],
            'Table'[Home]
        ),
        "cust", [CustID],
        "country", [Home]
    )
VAR _shipping =
    SELECTCOLUMNS (
        SUMMARIZE (
            CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[CustID] ) ),
            'Table'[CustID],
            'Table'[Shipping]
        ),
        "cust", [CustID],
        "country", [Shipping]
    )
VAR _intersect =
    INTERSECT ( _home, _shipping )
VAR _except =
    EXCEPT ( _shipping, _home )
RETURN
    IF (
        MAXX ( _except, [country] ) = BLANK (),
        MAXX ( _intersect, [country] ),
        MAXX ( _except, [country] )
    )

 

which generates this

smpa01_0-1641322213050.png

 


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

View solution in original post

8 REPLIES 8
Lexi
Frequent Visitor

Hi all, 

 

Thanks for all your contribution.

I tried out your formual but it aint working for me as I figured out my conditions are more complicated than I first intended. 

Lexi_0-1641838547842.png

 

 

I have one set of customers (150 in total) following one swich formual correct (VAR a), then about 5 customers that are the outliers and do not follow suit. I meantion two hard ones below (Var B, Var C)

 

Var a = switch ([customer],
               "Customer 1001" , "USA",
                "Customer 1002", "Asia", "Other  ...... and so on
Var b = if [Customer] = "2002" && [cat] <> "Household" THEN " USA" else "Europe"
Var c = if [Customer] = "2002" && [order date] > "2021, 10, 1" then "USA" else "Asia"

 

 

How the do I even try get this into a new custom column? Any direction will help! 

Lexi_1-1641838547844.png

 

smpa01
Community Champion
Community Champion

@Lexi  can you please create a new thread with sample data, expected output?


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================
smpa01
Community Champion
Community Champion

@Lexi  need more info, provide sample data and expected output.


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================
ValtteriN
Community Champion
Community Champion

Hi,

Unfortunately, your post is rather difficuilt to understand and it would be good if you provided more information. For now, the only help that I can give is to show some patterns/tips that I use in my models when I want to categorise customers in measures:

First typically I create a SWITCH + TRUE structure where it goes something like this:

SWITCH(TRUE(),
PLACE IF CONDITION HERE, "Category 1",
PLACE IF CONDITION HERE, "Category 2",
PLACE IF CONDITION HERE, "Category 2")

Then I create a table which has these states to use as a filter: 

Table = {("Category 1"),("Category 2"),("Category 3")}

Finally I create a filter measure to use in my visuals:
Customer category switch filter:= IF(
or(not(HASONEFILTER('Table'[Value])),[Customer category]=max('Table'[Value])),1,0)


I hope this helps to solve your issue and if it does consider accepting this as a solution and giving the post a thumbs up!





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Thanks folks!

I am trying to get at column D so I can use it later on as a filter in the main dashboard.

So I created this formal so far -

POSTAL CODE = Switch([CustomerID],
"Customer1001", "USA",
"Customer 1002", "EU", "Other"

This works for the customers that have no difference. I need to complete the column of the customers that dont conform. I want to try to complete a column with those easy customers that follow correctly and then the ones that dont, but I am unsure how to add the exceptions.

Ie: Customer 1001 if shipping address is europe then the postal code is eu otherwise usa

 

Lexi_0-1641320825278.png

 

smpa01
Community Champion
Community Champion

@Lexi  can you try this

Column =
VAR _home =
    SELECTCOLUMNS (
        SUMMARIZE (
            CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[CustID] ) ),
            'Table'[CustID],
            'Table'[Home]
        ),
        "cust", [CustID],
        "country", [Home]
    )
VAR _shipping =
    SELECTCOLUMNS (
        SUMMARIZE (
            CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[CustID] ) ),
            'Table'[CustID],
            'Table'[Shipping]
        ),
        "cust", [CustID],
        "country", [Shipping]
    )
VAR _intersect =
    INTERSECT ( _home, _shipping )
VAR _except =
    EXCEPT ( _shipping, _home )
RETURN
    IF (
        MAXX ( _except, [country] ) = BLANK (),
        MAXX ( _intersect, [country] ),
        MAXX ( _except, [country] )
    )

 

which generates this

smpa01_0-1641322213050.png

 


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================
smpa01
Community Champion
Community Champion

@Lexi  did you try this out yet?


========================
Did I answer your question? Mark my post as a solution!
Proud to be a Super User
My Custom Visualization Projects
• Plotting Live Sound: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================
Lexi
Frequent Visitor

Hi Smpa01, thanks you sir for helping me out!!! 

I tried out your formual but it aint working for me as I figured out my conditions are more complicated than I first intended. 😢

 

I have one set of customers (150 in total) following one swich formual correct (VAR a), then about 5 customers that are the outliers and do not follow suit. I meantion two hard ones below (Var B, Var C)

 

Var a = switch ([customer],
               "Customer 1001" , "USA",
                "Customer 1002", "Asia", "Other  ...... and so on
Var b = if [Customer] = "2002" && [cat] <> "Household" THEN " USA" else "Europe"
Var c = if [Customer] = "2002" && [order date] > "2021, 10, 1" then "USA" else "Asia"

 

 

How the do I even try get this into a new custom column? Any direction will help! 🤞

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

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.