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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Trying to create a measure using switch

Hello,

 

I'm trying to get my head around using the SWITCH function in Power BI. However it's not working out for me so wanted to ask the community for some help.

 

I currently have it working in PowerQuery with a custom column with the below if, then, else statement but would like to know how to get it to work as a measure and if it would be a good use case for the SWITCH function.

if[SalesGroup] = "DISTRIBUTORS"
then ([#"Gross Sales after disc."] / 100) * 0.3
else if[SalesGroup] = "BENELUX"
then ([#"Gross Sales after disc."] / 100) * 1
else if[SalesGroup] = "FRANCE"
then ([#"Gross Sales after disc."] / 100) * 1.2
else if[SalesGroup] = "GERMANY"
then ([#"Gross Sales after disc."] / 100) * 1
else if[SalesGroup] = "IBERIA"
then ([#"Gross Sales after disc."] / 100) * 0.9
else if[SalesGroup] = "NORDIC"
then ([#"Gross Sales after disc."] / 100) * 0.6
else if[SalesGroup] = "UK"
then ([#"Gross Sales after disc."] / 100) * 0.7
else if[SalesGroup] = "DIGITAL"
then ([#"Gross Sales after disc."] / 100) * 0.7
else ([#"Gross Sales after disc."] / 100) * 10.7

 Thank you!

1 ACCEPTED SOLUTION
az38
Community Champion
Community Champion

@Anonymous 

create column, not a measure.

make sure you have relationships between tables


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

View solution in original post

7 REPLIES 7
harshnathani
Community Champion
Community Champion

Hi @Anonymous ,

 

Measure =
VAR __SalesGroup = Table[SalesGroup]


return
SWITCH(

TRUE(),
(__SaleGroup = "DISTRIBUTORS" ), ([#"Gross Sales after disc."] / 100) * 0.3),
(__SaleGroup = "BENELUX" )|| (__SaleGroup = "GERMANY") ,([#"Gross Sales after disc."] / 100) * 1),
__SaleGroup = "FRANCE", [#"Gross Sales after disc."] / 100) * 1.2,
__SaleGroup = "IBERIA", [#"Gross Sales after disc."] / 100) * 0.9,
__SaleGroup = "NORDIC", [#"Gross Sales after disc."] / 100) * 0.6,

__SaleGroup = "UK" || __SaleGroup = "DIGITAL, [#"Gross Sales after disc."] / 100) * 0.7,
[#"Gross Sales after disc."] / 100) * 10.7
)

 

Regards,

Harsh Nathani

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

Hi @Anonymous ,

 

You can use RELATED Function

 

Measure =
VAR __SalesGroup = RELATED(Table[SalesGroup])


return
SWITCH(

TRUE(),
(__SaleGroup = "DISTRIBUTORS" ), ([#"Gross Sales after disc."] / 100) * 0.3),
(__SaleGroup = "BENELUX" )|| (__SaleGroup = "GERMANY") ,([#"Gross Sales after disc."] / 100) * 1),
__SaleGroup = "FRANCE", [#"Gross Sales after disc."] / 100) * 1.2,
__SaleGroup = "IBERIA", [#"Gross Sales after disc."] / 100) * 0.9,
__SaleGroup = "NORDIC", [#"Gross Sales after disc."] / 100) * 0.6,

__SaleGroup = "UK" || __SaleGroup = "DIGITAL, [#"Gross Sales after disc."] / 100) * 0.7,
[#"Gross Sales after disc."] / 100) * 10.7
)

 

If this does not work, please share some sample data and the relationship between the tables.

 

Regards,

Harsh Nathani

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

Anonymous
Not applicable

Hi and thanks for your help,

 

However it appears that the RELATED parameter is not the correct type.

 

04-05-2020 13-03-29.png

 

Thanks

az38
Community Champion
Community Champion

@Anonymous 

create column, not a measure.

make sure you have relationships between tables


do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
az38
Community Champion
Community Champion

Hi @Anonymous 

you don't need to create a measure, in DAX you can create a calculated column. SWITCH() is a good solution

Column = 
SWITCH([SalesGroup],
"DISTRIBUTORS",([#"Gross Sales after disc."] / 100) * 0.3,
"BENELUX", ([#"Gross Sales after disc."] / 100) * 1,
"FRANCE", ([#"Gross Sales after disc."] / 100) * 1.2,
"GERMANY", ([#"Gross Sales after disc."] / 100) * 1,
"IBERIA", ([#"Gross Sales after disc."] / 100) * 0.9,
"NORDIC", ([#"Gross Sales after disc."] / 100) * 0.6,
"UK", ([#"Gross Sales after disc."] / 100) * 0.7,
"DIGITAL", ([#"Gross Sales after disc."] / 100) * 0.7,
([#"Gross Sales after disc."] / 100) * 10.7
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
amitchandak
Super User
Super User

@Anonymous, Assuming Switch in DAX

switch (true(),
[SalesGroup] = "DISTRIBUTORS", ([#"Gross Sales after disc."] / 100) * 0.3 ,
[SalesGroup] = "BENELUX", ([#"Gross Sales after disc."] / 100) * 1 ,
[SalesGroup] = "FRANCE" , ([#"Gross Sales after disc."] / 100) * 1.2 ,
///Other condition .....................
([#"Gross Sales after disc."] / 100) * 10.7
)

Anonymous
Not applicable

Hi,

 

I forgot to mention that the SalesGroup data exists in a different table (SalesGroup) in the model.

 

Can it still be achieved using SWITCH?

 

Thank you.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.