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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
YunJ
Post Prodigy
Post Prodigy

How to write measure for specific condition for slicer?

Hi, 

I have a text column called "Year_bd", and I used it as slicer. Then I wrote a measure called GMV_LY, which means for example when Year_bd=2021 is selected, it will show sum(GMV) for 2020 (which is 2021-1).

 

But it shows cannot display the visual when I add it.

1.png2.png

1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

Hi @YunJ 

 

I see the problem...no need to convert the above one, but the below one, you can use either way, convert or the way provided by @amitchandak 

GMV_LY = 

VAR a = SELECTEDVALUE(Sheet1[year_bd])-1
RETURN
CALCULATE(SUM(Sheet1[GMV],Sheet1[year_bd]*1=a)

OR...

GMV_LY = 

VAR a = SELECTEDVALUE(Sheet1[year_bd])-1
RETURN
CALCULATE(SUM(Sheet1[GMV],CONVERT(Sheet1[year_bd],INTEGER)=a)

 

View solution in original post

8 REPLIES 8
Vera_33
Resident Rockstar
Resident Rockstar

Hi @YunJ 

 

So your Year_bd is like 2021 but text? Convert it first? Or can you change the data type?

CONVERT(SELECTEDVALUE(Sheet1[year_bd]),INTEGER)

  

Hi Vera,

I can have the correct result if I change it to "Whole number". But I'm wondering if there is any method that I can write DAX to accomplish this?

1.png

Please find the pbix here:

https://www.dropbox.com/s/om3z8h7qqdfb82q/SBD.pbix?dl=0

Thanks a lot for your time.

Vera_33
Resident Rockstar
Resident Rockstar

Hi @YunJ 

Have you tried to CONVERT it? The above DAX, I can't use dropbox...

Hi @Vera_33 

I tried the CONVERT measure, but still fail.

1.png

Vera_33
Resident Rockstar
Resident Rockstar

Hi @YunJ 

 

I see the problem...no need to convert the above one, but the below one, you can use either way, convert or the way provided by @amitchandak 

GMV_LY = 

VAR a = SELECTEDVALUE(Sheet1[year_bd])-1
RETURN
CALCULATE(SUM(Sheet1[GMV],Sheet1[year_bd]*1=a)

OR...

GMV_LY = 

VAR a = SELECTEDVALUE(Sheet1[year_bd])-1
RETURN
CALCULATE(SUM(Sheet1[GMV],CONVERT(Sheet1[year_bd],INTEGER)=a)

 

Thanks a lot @Vera_33 . It works!

So the function selectedvalue will return as number right? Even if sheet1[year_bd] is string?

SELECTEDVALUE(Sheet1[year_bd])-1

Another question is sheet1[year_bd]*1 will return as number right? even if sheet1[year_bd] is string?

Sheet1[year_bd]*1

Thanks again for your time!

amitchandak
Super User
Super User

@YunJ , Change var a like

 

var a = selectedvalue(Sheet1[year_bd])*1 -1

 

Otherwise, share the error.

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Hi @amitchandak 

I can have the correct result if I change it to "Whole number". But I'm wondering if there is any method that I can write DAX to accomplish this?

1.png

I tried your solution but fail to accomplish.

2.png

Please find the pbix here:

https://www.dropbox.com/s/om3z8h7qqdfb82q/SBD.pbix?dl=0

Thanks a lot for your time.

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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