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
Jatin77
Helper I
Helper I

Condition based dax situation

Hello!
I have a following data and I want to write a dax:

IDnamesubjecttest datecheck dateO/p
1AMath11/1/20155/2/2015retake the test
1AScience12/1/2015 retake the test
1AKT12/1/2015 retake the test
1AENVR 5/2/2015retake the test
2BScience11/1/201511/1/2015retake the test
2BMath  retake the test
2BKT  retake the test
2BENVR11/1/201511/1/2015retake the test
2BJBP 5/2/2015retake the test
2BHistory12/1/2015 retake the test
3CMath  test pending
3CScience  test pending
3CKT  test pending
4DMath11/1/20159/1/2015check pending
5EMath12/2/201512/2/2015check pending
6FScience  retake the test
6FMath11/1/2015 retake the test
7GMath  retake the test
7GScience12/1/201512/1/2015retake the test
7GKT11/1/20155/2/2015retake the test
7GENVR  retake the test
7GJBP12/2/20155/2/2015retake the test


In above table O/p column is what I want to achieve and below are he conditions for it:

o/p-Conditions-> cond 1 should be checked first if not met then it should move to 2nd and so on, but if 1st cond is met then it should stop ther only and reflect that result in all cells under o/p column for that emp 
1) If both test and review date is blank for all the subject for that particular employee only then a column cell should show "test pending" 
2) If any of the cell from test date or check date has a date then the whole cells under that employee should turn to "retake the test" 

3) If both the columns i.e. test date and check date has a date and if the check date is less than or equal to test date then it should show "check pending"

 
4) Else "NA" 


Thanks!

1 ACCEPTED SOLUTION

Hi @Jatin77 ,

 

You can try this method:

O/p =
VAR _BlankT =
    CALCULATE (
        MAX ( 'Table'[test date] ),
        FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) )
    )
VAR _BlankC =
    CALCULATE (
        MAX ( 'Table'[test date] ),
        FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) )
    )
VAR _CountT =
    CALCULATE ( COUNTA ( 'Table'[test date] ), ALLEXCEPT ( 'Table', 'Table'[ID] ) )
VAR _CountC =
    CALCULATE ( COUNTA ( 'Table'[check date] ), ALLEXCEPT ( 'Table', 'Table'[ID] ) )
VAR _TotalCountID =
    CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Table', 'Table'[ID] ) )
RETURN
    SWITCH (
        TRUE (),
        _BlankC = BLANK ()
            && _BlankT = BLANK (), "test pending",
        _CountT <> _TotalCountID
            || _CountC <> _TotalCountID, "retake the test",
        _CountT = _TotalCountID
            && _CountC = _TotalCountID
            && 'Table'[check date] <= 'Table'[test date], "check pending",
        BLANK (), "N/A"
    )

The result is:

vyinliwmsft_0-1669708357155.png

Hope this helps you.

Here is my PBIX file.

 

Best Regards,

Community Support Team _Yinliw

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

9 REPLIES 9
ERD
Super User
Super User

@Jatin77 , please, look carefully at your resulting column and the conditions you've provided. It's hard to understand what you want to achieve in the result. E.g.:

If both test and review date is blank for all the subject for that particular employee only then a column cell should show "test pending"

Looking at the sample you've provided, you should not have 'test pending' anywhere since you do not have an employee with ALL blank dates for ALL subjects.

Here is an example of the DAX you can use assuming the conditions you've written are true.

 

o/p result =
VAR current_employee = SELECTEDVALUE ( Table[name] )
VAR not_empty =
    COUNTROWS (
        CALCULATETABLE (
            Table,
            Table[name] = current_employee && Table[test date] <> BLANK () || Table[check date] <> BLANK (),
            ALL ( Table[subject] )
        )
    )
VAR current_check_date = SELECTEDVALUE ( Table[check date] )
VAR current_test_date = SELECTEDVALUE ( Table[test date] )
RETURN
    SWITCH (
        TRUE (),
        not_empty = 0, "test pending",
        current_check_date <> BLANK () && current_test_date <> BLANK ()
            && current_check_date < current_test_date, "check pending",
        current_check_date <> BLANK () || current_test_date <> BLANK (), "retake the test",
        "NA"
    )

 

ERD_0-1669564140897.png

Try to play with SWITCH conditions if the actual conditions are different.

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

Hello @ERD ,

Thank you for the reply. I have updated the data and made slight changes as well. I have also tried the dax which you have mentioned. Sharing the dax and o/p I'm getting:

----------------------------------------------------------------------------------------------------------------------

o/p result =
VAR current_employee = SELECTEDVALUE ( Sheet4[name] )
VAR not_empty =
    COUNTROWS (
        CALCULATETABLE (
            Sheet4,
            Sheet4[name] = current_employee && Sheet4[test date] <> BLANK () || Sheet4[check date] <> BLANK (),
            ALL ( Sheet4[subject] )
        )
    )
