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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Setting variable dates as filters

**Problem resolved but post can't be deleted**

 

I'm having an issue with filtering values using variable dates.

Currently my formula is as follows:

 

Measure=
VAR MinYear = CALCULATE(MIN(RawData[date]), RawData[Type] = "After")
VAR MaxYear = CALCULATE(MIN(RawData[date]), RawData[Type] = "After") + (SELECTEDVALUE(Years[Years])*365)
 
RETURN
CALCULATE(COUNT(RawData[Ref]), RawData[date] >= MinYear && RawData[date] <= MaxYear )
 
If I check each variable, I get the correct datestamp for the two variables, similarly I get the correct answer if I replace the variables in the calculate function with " date(XX,XX,XX) " but for whatever reason it doesn't work with the variable.
 
When testing, the variables show timestamped dates, so I have tried setting the data type to timestamp in the query editor but this doesn't make a different. Any suggestions?

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I've actually managed to fix this!

Turns out I had a filter being applied via another slicer which was changing the date ranges and therefore not applying the assumed window.

 

Thanks anyway all!

View solution in original post

6 REPLIES 6
v-lionel-msft
Community Support
Community Support

Hi @Anonymous ,

 

Is this what you want?

COUNT(RawData[Ref])
  when RawData[date] >= MinYear && RawData[date] <= MaxYears

Could you show me a sample data model? I need to know the relationship between 'RawData' table and 'Years' table.

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

JarroVGIT
Resident Rockstar
Resident Rockstar

Hi @Anonymous , 

I see you reference a non-existing variable (MinYearPost) in your return statement, that doesn't seem right?

Furthermore, what is the context this measure is evaluated in? is it part of a visual (and what visual)? For example; could it be there are multiple values in Year[Years] at time of evaluation and hence the SELECTEDVALUE() will not return anything? 

 

Kind regards

Djerro123

-------------------------------

If this answered your question, please mark it as the Solution. This also helps others to find what they are looking for.

Keep those thumbs up coming! 🙂





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

Proud to be a Super User!




Anonymous
Not applicable

Whoops, I had changed the variable names in the post so it would make more sense out of context, but clearly forgot to change the variable names in the CALCULATE function!
It should show:
 
Measure=
VAR MinYear = CALCULATE(MIN(RawData[date]), RawData[Type] = "After")
VAR MaxYear = CALCULATE(MIN(RawData[date]), RawData[Type] = "After") + (SELECTEDVALUE(Years[Years])*365)
 
RETURN
CALCULATE(COUNT(RawData[Ref]), RawData[date] >= MinYear && RawData[date] <= MaxYear )
 
Selected value is based on a number slicer, so the user selects a number between 1-5 which will be *365 to get the number of days in a year (though it doesn't account for leap years)

Hi @Anonymous ,

Very weird, using your formula I can return the correct result.

Measure = 
VAR x = 
CALCULATE(
    MIN(RawData[Date]),
    FILTER(
        RawData,
        RawData[Type] = "after"
    )
)
VAR y = x + [Years Value]*365
RETURN
CALCULATE(
    COUNT(RawData[Ref]),
    FILTER(
        RawData,
        [Date] >= x && [Date] <= y
    )
)

nn11.PNG

(Fact table)

nn12.PNG

(What IF parameter)

 

Could you provide more information about the problem you have met?

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

I've actually managed to fix this!

Turns out I had a filter being applied via another slicer which was changing the date ranges and therefore not applying the assumed window.

 

Thanks anyway all!

Hi @Anonymous ,

If your problem has been solved, please consider Accept it as the solution  to help the other members find it more quickly.

 

Best regards,
Lionel Chen

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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