Forum Discussion

Ale's avatar
Ale
Resolver II
7 years ago
Solved

Calculate function with filtering by date not working

Dear all,
 
I have a DAX where I want to show a YTD value. Since the user selects only one month in the slicer, I had to use a Calculate function to retrieve the YTD (function TOTALYTD did not work). You can check the code below.

Sales_YTD =
VAR SD = [StartDate]
VAR ED = [EndDate]
VAR Result = CALCULATE (
SUM(GMS_View[Value]); FILTER ( ALLEXCEPT (
GMS_View; GMS_View[Brand.Brand Level 01]; GMS_View[Country.Country Level 01]); GMS_View[date] >= SD && GMS_View[date] <= ED )
) RETURN Result
 
The function uses 2 measures to get the StartDate and EndDate. They simply return the dates in the format dd.MM.yyyy, where SD is normally 01.01.yyyy and ED is 01.MM.yyyy. (we work only with month, not days, that's why I assign day 01 always)
 
The issue is: if I only have data until August/2019, and the user selects November/2019, the DAX returns blank.
 
"Fun" fact is: if instead of using the Measure [EndDate], I simply use VAR ED = DATE(2019,11,01), the function returns the correct value. So, the issue seems to be that the [EndDate] does not bring the correct value. However, the measure is returning the right value (I did the test simply returning ED instead of Result in the DAX). Which leads me to conclude that the issue is between the Measure and the Calculate function.
 
Can anyone shed some light on that? Thanks in advance!
 
 
  • Hi Ale

     

    I would suggest you modify the ED like below.If it is not your case,please share the .pbix file for us to test. You can upload it to OneDrive and post the link here. Do mask sensitive data before uploading.

     

    Sales_YTD =
    VAR SD = [StartDate]
    VAR ED = SELECTEDVALUE(Table[Slicer])
    VAR Result = CALCULATE (
                           SUM(GMS_View[Value]);
                           FILTER (
                                  ALLEXCEPT (
                                            GMS_View;
                                            GMS_View[Brand.Brand Level 01];
                                            GMS_View[Country.Country Level 01]);
                                  GMS_View[date] >= SD && GMS_View[date] <= ED
                                   )
                           )
    RETURN Result

    Regards,

    Cherie

     

3 Replies

  • v-cherch-msft's avatar
    v-cherch-msft
    Microsoft Employee

    Hi Ale

     

    I would suggest you modify the ED like below.If it is not your case,please share the .pbix file for us to test. You can upload it to OneDrive and post the link here. Do mask sensitive data before uploading.

     

    Sales_YTD =
    VAR SD = [StartDate]
    VAR ED = SELECTEDVALUE(Table[Slicer])
    VAR Result = CALCULATE (
                           SUM(GMS_View[Value]);
                           FILTER (
                                  ALLEXCEPT (
                                            GMS_View;
                                            GMS_View[Brand.Brand Level 01];
                                            GMS_View[Country.Country Level 01]);
                                  GMS_View[date] >= SD && GMS_View[date] <= ED
                                   )
                           )
    RETURN Result

    Regards,

    Cherie

     

    • v-cherch-msft's avatar
      v-cherch-msft
      Microsoft Employee

      Hi Ale 

       

      Could you tell me if your problem has been solved? If it is,kindly mark the helpful answer as a solution if you feel that makes sense. Welcome to share your own solution. More people will benefit from here.

       

      Regards,

      Cherie