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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
StevoNorf
New Member

Case When in Power BI

Hi

I have a SQL Case When query that calculates the numbers of working days for the current month and working month total from a calendar table.

Select WorkCalendarDate.CalendarID Calendar,
Case When Month(WorkCalendarDate.TransDate) = Month(@YesterdayDateTime) And Year(WorkCalendarDate.TransDate) = Year(@YesterdayDateTime) And WorkCalendarDate.TransDate <= @YesterdayDateTime Then Abs(Sum(WorkCalendarDate.WorkTimeControl-1)) Else 0 End CMTotal,
Case When Month(WorkCalendarDate.TransDate) = Month(@YesterdayDateTime) And Year(WorkCalendarDate.TransDate) = Year(@YesterdayDateTime) Then Abs(Sum(WorkCalendarDate.WorkTimeControl-1)) Else 0 End WMTotal

 

I have tried to replicate this in Power BI with some DAX which works fine for all days except when the current date is the first day of a new month. I need this to work using yesterdays date details to ensure the calculation for total work days uses the previous month and not the new month where it resets the values.

 

CMTotalWKDays =
CALCULATE (
COUNT(WorkCalendarDate[WorkTimeControl] ),
FILTER (
WorkCalendarDate,
WorkCalendarDate[TransDate] >= CustInvoiceTrans[Start of Month]
&& WorkCalendarDate[TransDate] <= CustInvoiceTrans[Yesterday]
)
)

----------------------------------------------Total Closed Days Per Month To Date--------------------------------------------
CMTotalClosedDays =
CALCULATE (
SUM(WorkCalendarDate[WorkTimeControl] ),
FILTER (
WorkCalendarDate,
WorkCalendarDate[TransDate] >= CustInvoiceTrans[Start of Month]
&& WorkCalendarDate[TransDate] <= CustInvoiceTrans[Yesterday]
)
)

--------------------------------------------Total Work Days Current Month to Date--------------------------------------------
CMTotal = (
[CMTotalWKDays]-[CMTotalClosedDays]
)

 

Any ideas where I am going wrong? Any help is greatly appreciated.

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

Hi, @StevoNorf 

According to your description, you want to count the number of days in a working day, right, your measure returns at the beginning of the month, you can try using the EOMONTH() function to get the date data you want.

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

2 REPLIES 2
v-yueyunzh-msft
Community Support
Community Support

Hi, @StevoNorf 

According to your description, you want to count the number of days in a working day, right, your measure returns at the beginning of the month, you can try using the EOMONTH() function to get the date data you want.

 

If this method does not meet your needs, you can provide us with your special sample data and the desired output sample data in the form of tables, so that we can better help you solve the problem.

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

 

Hi @v-yueyunzh-msft 

Thanks for your reply and answer. This did work for me using the EOMONTH() function that gave me the data I needed.

 

Thanks for your help!

 

Best Regards

Steve

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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