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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
sakuragihana
Helper IV
Helper IV

My dax is not working correctly

Hello everyone, I have a dax for counting the number of days for Monday, Tuesday, Wednesday, Thursday, Friday, Saturday Sunday at the month.

 

DayOfTeachCount =
VAR SelectedWeekStart = MIN ('Teacher Schedule'[Date])
VAR SelectedWeekEnd = MAX ('Teacher Schedule'[Date])
VAR Starttime = VALUE(SELECTEDVALUE('Teacher Schedule'[StartTime]))
VAR Endtime = VALUE(SELECTEDVALUE('Teacher Schedule'[EndTime]))
VAR Result =
        SUMX(
            VALUES ( 'Teacher Schedule'[Date]),
            SWITCH (
                WEEKDAY ( 'Teacher Schedule'[Date],2 ),
                1, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1, 0 ),
                2, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1, 0 ),
                3, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1, 0 ),
                4, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1, 0 ),
                5, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1, 0 ),
                6, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1, 0 ),
                7, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1, 0 ),
                BLANK ()
            )
        )
  Return Result
But the visual is :
sakuragihana_0-1690189806962.png

 

But the DayofTeachCount is not exactly because 3 of Fridays are not time for teaching

sakuragihana_1-1690189806962.png

 

 

The visual right must have be : DayofTeachRight = 10

 

 

I make a power BI file with table data in this link:https://drive.google.com/file/d/1KRmfetcp4tVrf2Xti2HMZRFnMJELpvTU/view?usp=drive_link 

Please everyone help me add a condition into dax DayOfTeachCount : if StartTime > 0 and EndTime > 0 to count the number of days of Monday, Tuesday, ..., if StartTime = 0 and EndTime = 0 then the day is not counted.

Thank a lot

6 REPLIES 6
mlsx4
Super User
Super User

Hi @sakuragihana 

 

You can create a kind of multiplier:

Multiplier = 

SUMX('Teacher Schedule', IF('Teacher Schedule'[StartTime]="0",0,1))

 

And then, modify your measure:

DayOfTeachCount = 
VAR SelectedWeekStart = MIN ('Teacher Schedule'[Date])
VAR SelectedWeekEnd = MAX ('Teacher Schedule'[Date])
VAR Starttime = VALUE(SELECTEDVALUE('Teacher Schedule'[StartTime]))
VAR Endtime = VALUE(SELECTEDVALUE('Teacher Schedule'[EndTime]))
VAR Result = 
        SUMX(
            VALUES ( 'Teacher Schedule'[Date]),
            SWITCH (
                WEEKDAY ( 'Teacher Schedule'[Date],2),
                1, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1*[Multiplier], 0 ),
                2, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1*[Multiplier], 0 ),
                3, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1*[Multiplier], 0 ),
                4, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1*[Multiplier], 0 ),
                5, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1*[Multiplier], 0 ),
                6, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1*[Multiplier], 0 ),
                7, IF ( 'Teacher Schedule'[Date] >= SelectedWeekStart && 'Teacher Schedule'[Date] <= SelectedWeekEnd, 1*[Multiplier], 0 ),
                BLANK ()
            )
        )
  Return Result 

Hi @mlsx4 ,

I am sorry but the dax is not correctly. The result is : 21 ( the right result is 10)

 

sakuragihana_0-1690248713890.png

 

 

But why 10? Because of Saturdays and Sundays? 

Hi @mlsx4 ,

 You can see data in power BI file , the Friday that teacher have no schedule .

Hi @sakuragihana 

 

But I don't understand your point. It is not taking into account the fridays:

mlsx4_0-1690275518001.png

 

Hi @mlsx4 ,

sakuragihana_0-1690278638063.pngsakuragihana_1-1690278638715.png

 Total DayofTeach = 10 ( because Friday is not teach) , and another is count distinct because at one day have many time for teaching .

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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