Forum Discussion

mleepin's avatar
mleepin
Helper I
7 years ago
Solved

Sub query, selection criteria

I am hoping for some advice please re: best approaches to handle a scenario that is similar to a sub-query.

 

Assessment table

Date_assessmentAsessment_PeriodScoreCustomer_ID_FK
1/01/2019start11AAA1
1/02/20191 month12AAA1
1/03/20192 month13AAA1
1/01/2019start15AAA2
1/02/20191 month16AAA2
1/03/20192 month17AAA2
1/01/2019start20AAA3
1/02/20191 month21AAA3
1/03/20192 month22AAA3
1/05/2019start11AAA1
1/06/20191 month12AAA1
1/07/20192 month13AAA1
1/04/2019start21AAA6

 

 

Activity table

Date_startDate_finishActivity_nameCustomer_ID_FK
31/12/201828/03/2019Activity_1AAA1
15/12/20182/03/2019Activity_2AAA1
1/04/2019 Activity_1AAA1
31/12/201815/02/2019Activity_3AAA2
15/12/20182/03/2019Activity_5AAA2
1/04/2019 Activity_3AAA6
31/12/201815/02/2019Activity_1AAA3
15/12/20182/03/2019Activity_2AAA3
1/04/2019 Activity_1AAA3
1/05/2019 Activity_1AAA1
1/05/2019 Activity_2AAA1
1/05/2019 Activity_1AAA1

 

Customer table

Customer_ID_PKNameDOB
AAA1cust11/02/2000
AAA2cust21/04/2000
AAA6cust61/05/2000
AAA3cust31/06/2000

 

1- "customer" m-assessments;  1- "customer" m-"activities"

 

The requirement is to create a a filtered "Assessment" result set from the "Assessment" table for Customers whose "Activity" i.e. their "Date_start" and "Date_finish" has been within a selected "Date from" and "Date to".

 

I have been able filter the Customers in the "Activity" table whose activity has been within a selected "Date from" and "Date to" using a variables and a disconneced date slicer/calendar,  however I am not sure of the best approach to also filter the "Assessment" table for these Customers only i.e. to filter Assessments for these customers who have been active in within the selected date range only  - it is necessary to consider that further dax measures would need to be developed for this filtered "Assessment"  result set because this would be considered the base result set to analyze.

 

Any advice would be most appreciated.

 

 

 

 

 

 

 

 

 

  • mleepin's avatar
    mleepin
    7 years ago

    Relationships

     

    Outputs

     

     

    Ok, thank you for your advice.  I have found a way forward that may be suitable (for now).

     

    Relationships have been created as above in the relationships image.  

     

    To create the complex selection parameters to filter only those customers who have been active within a selected dates in the activity table :

     

    1. A ‘date from’ and a ‘date to’ have been captured as variables via a disconnected date slicer and used in measures:

     

    DATE_FROM = VAR date_from = FIRSTDATE(ALLSELECTED(CAL_DATE[DATE])) return date_from

    DATE_TO = VAR date_to = LASTDATE(ALLSELECTED(CAL_DATE[DATE])) return date_to

     

     

    2. Next  another measure was created to only flag /  count  active customers for the selected date range, this has also been used as a visual filter in the outputs image above and also helps identify assessments for only the  active customers in the selected date range:

     

    ACTIVE_CLIENT_FLAG =

    COUNTAX (

        'Activity table',

        IF (

            ISBLANK ( 'Activity table'[Date_finish] ) = TRUE ()

                && ( 'Activity table'[Date_start] >= [DATE_FROM]

                && 'Activity table'[Date_start] <= [DATE_TO] ),

            1,

            //a: above this flags new customers currently enrolled in the selected period

            IF (

                ISBLANK ( 'Activity table'[Date_finish] ) = TRUE ()

                    && ( 'Activity table'[Date_start] < [DATE_FROM] ),

                1,

                //b: above this flags continuing customers not yet completed in the selected period

                IF (

                    ( 'Activity table'[Date_finish] >= [DATE_FROM]

                        && 'Activity table'[Date_finish] <= [DATE_TO] )

                        && ( 'Activity table'[Date_start] >= [DATE_FROM]

                        && 'Activity table'[Date_start] <= [DATE_TO] ),

                    1,

                    //c: above this flags new customers for the selected period who completed in selected period

                    IF (

                        ( 'Activity table'[Date_finish] >= [DATE_FROM]

                            && 'Activity table'[Date_finish] <= [DATE_TO] )

                            && ( 'Activity table'[Date_start] < [DATE_FROM] ),

                        1

                    )

                )

            )

        )

    )

     

     

     

     

     

     

2 Replies

  • v-diye-msft's avatar
    v-diye-msft
    Community Support

    Hi mleepin 

     

    If you’d like to make the filter applied from Activity table to Assessment table, it’s not accessible based on the relationship auto-detected. The limitation- regarding to chains of relationship- is that all the relationship need to be of the same type, and all of them going in the same direction.

     

    You can manage the relationship like this below:

    When you using the calendar as slicer to filter Activity table, the customer table would be filtered as well, and the condition will pass to Assessment table finally.

    Please keep me updated whether it works.

     

    Best regards,

    Dina Ye

    • mleepin's avatar
      mleepin
      Helper I

      Relationships

       

      Outputs

       

       

      Ok, thank you for your advice.  I have found a way forward that may be suitable (for now).

       

      Relationships have been created as above in the relationships image.  

       

      To create the complex selection parameters to filter only those customers who have been active within a selected dates in the activity table :

       

      1. A ‘date from’ and a ‘date to’ have been captured as variables via a disconnected date slicer and used in measures:

       

      DATE_FROM = VAR date_from = FIRSTDATE(ALLSELECTED(CAL_DATE[DATE])) return date_from

      DATE_TO = VAR date_to = LASTDATE(ALLSELECTED(CAL_DATE[DATE])) return date_to

       

       

      2. Next  another measure was created to only flag /  count  active customers for the selected date range, this has also been used as a visual filter in the outputs image above and also helps identify assessments for only the  active customers in the selected date range:

       

      ACTIVE_CLIENT_FLAG =

      COUNTAX (

          'Activity table',

          IF (

              ISBLANK ( 'Activity table'[Date_finish] ) = TRUE ()

                  && ( 'Activity table'[Date_start] >= [DATE_FROM]

                  && 'Activity table'[Date_start] <= [DATE_TO] ),

              1,

              //a: above this flags new customers currently enrolled in the selected period

              IF (

                  ISBLANK ( 'Activity table'[Date_finish] ) = TRUE ()

                      && ( 'Activity table'[Date_start] < [DATE_FROM] ),

                  1,

                  //b: above this flags continuing customers not yet completed in the selected period

                  IF (

                      ( 'Activity table'[Date_finish] >= [DATE_FROM]

                          && 'Activity table'[Date_finish] <= [DATE_TO] )

                          && ( 'Activity table'[Date_start] >= [DATE_FROM]

                          && 'Activity table'[Date_start] <= [DATE_TO] ),

                      1,

                      //c: above this flags new customers for the selected period who completed in selected period

                      IF (

                          ( 'Activity table'[Date_finish] >= [DATE_FROM]

                              && 'Activity table'[Date_finish] <= [DATE_TO] )

                              && ( 'Activity table'[Date_start] < [DATE_FROM] ),

                          1

                      )

                  )

              )

          )

      )