Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Filter context not working as expected

Hi all, I have a table with customers IDs, and their first, second and third car purchase dates. With the below formula, what I am trying to do is at any given date, calculate the number of customers, who has ONLY two cars. It is basically count of customers who have purchased their first car, and not yet their second car (So the date should be greater than their first car purchase date and less than their second car purchase date.) This all works fine without any relationship. However when I create a relationship with another table based on customer ID, then somehow the filter context applies, and my numbers are messed up (It has to do something with the dates). Would anyone know how can I rectify this formula to accommodate relationships? It feels like I should put an allexcept somewhere in the formula but I could not figure it out

 

Two_cars_customers_count =
VAR tmpEff = ADDCOLUMNS('Device PURCHASE data',"Effective Date",IF(ISBLANK([THIRD_PURCHASE_DATE]),TODAY(),[THIRD_PURCHASE_DATE]))
VAR tmpTable =  
SELECTCOLUMNS(
    FILTER(
        GENERATE(
            tmpEff,
            'Date_Lookup'
        ),
        [Date] >= [SECOND_PURCHASE_DATE] &&
        [Date] < [Effective Date] &&
        [SECOND_PURCHASE_DATE]<> BLANK()
    ),
    "ID",[Customer_ID],
    "Date",[Date]
)
VAR tmpTable1 = GROUPBY(tmpTable,[ID],"Count",COUNTX(CURRENTGROUP(),[Date]))
RETURN  IF(ISBLANK(COUNTROWS(tmpTable1)),0, COUNTROWS(tmpTable1) )

 

Screenshot of my graph without any relationship (Working as expected)

 

 

I am putting the below relationship

 

 

Now my graph looks like this (Not expected), most likely due to context filter. (I did not change anything else)

 

 

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Anonymous ,

    Please have a try.

    Two_cars_customers_count =
    VAR tmpEff =
        ADDCOLUMNS (
            'Device PURCHASE data',
            "Effective Date", IF ( ISBLANK ( [THIRD_PURCHASE_DATE] ), TODAY (), [THIRD_PURCHASE_DATE] )
        )
    VAR tmpTable =
        SELECTCOLUMNS (
            FILTER (
                GENERATE ( tmpEff, 'Date_Lookup' ),
                [Date] >= [SECOND_PURCHASE_DATE]
                    && [Date] < [Effective Date]
                    && [SECOND_PURCHASE_DATE] <> BLANK ()
            ),
            "ID", [Customer_ID],
            "Date", [Date]
        )
    VAR tmpTable1 =
        GROUPBY (
            ALLEXCEPT ( tmpTable, tmpTable[ID] ),
            tmpTable[ID],
            "Count", COUNTX ( CURRENTGROUP (), [Date] )
        )
    RETURN
        IF ( ISBLANK ( COUNTROWS ( tmpTable1 ) ), 0, COUNTROWS ( tmpTable1 ) )
    

     

    How to Get Your Question Answered Quickly 

     

    If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

     

    Best Regards
    Community Support Team _ Rongtie

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

     

     

     

     

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    Please have a try.

    Two_cars_customers_count =
    VAR tmpEff =
        ADDCOLUMNS (
            'Device PURCHASE data',
            "Effective Date", IF ( ISBLANK ( [THIRD_PURCHASE_DATE] ), TODAY (), [THIRD_PURCHASE_DATE] )
        )
    VAR tmpTable =
        SELECTCOLUMNS (
            FILTER (
                GENERATE ( tmpEff, 'Date_Lookup' ),
                [Date] >= [SECOND_PURCHASE_DATE]
                    && [Date] < [Effective Date]
                    && [SECOND_PURCHASE_DATE] <> BLANK ()
            ),
            "ID", [Customer_ID],
            "Date", [Date]
        )
    VAR tmpTable1 =
        GROUPBY (
            ALLEXCEPT ( tmpTable, tmpTable[ID] ),
            tmpTable[ID],
            "Count", COUNTX ( CURRENTGROUP (), [Date] )
        )
    RETURN
        IF ( ISBLANK ( COUNTROWS ( tmpTable1 ) ), 0, COUNTROWS ( tmpTable1 ) )
    

     

    How to Get Your Question Answered Quickly 

     

    If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

     

    Best Regards
    Community Support Team _ Rongtie

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