Forum Discussion
DAX how to count conditionally
Notes:
1. The TestPeriod field is the number type and increases for the next new trial period
Question:
How to write DAX to calculate the unique TestUser number that never happened in previous tests (not including the current selected test period) during a selected test period?
For example:
For TestPeriod 2003, all 4 test users are counted, because there is no pre-test
For TestPeriod 2004, 3 users C, D, E are counted:
- User A has a record of apaso in the previous TestPeriod 2003, not counted
- User B has a record of overdoing in the previous TestPeriod 2003, not counted
- User C, D, E has no pass record in previous test periods, counted (note: user D joined two tests in the same 2004 trial period, but must be counted as a user in the DAX)
For TestPeriod 2005, 2 D and F users are counted
Hi j_w ,
Try the following measure:
Measure = VAR Current_Period = SELECTEDVALUE ( Tests[TestPeriod] ) VAR temp_table_previous = SUMMARIZE ( FILTER ( ALL ( Tests[TestPeriod]; Tests[TestResult]; Tests[TestUser] ); Tests[TestPeriod] < Current_Period && Tests[TestResult] = "PASS" ); Tests[TestUser] ) VAR temp_table_current = SUMMARIZE ( FILTER ( ALL ( Tests ); Tests[TestPeriod] = Current_Period ); Tests[TestUser] ) VAR count_new_user = FILTER ( ADDCOLUMNS ( SUMMARIZE ( Tests; Tests[TestUser]; Tests[TestPeriod] ); "TTT"; CALCULATE ( COUNT ( Tests[TestUser] ); Tests[TestPeriod] < Current_Period ) ); [TTT] = BLANK () ) RETURN COUNTROWS ( count_new_user ) + COUNTROWS ( INTERSECT ( temp_table_previous; temp_table_current ) )This is giving me the correct result however with such a small number of datapoints there can be an error.
3 Replies
- MFelix
Super User
Hi j_w ,
Try the following measure:
Measure = VAR Current_Period = SELECTEDVALUE ( Tests[TestPeriod] ) VAR temp_table_previous = SUMMARIZE ( FILTER ( ALL ( Tests[TestPeriod]; Tests[TestResult]; Tests[TestUser] ); Tests[TestPeriod] < Current_Period && Tests[TestResult] = "PASS" ); Tests[TestUser] ) VAR temp_table_current = SUMMARIZE ( FILTER ( ALL ( Tests ); Tests[TestPeriod] = Current_Period ); Tests[TestUser] ) VAR count_new_user = FILTER ( ADDCOLUMNS ( SUMMARIZE ( Tests; Tests[TestUser]; Tests[TestPeriod] ); "TTT"; CALCULATE ( COUNT ( Tests[TestUser] ); Tests[TestPeriod] < Current_Period ) ); [TTT] = BLANK () ) RETURN COUNTROWS ( count_new_user ) + COUNTROWS ( INTERSECT ( temp_table_previous; temp_table_current ) )This is giving me the correct result however with such a small number of datapoints there can be an error.