Forum Discussion

Displaced_2000's avatar
Displaced_2000
Frequent Visitor
2 years ago
Solved

DAX code to countrows for all cases active during a week

Hi,   I am wanting to show my data by each week and count the cases that are active in that specific week based upong start and end dates.  If the end date is blank it should be included in all wee...
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi,Displaced_2000.Hello, hackcrr,thank you very much for your help on this issue, I would like to share my solution below.
    I am glad to help you.

    According to your description, you want to count the number of activities per week and count it as the current week if the end date is empty (the activity is included in all weeks from the start date to the current date)
    I have performed the following test as I understand it and you can refer to it:
    Since there is a relationship between the 'tracker' table and the dates table, I used the weekNum function to calculate the number of weeks for the start date and end date of each activity (counting the weeks in the current year), and the number of weeks to see how many weeks the activity spanned, then I counted the number of weeks for all the dates in the dates column, and finally created a measure,if the dates column in the dates is between the If the date column in dates is between the start and end weeks in the tracker table, then count the data in the 'tracker' table that meets this condition

    Here is the test data and Dax code

    The code of the calculate column

    C_endTime = IF(ISBLANK('tracker'[ConditionalWeeklyActiveDate]),TODAY(),'tracker'[ConditionalWeeklyActiveDate]) 
    C_endWeek = WEEKNUM('tracker'[C_endTime],2)
    C_startWeek = WEEKNUM('tracker'[Created],2) 
    date_weekNum = WEEKNUM('Dates'[Date],2)

    The code of the result

    M_result = CALCULATE(
        COUNTROWS('tracker'),FILTER(ALL(tracker),SELECTEDVALUE(Dates[date_weekNum])<='tracker'[C_endTime]&&SELECTEDVALUE(Dates[date_weekNum])>='tracker'[C_startWeek])
    )
    

    The result is shows below.

    I hope my code provides you with ideas to solve the problem, and you can optimize the code according to your own needs!

    If you have any more questions, please clarify in a follow-up reply.

    Best Regards,

    Carson Jian,

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