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
harib
Post Patron
Post Patron

How to create switch statement in columns

Hi

I would like to create below switch statement using column. I have tried, but it's not showing the values. If it's possible please let me know

 

SWITCH(
TRUE(),
SELECTEDVALUE(Columns_names[Query1])="A",FIRSTNONBLANK(Orders[A],1),
SELECTEDVALUE(Columns_names[Query1])="B",FIRSTNONBLANK(Orders[B],1),
SELECTEDVALUE(Columns_names[Query1])="C",FIRSTNONBLANK(Orders[C],1),
"Nothing")

 

Thanks in advance

5 REPLIES 5
Greg_Deckler
Super User
Super User

I have a suspicion that you are likely not using SELECTEDVALUE correctly but that is really hard to say without context. If you are trying to do this in a calculated column for example, I would not use SELECTEDVALUE in that case, I would just use the column name. Also, using SWITCH TRUE here technically does not make sense, just use the standard form of SWITCH. So you might try:

 

 

Column = 
  SWITCH(
    [Query1],
    "A",FIRSTNONBLANK(Orders[A],1),
    "B",FIRSTNONBLANK(Orders[A],1),
    "C",FIRSTNONBLANK(Orders[A],1),
    "Nothing"
  )

 

 

If this is a measure, I would try this:

 

 

Measure = 
  SWITCH(
    MAX('Columns_names'[Query1]),
    "A",FIRSTNONBLANK(Orders[A],1),
    "B",FIRSTNONBLANK(Orders[A],1),
    "C",FIRSTNONBLANK(Orders[A],1),
    "Nothing"
  )

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@amitchandak 

@Greg_Deckler 

 

From the below Actual output if we select any value from Input_Groups, it's reflecting the same in the table visual, let's say if i select All, it will appeare in the table , same other values also.

6.PNG

 

But my requirement is if i dont select any values from Input_Groups it has to come like below output with aggerated values else by default it has to display "ALL".  With All we can see aggregated values. 

 

Usevally what i was doing, by default selecting the "All" values and deploying into the services. But if user remove the filter from the Input_Groups it will give all detailes values and aggreageted values will split . 

7.PNG

Sample File: https://drive.google.com/file/d/1I4iiRDKf-H9vQ0vU4BTtGpn-Ey0YeiT/view?usp=sharing

 

If it's possible please let me know. 

Thanks in advance

After looking at your PBIX it strikes me that you might possibly want to unpivot your three columns to obtain a single column and then things will be easier. I would try this, but I do not have your Sample.xlsx file so I cannot test this out. But, I'm pretty sure that unpivoting would pretty much make this a cake walk.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

 

I have attached xlsx file. could u pleaase check and lt me know the solutions. 

 

Sample PBIX : https://drive.google.com/file/d/1I4iiRDKf-H9vQ0vU4BTtGp-Ey0YehiT/view?usp=sharing

 

Sample.xlsx file : https://drive.google.com/file/d/1brZTsnfqx0mTryREYkTrb5PreaC5mO8a/view?usp=sharing

 

Regards,

Hari

amitchandak
Super User
Super User

When you this calculation in measure context is very important. Can you share sample data and sample output.

 

In between try

var _max = maxx(Columns_names,SELECTEDVALUE(Columns_names[Query1]))
SWITCH(
TRUE(),
_max="A",FIRSTNONBLANK(Orders[A],1),
_max="B",FIRSTNONBLANK(Orders[B],1),
_max="C",FIRSTNONBLANK(Orders[C],1),
"Nothing")

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.