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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply

Using a variable date in a CALCULATE function

Hi

I wonder if someone can help me, I have a project that involves using a CALCULATE function, and adding some filter values at the end of the function. One of these needs to a date value which is based on SELECTEDVALUE of a slicer.

My slicer uses the year part of a date field, and it is this I need to modify.

My function is:
CALCULATE(DISTINCTCOUNT(column),Column = 1,Date>DATE(SELECTEDVALUE(Datefield.Year)-1,4,1)))

So, for example if the user has selected '2021' from the year slicer, I am looking for this function to use '2020' as its filter.


This is being thrown back at me with the error that a true/false value is not allowed with CALCULATE.
I've tried adding the calculation as a variable at the start, but again it fails.

Is there something obvious I'm missing? Any help would be appreciated.

1 ACCEPTED SOLUTION

Please try to create a measure like below:

Measure 2 = CALCULATE(DISTINCTCOUNT('Table (2)'[Column]),FILTER(ALL('Table (2)'),'Table (2)'[Column1]=1&&YEAR('Table (2)'[date])=SELECTEDVALUE('Table (2)'[date].[Year])-1))

For more details,please refer to this sample .pbix

View solution in original post

3 REPLIES 3
VahidDM
Super User
Super User

Hi @powerbiuser9999 

 

Try to use FILTER dax code, like this:

measure = CALCULATE(DISTINCTCOUNT(column),Column=1,filter(table,Date>DATE(SELECTEDVALUE(Datefield.Year)-1,4,1)))

 

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

Appreciate your Kudos VahidDM_0-1630079436462.png !!

 

Hi @VahidDM 
Thanks for this, however it does not give me the result I am looking for.

If it helps, the following DAX does work.

CALCULATE(DISTINCTCOUNT(Column),Column=1,Date>DATE(2020,4,1)))

(I've hard coded a date for the test to work)

Its probably worth noting that:

DATE(SELECTEDVALUE(Column.Year)-1,4,1) works on its own, but not as part of the CALCULATE function.

Please try to create a measure like below:

Measure 2 = CALCULATE(DISTINCTCOUNT('Table (2)'[Column]),FILTER(ALL('Table (2)'),'Table (2)'[Column1]=1&&YEAR('Table (2)'[date])=SELECTEDVALUE('Table (2)'[date].[Year])-1))

For more details,please refer to this sample .pbix

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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