VAR current_check_date = SELECTEDVALUE ( Sheet4[check date] )
VAR current_test_date = SELECTEDVALUE ( Sheet4[test date] )
RETURN
    SWITCH (
        TRUE (),
        not_empty = 0, "test pending",
        current_check_date <> BLANK () && current_test_date <> BLANK ()
            && current_check_date < current_test_date, "check pending",
        current_check_date <> BLANK () || current_test_date <> BLANK (), "retake the test",
        "NA"
    )


----------------------------------------------------------------------------------------------------------------------
--> o/p:

Jatin77_0-1669704369297.png


Thanks an Regards,
Jatin77



@Jatin77 , there might be a better way, but you can try this measure:

o/p result = 
VAR current_employee = SELECTEDVALUE ( Sheet4[name] )
VAR wrong_rows =
    CALCULATETABLE (
        ADDCOLUMNS (
            SUMMARIZE ( Sheet4, Sheet4[name], Sheet4[test date], Sheet4[check date] ),
            "i_value",
                SWITCH (
                    TRUE (),
                    ( Sheet4[test date] = BLANK () && Sheet4[check date] <> BLANK () )
                        || ( Sheet4[test date] <> BLANK () && Sheet4[check date] = BLANK () )
                        || Sheet4[check date] > Sheet4[test date], 
                    1,
                    Sheet4[test date] <> BLANK () && Sheet4[check date] <> BLANK ()
                        && Sheet4[check date] <= Sheet4[test date], 
                    2,
                    0
                )
        ),
        Sheet4[name] = current_employee,
        ALL ( Sheet4 )
    )
RETURN
    SWITCH (
        TRUE (),
        SUMX ( wrong_rows, [i_value] ) = 0, "test pending",
        MINX ( wrong_rows, [i_value] ) = 2, "check pending",
        AVERAGEX ( wrong_rows, [i_value] ) > 0 && AVERAGEX ( wrong_rows, [i_value] ) < 2, "retake the test",
        "NA"
    )

I've also added 2 more rows to check more options that might accur:

ERD_0-1669716607238.png

As for the 'Table_5', I just forgot to rename it, sorry.

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

Check out my latest demo report in the data story gallery.

Stand with Ukraine!


Here are official ways you can support Ukraine financially (accounts with multiple currencies):
1) Support the Armed Forces of Ukraine: https://bank.gov.ua/ua/about/support-the-armed-forces
2) Come Back Alive foundation: https://www.comebackalive.in.ua/

Thank you!

Hi @ERD,
Thank you for the reply. Tried this dax but it is not working for me.

Thanks and Regards,
Jatin77

Hi @Jatin77 ,

 

You can try this method:

O/p =
VAR _BlankT =
    CALCULATE (
        MAX ( 'Table'[test date] ),
        FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) )
    )
VAR _BlankC =
    CALCULATE (
        MAX ( 'Table'[test date] ),
        FILTER ( 'Table', 'Table'[ID] = EARLIER ( 'Table'[ID] ) )
    )
VAR _CountT =
    CALCULATE ( COUNTA ( 'Table'[test date] ), ALLEXCEPT ( 'Table', 'Table'[ID] ) )
VAR _CountC =
    CALCULATE ( COUNTA ( 'Table'[check date] ), ALLEXCEPT ( 'Table', 'Table'[ID] ) )
VAR _TotalCountID =
    CALCULATE ( COUNTROWS ( 'Table' ), ALLEXCEPT ( 'Table', 'Table'[ID] ) )
RETURN
    SWITCH (
        TRUE (),
        _BlankC = BLANK ()
            && _BlankT = BLANK (), "test pending",
        _CountT <> _TotalCountID
            || _CountC <> _TotalCountID, "retake the test",
        _CountT = _TotalCountID
            && _CountC = _TotalCountID
            && 'Table'[check date] <= 'Table'[test date], "check pending",
        BLANK (), "N/A"
    )

The result is:

vyinliwmsft_0-1669708357155.png

Hope this helps you.

Here is my PBIX file.

 

Best Regards,

Community Support Team _Yinliw

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

Hi @v-yinliw-msft @ERD @FreemanZ 


I have one more similar kind of a situation like -

I have two more columns named "status" which have yes or no or blanks and "color" which have either yellow/blue

I want to calculate the no of distinct id's for following condition -

Color: yellow
Date: 11/01/2015

And in the status column "yes" should be there for all the subjects, if for that id, any of the subject has any other value then it should not select that id

** The selected distinct id's should have "yes" for all the subjects **

Hi @v-yinliw-msft,

Thank you for the reply. This solution worked on sample data but in original data, there's an additional condition occcured i.e. the o/p column says - "blank" if "test date and check date both are blank" or ""test date and check date both have a date" wherein blanks should only come for the condition if "check date>test date"

Thanks and Regards,
Jatin77

Jatin77
Helper I
Helper I

Thanks @FreemanZ 

I have updated the data now.

FreemanZ
Super User
Super User

the O/P column seems contradictary with the conditions. or?

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.