Forum Discussion

Rt0790's avatar
Rt0790
Regular Visitor
3 years ago
Solved

Need Help_Dax

Hi All, I have a dataset that is getting dates list between start date time and end date time and students that are scheduled for courses, 

 

start datetimeend datetimeStudentIDList datesCoursesScheduled
11/16/2022 12:06pm2/28/2023 12:00pm151/10/2023MathTRUE
11/16/2022 12:06pm2/28/2023 12:00pm151/10/2023ChemTRUE

 

 

created a new column "scheduled" to check if a student is scheduled for a course at 8 am everyday by using 

 

"at 8am = var _1 = date(year(list dates),month(list dates),day(list dates)+TIME (8,0,0)
Return
IF ( _1 <= end datetime && _1 >= start date time || _1-1 < end datetime && _1-1 >= start datetime, "TRUE","FALSE") "

 

My question is if a student is schedueld for more than one subject at the same time on any given day I want CHEM to be TRUE and not math 

 

Expected result

 

start datetimeend datetimeStudentIDList datesCoursesScheduled
11/16/2022 12:06pm2/28/2023 12:00pm151/10/2023MathFALSE
11/16/2022 12:06pm2/28/2023 12:00pm151/10/2023ChemTRUE

 

Thanks in advance

Really appreciate any help.

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Rt0790 ,

    Please try to create a new column with below dax formula:

    Column =
    VAR ed = [end datetime]
    VAR sd = [start datetime]
    VAR _id = [StudentID]
    VAR cse = [Courses]
    VAR tmp =
        SUMMARIZE (
            'Table',
            'Table'[StudentID],
            'Table'[List dates],
            'Table'[start datetime],
            'Table'[end datetime],
            "Ctn", COUNTROWS ( 'Table' )
        )
    VAR tmp1 =
        CALCULATETABLE ( VALUES ( 'Table'[StudentID] ), FILTER ( tmp, [Ctn] >= 2 ) )
    VAR _1 =
        DATE ( YEAR ( [List dates] ), MONTH ( [List dates] ), DAY ( [List dates] ) + TIME ( 8, 0, 0 ) )
    VAR _flg =
        IF ( _1 <= ed && _1 >= sd || _1 - 1 < ed && _1 - 1 >= sd, "TRUE", "FALSE" )
    RETURN
        SWITCH (
            TRUE (),
            _id
                IN tmp1
                && cse = "Chem",
                IF ( _1 <= ed && _1 >= sd || _1 - 1 < ed && _1 - 1 >= sd, "TRUE", "False" ),
            _id
                IN tmp1
                && cse <> "Chem", "False",
            NOT ( _id IN tmp1 ),
                IF ( _1 <= ed && _1 >= sd || _1 - 1 < ed && _1 - 1 >= sd, "TRUE", "FALSE" )
        )
    

    Please refer the attached .pbix file.

     

    Best regards,
    Community Support Team_Binbin Yu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

3 Replies

  • Hi -

    One very way to do this, which I think is a bit crude but might work, is to group on the fields you have shown above in the Power Query editor, and add a row number for the Courses Scheduled subject. A technique for adding an index on groups is here:

     

    https://radacad.com/create-row-number-for-each-group-in-power-bi-using-power-query

     

    Then you would add a condition in your forumula to check to see if the index of the group was "1", which would correspond with "CHEM" in the grouping because it comes ahead of math. It isn't pretty but I think it would work.

     

    Hope this helps,

     

    Peter

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Rt0790 ,

    Please try to create a new column with below dax formula:

    Column =
    VAR ed = [end datetime]
    VAR sd = [start datetime]
    VAR _id = [StudentID]
    VAR cse = [Courses]
    VAR tmp =
        SUMMARIZE (
            'Table',
            'Table'[StudentID],
            'Table'[List dates],
            'Table'[start datetime],
            'Table'[end datetime],
            "Ctn", COUNTROWS ( 'Table' )
        )
    VAR tmp1 =
        CALCULATETABLE ( VALUES ( 'Table'[StudentID] ), FILTER ( tmp, [Ctn] >= 2 ) )
    VAR _1 =
        DATE ( YEAR ( [List dates] ), MONTH ( [List dates] ), DAY ( [List dates] ) + TIME ( 8, 0, 0 ) )
    VAR _flg =
        IF ( _1 <= ed && _1 >= sd || _1 - 1 < ed && _1 - 1 >= sd, "TRUE", "FALSE" )
    RETURN
        SWITCH (
            TRUE (),
            _id
                IN tmp1
                && cse = "Chem",
                IF ( _1 <= ed && _1 >= sd || _1 - 1 < ed && _1 - 1 >= sd, "TRUE", "False" ),
            _id
                IN tmp1
                && cse <> "Chem", "False",
            NOT ( _id IN tmp1 ),
                IF ( _1 <= ed && _1 >= sd || _1 - 1 < ed && _1 - 1 >= sd, "TRUE", "FALSE" )
        )
    

    Please refer the attached .pbix file.

     

    Best regards,
    Community Support Team_Binbin Yu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Rt0790's avatar
      Rt0790
      Regular Visitor

      Hi, I tried this on a similar dataset and I get the following error, "Argument of function 'DATE' has the wrong data type or the result is too large or too small".