Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Returning first working date per week

Hello Community,

 

Hope you are well.

 

I have the below calendar table. I need to add a calculated column returning the first working date per week.

 

Any help more than welcomed.

 

DateYearQuarterMonthWeek of YearYear_QuarterYear_MonthYear_WeekDay_NameWorking/Weekend_Day

01/01/202120211112021.Q12021.M012021.W01FriWorking
02/01/202120211112021.Q12021.M012021.W01SatWeekend
03/01/202120211112021.Q12021.M012021.W01SunWeekend
04/01/202120211122021.Q12021.M012021.W02MonWorking
05/01/202120211122021.Q12021.M012021.W02TueWorking
06/01/202120211122021.Q12021.M012021.W02WedWorking
07/01/202120211122021.Q12021.M012021.W02ThuWorking
08/01/202120211122021.Q12021.M012021.W02FriWorking
09/01/202120211122021.Q12021.M012021.W02SatWeekend
10/01/202120211122021.Q12021.M012021.W02SunWeekend
11/01/202120211132021.Q12021.M012021.W03MonWorking
12/01/202120211132021.Q12021.M012021.W03TueWorking
13/01/202120211132021.Q12021.M012021.W03WedWorking
14/01/202120211132021.Q12021.M012021.W03ThuWorking
15/01/202120211132021.Q12021.M012021.W03FriWorking
16/01/202120211132021.Q12021.M012021.W03SatWeekend
17/01/202120211132021.Q12021.M012021.W03SunWeekend
18/01/202120211142021.Q12021.M012021.W04MonWorking
19/01/202120211142021.Q12021.M012021.W04TueWorking
20/01/202120211142021.Q12021.M012021.W04WedWorking
21/01/202120211142021.Q12021.M012021.W04ThuWorking
22/01/202120211142021.Q12021.M012021.W04FriWorking
23/01/202120211142021.Q12021.M012021.W04SatWeekend
24/01/202120211142021.Q12021.M012021.W04SunWeekend
25/01/202120211152021.Q12021.M012021.W05MonWorking
26/01/202120211152021.Q12021.M012021.W05TueWorking
27/01/202120211152021.Q12021.M012021.W05WedWorking
28/01/202120211152021.Q12021.M012021.W05ThuWorking
29/01/202120211152021.Q12021.M012021.W05FriWorking
30/01/202120211152021.Q12021.M012021.W05SatWeekend
31/01/202120211152021.Q12021.M012021.W05SunWeekend
01/02/202120211262021.Q12021.M022021.W06MonWorking

 

Thank you in advance,

 

George

  • If you want a date for each row, then try

    FirstWorkdayOfWeek =
    CALCULATE (
        MIN ( 'Calendar'[Date] ),
        ALLEXCEPT ( 'Calendar', 'Calendar'[Year], 'Calendar'[Week of Year] )
    )

    This takes the minimal date in the calendar table for the Year and Week of Year combination in that particular row.

9 Replies

  • If you want a date for each row, then try

    FirstWorkdayOfWeek =
    CALCULATE (
        MIN ( 'Calendar'[Date] ),
        ALLEXCEPT ( 'Calendar', 'Calendar'[Year], 'Calendar'[Week of Year] )
    )

    This takes the minimal date in the calendar table for the Year and Week of Year combination in that particular row.

  • Anonymous ,

    NEw column =

    if( [Date] =minx(filter(table, [Year_Week] = earlier([Year_Week]) ),[Date]) , True(), false())

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

    Hi Anonymous 

    Try

    MyColumn =
    CALCULATE (
    VALUES ( 'Calendar'[Working/Weekend] ),
    FILTER (
    ALL ( 'Calendar' ),
    'Calendar'[Year-Week] = EARLIER ( 'Calendar'[Year-Week] )
    && 'Calendar'[Working/Weekend] = EARLIER ( 'Calendar'[Working/Weekend] )
    ),
    'Calendar'[Date] = MIN ( 'Calendar'[Date] )
    )

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Please try:

    Column = CALCULATE(FIRSTNONBLANK('Table'[Date],TRUE()),FILTER('Table',[Year_Week]=EARLIER('Table'[Year_Week]) && [Working/Weekend_Day]="Working"))

    Output:

      

     

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

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hello all,

     

    Thank you very much for your replies. Please see below the outcome of all 4 replies. I decided to flag AlexisOlson as the preferred solution as I still try to put my mind around the "earlier" function that other users had used at their replies. 

     

     

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

      Hi Anonymous 

      for a calculated column you have a row context. For each row (ech cell of this calculated coulumn), FILTER will iterate over the table creating a new row context inside the earlier one. EARLIER restores the previous raw context and evaluates the expression based on it. 
      In your caseEARLIER ( 'Calendar'[Working/Weekend] ) is actually the value of this coulmn at the same row under evaluation. While 'Calendar'[Working/Weekend] will have different values during the iteration of FILTER

    • AlexisOlson's avatar
      AlexisOlson
      Icon for Super User rankSuper User

      EARLIER can be a bit confusing if you think it refers to an earlier time when it actually refers to an earlier row context.

       

      You can rewrite a version using EARLIER

      CALCULATE (
          MIN ( 'Calendar'[Date] ),
          FILTER (
              ALL ( 'Calendar' ),
              'Calendar'[Year_Week] = EARLIER ( 'Calendar'[Year_Week] )
          )
      )

      using a variable instead

      VAR CurrYear_Week = 'Calendar'[Year_Week]
      RETURN
          CALCULATE (
              MIN ( 'Calendar'[Date] ),
              FILTER ( ALL ( 'Calendar' ), 'Calendar'[Year_Week] = CurrYear_Week )
          )
      • Anonymous's avatar
        Anonymous
        Not applicable

        thank you AlexisOlson  for that . There is a very educative vide at Curbal youtube channel explaining earlier. However, I prefer the use of variables as , among other things , provides a clearer picture of how the measure works . Also, the SQLBI team is also in favour of using variables instead of earlier. Going back to your variable example, I believe that the variable reflects the current row, am i correct in reading it like that? thank you