Forum Discussion

unais's avatar
unais
Helper V
6 years ago
Solved

Same Period Last Year Not Working With Filter

Dears,

Please advise below  DAX  with Sameperiodlast year not working..

 

SalaryWithCC(WithoutWPS) Last Year = CALCULATE(SUM(ZFAGLFLEXA_FI_DOC[NEWHSL]) ,FILTER(ZFAGLFLEXA_FI_DOC,ZFAGLFLEXA_FI_DOC[RCNTR] <> "" ),FILTER(GLMapping,GLMapping[GLGroup_Index]="3"),FILTER(ZCEPCT_PRFT_TXT,ZCEPCT_PRFT_TXT[INCGRP1]="INCGRP1"),SAMEPERIODLASTYEAR('Calendar'[Date]) )
 
Whereas without the same period last year it's working.
 
SalaryWithCC(WithoutWPS) Last Year = CALCULATE(SUM(ZFAGLFLEXA_FI_DOC[NEWHSL]) ,FILTER(ZFAGLFLEXA_FI_DOC,ZFAGLFLEXA_FI_DOC[RCNTR] <> "" ),FILTER(GLMapping,GLMapping[GLGroup_Index]="3"),FILTER(ZCEPCT_PRFT_TXT,ZCEPCT_PRFT_TXT[INCGRP1]="INCGRP1") )

 

Also if I remove Filter : 

FILTER(ZFAGLFLEXA_FI_DOC,ZFAGLFLEXA_FI_DOC[RCNTR] <> "" )
and with SAMEPERIODLAST YEAR it's working
 
 
SalaryWithCC(WithoutWPS) Last Year = CALCULATE(SUM(ZFAGLFLEXA_FI_DOC[NEWHSL]) ,FILTER(GLMapping,GLMapping[GLGroup_Index]="3"),FILTER(ZCEPCT_PRFT_TXT,ZCEPCT_PRFT_TXT[INCGRP1]="INCGRP1"),SAMEPERIODLASTYEAR('Calendar'[Date]) )
 
 so what's wrong with the query  please advise

 

  • I think best thing is to do is create 2 filters

     

    - One with your actual measures with Filter

    SalaryWithCC(WithoutWPS)  = CALCULATE(SUM(ZFAGLFLEXA_FI_DOC[NEWHSL]) ,FILTER(ZFAGLFLEXA_FI_DOC,ZFAGLFLEXA_FI_DOC[RCNTR] <> "" ),FILTER(GLMapping,GLMapping[GLGroup_Index]="3"),FILTER(ZCEPCT_PRFT_TXT,ZCEPCT_PRFT_TXT[INCGRP1]="INCGRP1") )

     

    - After that you should create Last Year Measure

     

    SalaryWithCC(WithoutWPS) Last Year = CALCULATE( SalaryWithCC(WithoutWPS), SAMEPERIODLASTYEAR (Calendar[Date]))
    
    

     

    Make sure that your calendar is marked as "Date Table"

5 Replies

  • vanessafvg's avatar
    vanessafvg
    Community Champion
    maybe it would be better to use if(isblank(field) rather than <> ""
  • FarhanAhmed's avatar
    FarhanAhmed
    Community Champion

    I think best thing is to do is create 2 filters

     

    - One with your actual measures with Filter

    SalaryWithCC(WithoutWPS)  = CALCULATE(SUM(ZFAGLFLEXA_FI_DOC[NEWHSL]) ,FILTER(ZFAGLFLEXA_FI_DOC,ZFAGLFLEXA_FI_DOC[RCNTR] <> "" ),FILTER(GLMapping,GLMapping[GLGroup_Index]="3"),FILTER(ZCEPCT_PRFT_TXT,ZCEPCT_PRFT_TXT[INCGRP1]="INCGRP1") )

     

    - After that you should create Last Year Measure

     

    SalaryWithCC(WithoutWPS) Last Year = CALCULATE( SalaryWithCC(WithoutWPS), SAMEPERIODLASTYEAR (Calendar[Date]))
    
    

     

    Make sure that your calendar is marked as "Date Table"

  • unais , use

    not(isblank(ZFAGLFLEXA_FI_DOC[RCNTR] )) for ZFAGLFLEXA_FI_DOC[RCNTR] <> ""

     

    Also, make sure your calendar table is marked as date table.

    Example of other ways

    YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
    Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))
    This year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR('Date'[Date]),"12/31"))
    Last year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31"))
    Last to last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-2,Year),"12/31"))
    Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

     

    refer

    https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a

     

    To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
    https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
    https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
    https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

    See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184


    Appreciate your Kudos.

    • unais's avatar
      unais
      Helper V

      I had created Two Mesure 

      like below

       

       

      GLTotalLR = CALCULATE(SUM(ZFAGLFLEXA_FI_DOC[NEWHSL]),DATESYTD(DATEADD('Calendar'[Date],-1,YEAR),"12/31"))
       
      SalaryWithCC(WithoutWPS) Last Year = CALCULATE([GLTotalLR],FILTER(ZFAGLFLEXA_FI_DOC,NOT(ISBLANK(ZFAGLFLEXA_FI_DOC[RCNTR])) ),FILTER(GLMapping,GLMapping[GLGroup_Index]="3"),FILTER(ZCEPCT_PRFT_TXT,ZCEPCT_PRFT_TXT[INCGRP1]="INCGRP1"))
      but didn't work ...
       
  • Icey's avatar
    Icey
    Community Support

    Hi unais ,

     

    It's best to share us your .pbix file for test. Please remove sensitive information and unnecessary visuals. It is suggested to upload your file to OneDrive for Business and then paste the link here.

     

     

     

    Best Regards,

    Icey