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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Anonymous
Not applicable

Switch function with If statement

Hello community,  

I need your help with rewriting switch function with If statement. Below is my current code

 

Coke Goal = 
VAR _7D=MAX(table 1[DATE])-7
VAR _30D=MAX(table 1[DATE])-30
VAR _365D=MAX(table 1[DATE])-365
VAR _switch=
SWITCH(
    TRUE(),
    MAX('Table'[Value])="7D",7,
    MAX('Table'[Value])="30D",30,
   MAX('Table'[Value])="30D",365)

RETURN
    CALCULATE(
        SUM(Table 2[Coke_Goal]) * _switch
    )

 

 

What the code is doing is basically multiplying the "Coke goal"  values to 7, 30 and 365. 

 

I have table 3 which shows site number and how many days it has been opened for. 

SiteDays Open
135
2200
315
4400

 

What I want to do is for the 7 multiplication, if the site has been open more than 7 days, then multiply value by 7, for the 30, if the site is open more than 30 days, multiple value by 30 and for 365, multiple vfalue by 365 else multilple value by the days it has been open if any condition does not match. Please see belows post for your reference.

 

https://community.fabric.microsoft.com/t5/Desktop/Formatting-dashboard-with-multiple-filters/m-p/423...

I have been stuck here for couple of days so any help would be greatly appreciated.

 

Thank you.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous ,

 

Maybe you can try formula like below to create calculated column:

Coke Value =
SWITCH (
    TRUE (),
    'Table'[Days Open] < 365, 'Table'[Days Open] * 'Table'[Coke Goal],
    'Table'[Days Open] >= 365, 365 * 'Table'[Coke Goal]
)

vkongfanfmsft_0-1729221244108.png

 

Best Regards,
Adamk Kong

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @Anonymous ,

 

Maybe you can try formula like below to create calculated column:

Coke Value =
SWITCH (
    TRUE (),
    'Table'[Days Open] < 365, 'Table'[Days Open] * 'Table'[Coke Goal],
    'Table'[Days Open] >= 365, 365 * 'Table'[Coke Goal]
)

vkongfanfmsft_0-1729221244108.png

 

Best Regards,
Adamk Kong

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

@lbendlin  Just to clarify
"You will not be able to distinguish between "more than 365 days" and "else" 

SiteDays OpenCoke Goal
1351
22000.5
3152.5
44002

 

SiteCoke Value
1 
7d7 (7* goal)
30d30 (30 * goal)
365d35 ( Since days open is less than 365, I want the goal * days open)
4 
7d14
30d60
365d730( since the days open is over 365, I want 365 * the goal)

 

This is what I'm trying to acheive.

You lost me a bit here. Are you trying to apply multiple rules at the same time?

lbendlin
Super User
Super User

What I want to do is for the 7 multiplication, if the site has been open more than 7 days, then multiply value by 7, for the 30, if the site is open more than 30 days, multiple value by 30 and for 365, multiple vfalue by 365 else multilple value by the days it has been open if any condition does not match.

You will not be able to distinguish between "more than 365 days" and "else" .

 

You will need to provide another cutoff value.

 

You need to switch the order of execution.

 

Note that you defined variables but didn't use them

 

Coke Goal = 
var md = TODAY()-max('Table 1'[Date])
var _switch = switch(TRUE(),
md>730,md,
md>=365,365,
md>=30,30,
md>=7,7)
return calculate(sum('Table 2'[Coke_Goal]) * _switch)

 

Note that this will result in BLANK if the difference is less than 7.

Helpful resources

Announcements
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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.