Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

YTD for Weeks

I am able to calculate ytd just with simple totalytd function 

its working for years months days but not for weeks 

i got week number from date column 

sample table will be like

date sales_amount 

now week from date 


and in line chart i have to show ytd using heirarchy of year month week and day

how can i do it please help me out

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Anonymous 

     

    Thank you very much Sergii24 and rajendraongole1 for your prompt reply.

     

    For your idea of displaying YTD in the line chart according to the hierarchy of year, month, day and week, here I provide a way of thinking:

     

    Here's some dummy data

     

    “Table”

     

    Adds a calculation column to calculate the number of weeks.

     

    WeekNum = WEEKNUM('Table'[Date], 21)

     

     

    Create a measure.

     

    YTD Sales by Week = 
    CALCULATE(
        SUM('Table'[Sales_Amount]),
        FILTER(
            ALL('Table'),
            YEAR('Table'[Date]) = YEAR(TODAY())        
            &&
            'Table'[WeekNum] = MAX('Table'[WeekNum])
        )
    )

     

    Here is the result.

     

     

    If you're still having problems, provide some dummy data and the desired outcome. It is best presented in the form of a table.

     

    Regards,

    Nono Chen

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

5 Replies

  • Hi Anonymous - Make sure your date table has weeknumber in it as like below:

    You can create a YTD measure that considers the week level by using TOTALYTD:

     

    YTD Sales =
    CALCULATE(
    SUM(Saless[sales_amount]),
    FILTER(
    ALL(DateTable),
    DateTable[Year] = MAX(DateTable[Year]) &&
    DateTable[Date] <= MAX(DateTable[Date])
    )
    )

     

    or specifically handle YTD up to the end of the current week:

     

    YTD Sales by Week =
    CALCULATE(
    SUM(Saless[sales_amount]),
    FILTER(
    ALL(DateTable),
    DateTable[Year] = MAX(DateTable[Year]) &&
    DateTable[Week Number] <= MAX(DateTable[Week Number])
    )
    )

     

    Hope it works.

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Try to put your solution in a matrix or something and try it 

      it will not work when you insert weeks in between month and days

       

  • Hi Anonymous, there are 2 reasons the reason why you can't apply YTD to weeks:

    1. Weeks are not uniquely assgined to year/quarter/month (for instance week 52 of 2021 included the preiod 27th of Dec 2021 - 2nd Jan 2022). So where do you want to see it in a hierarchy? Under 2021 or 2022?
    2. Same week corresponds to different period in 2 consequent years, therefore comparison Year over Year is complex as well (do you want to compare the same calendar period or the same week number?)

    Once you answer questions above for yourself you can use Calculate() function to ovewrite filters.

    The following DAX code for Calendar Table will help you. Feel free to adapt it to your needs. The column you're interested in is "Year-Week" (you can copy-paste this code and create a new table in PowerBI to play around).

    c_Calendar = 
    VAR MinDate = DATE( 2020,1,1)
    VAR MaxDate = DATE( 2025,12,31)
    
    VAR BaseCalendar =
        CALENDAR( MinDate, MaxDate )
    
    RETURN
        GENERATE(
            BaseCalendar,
            VAR _BaseDate = [Date]
            VAR _YearDate = YEAR( _BaseDate )
            VAR _MonthNumber = MONTH( _BaseDate )
            VAR _WeekNum = WEEKNUM( _BaseDate, 21 )                                          //ISO format is used to properly get the correct week number for the end of the year 
            
            RETURN ROW (
                "Year", _YearDate,
                "Month", _MonthNumber,
                "Month Name", FORMAT( _BaseDate, "MMM" ),
                "Week", _WeekNum,
                "Year-Week",
                    IF(
                        _WeekNum > 50 && _MonthNumber = 1,                                   //January doesn't have weeks more than 5, so if WeekNum is > 50, it's the end of the previous year
                        INT( _YearDate-1 & FORMAT( _WeekNum, "00" ) ),                       //therefore ISO Year corresponds to year-1
                        IF(
                            _WeekNum < 3 && _MonthNumber = 12,                               //the same for Dec and week less than 3, which can't be in Dec, so if WeekNum is < 3, it's the beginning of the next year
                            INT( _YearDate+1 & FORMAT( _WeekNum, "00" ) ),                   //therefore ISO Year corresponds to year+1
                            INT( _YearDate & FORMAT( _WeekNum, "00" ) )
                        )
                    ),                           
                "Year-Month Number", INT( _YearDate & FORMAT( _MonthNumber, "00" ) ), 
                "Month-Year", FORMAT( _BaseDate, "MMM" ) & "-" & _YearDate )
        )

     

    Good luck with your project!

    • Anonymous's avatar
      Anonymous
      Not applicable

      if you do weeknum(date,21)


      it will not show 52 after it

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

     

    Thank you very much Sergii24 and rajendraongole1 for your prompt reply.

     

    For your idea of displaying YTD in the line chart according to the hierarchy of year, month, day and week, here I provide a way of thinking:

     

    Here's some dummy data

     

    “Table”

     

    Adds a calculation column to calculate the number of weeks.

     

    WeekNum = WEEKNUM('Table'[Date], 21)

     

     

    Create a measure.

     

    YTD Sales by Week = 
    CALCULATE(
        SUM('Table'[Sales_Amount]),
        FILTER(
            ALL('Table'),
            YEAR('Table'[Date]) = YEAR(TODAY())        
            &&
            'Table'[WeekNum] = MAX('Table'[WeekNum])
        )
    )

     

    Here is the result.

     

     

    If you're still having problems, provide some dummy data and the desired outcome. It is best presented in the form of a table.

     

    Regards,

    Nono Chen

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