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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
schoden
Post Partisan
Post Partisan

SWITCH function problem

Hi Community, 

 

I want to group customer based on years of transaction using SWITCH function.

For example customer A first transaction in 2010 , last transaction in 2020 , so years of transaction is 10.

Like wise for other customers. 

 

Date Diff(measure)

 

MAX('Table'[Year ])-MIN('Table'[Year ])

 

 

Loyalty(New Column)

 

Loyalty = SWITCH(TRUE(), 
[DateDiff]>=0 && [DateDiff]<=2, "BRONZE", 
[DateDiff]>=3 && [DateDiff]<=5, "SILVER",
[DateDiff]>=6 && [DateDiff]<=8, "GOLD")

 

 

But the column Loyalty when put in a slicer, it doesnt show all groups.

 

Please check the sample data.

 

https://netlinkgroup-my.sharepoint.com/:u:/g/personal/schoden_netlinkgroup_com_au/EXs9DuXKukVPvzVPZD...

 

 

Thank you in advance.

 

 

1 ACCEPTED SOLUTION
AllisonKennedy
Community Champion
Community Champion

You need to give it a row context to evaluate the DateDiff before you can do the switch, try doing the datediff as a column in the table: 

 

Years as Customer =
var customer = 'Table'[Customer ]
var firstyear = MINX(FILTER(ALL('Table'),'Table'[Customer ] = customer), 'Table'[Year ])
var lastyear = MAXX(FILTER(ALL('Table'),'Table'[Customer ] = customer), 'Table'[Year ])
RETURN lastyear-firstyear

 

 

Loyalty = SWITCH(TRUE(),
[Years as Customer]>=0 && [Years as Customer]<=2, "BRONZE",
[Years as Customer]>=3 && [Years as Customer]<=5, "SILVER",
[Years as Customer]>=6 && [Years as Customer]<=8, "GOLD")

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

2 REPLIES 2
schoden
Post Partisan
Post Partisan

@AllisonKennedy   Thank you so much for the solution. 

 

 

AllisonKennedy
Community Champion
Community Champion

You need to give it a row context to evaluate the DateDiff before you can do the switch, try doing the datediff as a column in the table: 

 

Years as Customer =
var customer = 'Table'[Customer ]
var firstyear = MINX(FILTER(ALL('Table'),'Table'[Customer ] = customer), 'Table'[Year ])
var lastyear = MAXX(FILTER(ALL('Table'),'Table'[Customer ] = customer), 'Table'[Year ])
RETURN lastyear-firstyear

 

 

Loyalty = SWITCH(TRUE(),
[Years as Customer]>=0 && [Years as Customer]<=2, "BRONZE",
[Years as Customer]>=3 && [Years as Customer]<=5, "SILVER",
[Years as Customer]>=6 && [Years as Customer]<=8, "GOLD")

Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

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.