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
Synik
Helper II
Helper II

Trouble with showing correct year value with slicers

Hello everyone! 

I'm so close with this code, but I keep running into the same issue and could use another set of eyes to point out what I'm missing. 

What I'm trying to do is to simply display the values of year slicers on a card depending on 3 cases: no selection, current year, or current year -1. Here is my current code: 

 

 

var yearDefault = YEAR(NOW()) & " & " & YEAR(NOW())-1
var yearSelected = NOT( ISFILTERED( 'Data'[Year]))

VAR YearReturn =  IF( yearSelected
    , SELECTEDVALUE('Data'[Year]) 
    , yearDefault
    )


RETURN 
YearReturn

 

 

What is currently happening is when nothing is selected, I get a "(Blank)" showing in the card visual when it should be what I'm referencing in yearDefault, which is "2021 & 2020". Any ideas on what is causing this to show (Blank) instead of the years?

I've tried many various attempts but it has never worked so far. I'd appreciate any help into this! So close!

1 ACCEPTED SOLUTION
Synik
Helper II
Helper II

I was thinking of how to fix this and discovered an easier way if I just removed the IF statement. 

var yearDefault = YEAR(NOW()) & " & " & YEAR(NOW())-1

VAR test = SELECTEDVALUE('data'[Year], yearDefault)

As I normally do, I was overcomplicating it.  

View solution in original post

2 REPLIES 2
Synik
Helper II
Helper II

I was thinking of how to fix this and discovered an easier way if I just removed the IF statement. 

var yearDefault = YEAR(NOW()) & " & " & YEAR(NOW())-1

VAR test = SELECTEDVALUE('data'[Year], yearDefault)

As I normally do, I was overcomplicating it.  

VahidDM
Super User
Super User

Hi @Synik 

 

Try this measure:

 

 

Selected Year = 
Var _yearDefault = YEAR(today()) & " & " & YEAR(today())-1
VAR _YearReturn =  IF( HASONEVALUE('Data'[Year])
    , SELECTEDVALUE('Data'[Year]) 
    , _yearDefault
    )


RETURN 
_YearReturn

 

Output:

VahidDM_1-1630032894870.png

 

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

Appreciate your Kudos VahidDM_0-1630032853674.png !!

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.

Top Solution Authors