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

date variable not filtered in calculate function

Can someone help me to understand why my calculate function is not filtered by a variable containing a date ?

Code working with static date function in calculate:

VAR _MaxDate  =      MAX('f_Transactions'[Date.Transaction])         // Max Date Transactions = 2022.07.08
VAR _MinDate  =      DATE(2022,07,04)                                           // Min Date = Max Date - 30 Days = 2022.07.04
    Return
        CALCULATE(
            [Total Sales],
            FILTER(
                'd_Tbl Calendar',
                And(
                    'd_Tbl Calendar'[Date] <= _MaxDate,
                    'd_Tbl Calendar'[Date] >= _MinDate
                )
            )
        )
Result in Table =
AnSchmi_0-1658486757903.png

When I replace the static date function by for example

VAR _MinDate _MaxDate - 3 
I do not get the table filtered as expeced. Instead I do get all records in table:
AnSchmi_1-1658486991909.png


What is wrong ? 
Thanks a lot 

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @AnSchmi ,

 

Not sure what the model looks like. The steps of my test are as follows:

Create a date table and create relationships between it and the fact table.

Calendar = CALENDAR(MIN('f_Transactions'[Date.Transaction]),MAX('f_Transactions'[Date.Transaction]))

vcgaomsft_0-1658813166504.png

Create a new measure:

Measure = 
VAR _MaxDate = MAX('Calendar'[Date])
VAR _MinDate = _MaxDate - 3 
VAR _Result = 
CALCULATE(
    [Total Sales],
        FILTER(ALL('Calendar'),
        AND('Calendar'[Date]<=_MaxDate,
        'Calendar'[Date]>=_MinDate)
        )
)
RETURN
_Result

Drag the measures and the date column from calendar table into the table visual:

vcgaomsft_1-1658813327023.png

Filter correctly

vcgaomsft_2-1658813452362.png

The PBIX file is attached for reference.

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly -- How to provide sample data

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @AnSchmi ,

 

Not sure what the model looks like. The steps of my test are as follows:

Create a date table and create relationships between it and the fact table.

Calendar = CALENDAR(MIN('f_Transactions'[Date.Transaction]),MAX('f_Transactions'[Date.Transaction]))

vcgaomsft_0-1658813166504.png

Create a new measure:

Measure = 
VAR _MaxDate = MAX('Calendar'[Date])
VAR _MinDate = _MaxDate - 3 
VAR _Result = 
CALCULATE(
    [Total Sales],
        FILTER(ALL('Calendar'),
        AND('Calendar'[Date]<=_MaxDate,
        'Calendar'[Date]>=_MinDate)
        )
)
RETURN
_Result

Drag the measures and the date column from calendar table into the table visual:

vcgaomsft_1-1658813327023.png

Filter correctly

vcgaomsft_2-1658813452362.png

The PBIX file is attached for reference.

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly -- How to provide sample data

Dear Gao, 

thanks a lot for your reply. 

Your solution seems to me static as well. 

Nevertheless, thanks a lot for your help. 
Best Regards

Andreas

AntonioM
Solution Sage
Solution Sage

I think the problem is the table is filtering the variable '_MaxDate'. It only gets the max date for the dates it can see, so if you have a row of one single date, that is the max date (instead of 22/07/08) . Try

VAR _MaxDate = CALCULATE ( MAX('f_Transactions'[Date.Transaction]), ALL('f_Transactions') ) 

and see if that works

 

Hi AntonioM, 
unfortunately not, the result after changing the _MaxDate is a table with only the last record.

AnSchmi_0-1658497667953.png

 

And that's with _Mindate as _Maxdate - 3?

Dear AntonioM, 

what do you mean by 
_Mindate as _Maxdate - 3 ? 
I do not understand your question. 

Can you be more precice please. 
The code after your recommendiation: 

Sales not Filtered in Calculate() =
    VAR _MaxDate =
        CALCULATE(
            MAX('f_Transactions'[Date.Transaction]),
            All(f_transactions)
                 )                                   // Max Date Transactions = 2022.07.08

    VAR _MinDate =
        _MaxDate - 3                                 // Min Date = Max Date - 30 Days = 2022.07.04
       

    Return
        CALCULATE(
            [Total Sales],
            FILTER(
                'd_Tbl Calendar',
                And(
                    'd_Tbl Calendar'[Date] <= _MaxDate,
                    'd_Tbl Calendar'[Date] >= _MinDate
                )
            )
        )

AnSchmi_0-1659334763901.png

Best Regards

Andreas

 

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.