Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Sunilkulkarni
Frequent Visitor

DAX to calculate Number of days in a week

Hi There

How to calculate number of days in a week, like, 

I have attendance Table in which i want to calculate for how many days students are present for that week, Week1,2,3,4,5

in 1st week, how many students are present for 1 day,2days,3days,4 days,5 days

 

1 ACCEPTED SOLUTION
v-jiewu-msft
Community Support
Community Support

Hi @Sunilkulkarni ,

Please try the following methods and check if they can solve your problem:

1.Create the simple table.

vjiewumsft_0-1708425317279.png

2.Using the week num and week day to create the new column.

 

weeknumber = WEEKNUM('student present'[Date],2)
weekday = WEEKDAY('student present'[Date],2)

 

3.Create the measure to calculate student.

 

student present = CALCULATE(
            COUNTROWS('student present'),
            FILTER(
                'student present', 'student present'[IsPresent] = True
            )
        )

 

4.Drag the week number into the Rows, drag the weekday into the Columns and drag the measure into the matrix visual values.

vjiewumsft_1-1708425407822.png
Best Regards,
Wisdom Wu

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

 
 

View solution in original post

5 REPLIES 5
v-jiewu-msft
Community Support
Community Support

Hi @Sunilkulkarni ,

Please try the following methods and check if they can solve your problem:

1.Create the simple table.

vjiewumsft_0-1708425317279.png

2.Using the week num and week day to create the new column.

 

weeknumber = WEEKNUM('student present'[Date],2)
weekday = WEEKDAY('student present'[Date],2)

 

3.Create the measure to calculate student.

 

student present = CALCULATE(
            COUNTROWS('student present'),
            FILTER(
                'student present', 'student present'[IsPresent] = True
            )
        )

 

4.Drag the week number into the Rows, drag the weekday into the Columns and drag the measure into the matrix visual values.

vjiewumsft_1-1708425407822.png
Best Regards,
Wisdom Wu

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

 
 
123abc
Community Champion
Community Champion

To calculate the number of days each student is present in a given week, you can follow these steps using DAX (Data Analysis Expressions) in Power BI or Excel:

  1. Assuming you have a table named Attendance with columns StudentID, Date, and any other relevant columns.

  2. Create a calculated column to extract the week number from the date. Let's name it WeekNumber.

WeekNumber = WEEKNUM('Attendance'[Date])

 

Now, you can create a measure to count the number of days each student is present in each week.

 

DaysPresent =
CALCULATE(
COUNTROWS('Attendance'),
FILTER(
'Attendance',
'Attendance'[Presence] = "Present" && 'Attendance'[WeekNumber] = SELECTEDVALUE('Attendance'[WeekNumber])
)
)

 

In this measure:

  • COUNTROWS('Attendance') counts the number of rows (days) in the attendance table.
  • FILTER('Attendance', 'Attendance'[Presence] = "Present" && 'Attendance'[WeekNumber] = SELECTEDVALUE('Attendance'[WeekNumber])) filters the attendance table to only count the rows where the student is present and the week number matches the selected week number.

This measure will give you the count of days each student is present in the selected week. You can slice and dice the data by student and week to get the desired analysis. Make sure to replace 'Attendance', 'Presence', and other column names with the actual names from your data model.

 
 
 
 

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

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Thanks for the suggestion, will try this,

 

Im not selecting any week No. but i want to represnt in the Stacked bar chart,  below Num are no. of students present 

Week1Day2 Days3 Days4Days5Days
11035304510
2     
3     
4     

To represent the number of students present for each day of the week in a stacked bar chart in Power BI, you can follow these steps:

  1. Create a New Measure for Each Day: You need to create a separate measure for each day of the week (1 Day, 2 Days, 3 Days, 4 Days, 5 Days) that calculates the count of students present for that specific number of days.

    Here is an example of how you can create a measure for the count of students present for 1 day:

 

StudentsPresent_1Day = CALCULATE(COUNTROWS(Attendance), Attendance[WeekNumber] = SELECTEDVALUE(Attendance[Week]) && Attendance[DaysPresent] = 1)

 

  1. You should create similar measures for 2 Days, 3 Days, 4 Days, and 5 Days, replacing the condition Attendance[DaysPresent] = 1 with the appropriate conditions for each measure.

  2. Create a Stacked Bar Chart: Once you have created the measures for each day, you can create a stacked bar chart with the following settings:

    • Drag the Week field to the Axis.
    • Drag each measure (StudentsPresent_1Day, StudentsPresent_2Days, etc.) to the Values section of the chart.
    • Ensure that the chart type is set to Stacked Bar Chart.
  3. Format the Chart: You can format the chart to make it more readable and visually appealing. You may want to adjust the colors, add data labels, and provide a suitable chart title and axis labels.

By following these steps, you should be able to create a stacked bar chart in Power BI that represents the number of students present for each day of the week across different weeks. Adjust the measures and chart settings based on your specific data structure and requirements.

 

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

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Hi , can you please reply the DAX

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors