Forum Discussion

bilingual's avatar
bilingual
Icon for Helper V rankHelper V
6 years ago

SAMEPERIODLASTYEAR - when using with Power BI Date table , it adds future years

Hi, i have used SAMEPERIODLASTYEAR  with no problems, before, but when i use it with the date table created in Power BI, somehow i get the 2020 results replicated in 2021, despite there is no data yet from that period(only data to Juli 2020)   

 

My formula in Danish : 

Samme periode sidste år = CALCULATE((SUM(Data_akk[Antal rejser]));SAMEPERIODLASTYEAR(Kalender[Date]))
 
For example, for Juli 2021, i get the results from Juli 2020 in the column where SAMEPERIODLASTYEAR is used.
Can anybody help a novice? 🙂

7 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi bilingual ,

     

    You cna try restricting you Calendar Date to  the maximum date in the Data_akk Table

     

    Min Year = Minimum year in Data_akk Table

    Max Year  = Maximum Year in Data_akk Table

     

    Date =
    VAR MinYear = YEAR ( MIN ( Data_Akk[Order Date] ) )
    VAR MaxYear = YEAR ( MAX ( Data_Akk[Order Date] ) )
    RETURN
    ADDCOLUMNS (
        FILTER (
            CALENDARAUTO( ),
            AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
        ),
        "Calendar Year", "CY " & YEAR ( [Date] ),
        "Month Name", FORMAT ( [Date], "mmmm" ),
        "Month Number", MONTH ( [Date] )
    )

     

    Regards,
    Harsh Nathani

    Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

     

    • bilingual's avatar
      bilingual
      Icon for Helper V rankHelper V

      Thanks a lot, it solved and removed 2021, however despite the data is only till Juli 2020, SAMEPERIODLASTYEAR still gives me the 2019 results for August -December and thereby extends the date period to August-December 2020.

       

      Can i limit the formula somehow so it only gives the results for last year for the active months?

  • v-yingjl's avatar
    v-yingjl
    Icon for Community Support rankCommunity Support

    Hi bilingual ,

    How did you create the Calendar date table? Could you please consider sharing some screenshots about this issue for further discussion?

    Or you can try to use Calendartauto() to create a date table, create relationship with the fact table and use sameperiodlastyear() function to test.

     

    Best Regards,
    Yingjie Li

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

    • bilingual's avatar
      bilingual
      Icon for Helper V rankHelper V

      Hej Yingjie, this is my Date Calender(Danish)

      Kalender =
      VAR MinYear = YEAR ( MIN ( Data[Dato] ) )
      VAR MaxYear = YEAR ( MAX ( Data[Dato] ) )
      RETURN
      ADDCOLUMNS (
      FILTER (
      CALENDARAUTO( );
      AND ( YEAR ( [Date] ) >= MinYear; YEAR ( [Date] ) <= MaxYear )
      );
      "Dato for heltal"; FORMAT ( [Date]; "YYYYMMDD" );
      "År"; YEAR ( [Date] );
      "Månedsnummer"; FORMAT ( [Date]; "MM" );
      "År/måned tekst"; FORMAT ( [Date]; "YYYY/MM" );
      "År/måned"; FORMAT ( [Date]; "YYYY/mmm" );
      "Månedsnavn_kort"; FORMAT ( [Date]; "mmm" );
      "Månedsnavn_UK"; FORMAT ( [Date]; "mmmm" );
      "Ugenr"; WEEKNUM([Date];2);
      "Ugedag"; FORMAT ( [Date]; "dddd" );
      "Ugedag forkortet"; FORMAT ( [Date]; "ddd" );
      "Kvartal"; "Q" & FORMAT ( [Date]; "Q" );
      "YearQuarter"; FORMAT ( [Date]; "YYYY" ) & "/Q" & FORMAT ( [Date]; "Q" )
      )

  • Mariusz's avatar
    Mariusz
    Icon for Community Champion rankCommunity Champion

    Hi bilingual 

     

    You can add a filter to your calendar table to be less than today 


    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.

    Please feel free to connect with me.
    LinkedIn



  • bilingual , is Jul 2021 is selected in slicer or filtered on the page or you are showing report by Month-year you will.

    Also, make sure Kalendar is marked as Calendar table.

    other ways

    MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD('Date'[Date]))
    last MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
    last MTD (complete) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date],-1,MONTH))))
    last year MTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(dateadd('Date'[Date],-12,MONTH)))
    last year MTD (complete) Sales =  CALCULATE(SUM(Sales[Sales Amount]),DATESMTD(ENDOFMONTH(dateadd('Date'[Date],-12,MONTH))))
    Month behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Month))
    Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))
    Next month value =  CALCULATE(sum('table'[total hours value]),nextmonth('Date'[Date]))

     

     

    Power BI — MTD Questions — Time Intelligence 3–5
    https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e

    Power BI — YTD Questions — Time Intelligence 1–5
    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.