Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

SUM column based on two different filters for same unique ID

We record our promo codes for membership and our actual membership purchases as seperate line items in table call 'Activity'. There is no relationship between the two line items, except they're both recorded under the same member ID. Additionally, their member type is based on their latest 'Product_Code' purchased with a valid 'THRU_DATE'. I have some code that creates a measure to pull the MAX 'SEQN' for 'ACTIVITY_TYPE' DUES and DUESOFFER to ensure we're grabbing the most recent dues and dues offer transaction for a member. Sample data below:

IDLAST_NAMEFIRST_NAMEPRODUCT_CODEACTIVITY_TYPETHRU_DATETRANSACTION_DATEUF_1AMOUNTSEQNLatestDUESOFFER_SEQNLatestDUES_SEQN
9E+08NonyaBusinessABCDUESOFFER9/30/2021 0:009/20/2020 0:00Vgabf2020$012324051232405 
9E+08NonyaBusinessABC-1YRDUES9/30/2021 0:009/20/2020 0:00Approved$491232404 1232404
9E+08NonyaBusinessABC-1YRDUES8/31/2020 0:008/1/2019 0:00Approved$431123159  
9E+08NonyaBusinessABCDUESOFFER7/31/2019 0:007/6/2018 0:00WOODBOOK$0969559  
9E+08NonyaBusinessABC-1YRDUES7/31/2019 0:007/6/2018 0:00Approved$43969557  

 

My goal is to figure out total revenue for a given promotion code that we run. So expected results from sample data above is  DuesOfferRevenue = $49. I tried something like this below but getting back no results.

 

 

 

 

 

 

 

 

 

DuesOfferRevenue = 

VAR LatestDuesOfferID =
IF(NOT(ISBLANK('Activity'[LatestDUESOFFER_SEQN])), SELECTEDVALUE ( 'Activity'[ID] ), "NoDuesOffer")

VAR LatestDuesID =
IF(NOT(ISBLANK('Activity'[LatestDUES_SEQN])), SELECTEDVALUE ( 'Activity'[ID] ), "NoDues")

RETURN IF(LatestDuesID = LatestDuesOfferID, SUMX(Activity, Activity[AMOUNT]), BLANK())

 

 

 

 

 

 

 

 

 

 

Any guidance would be greatly appreciated.

Cheers,
Ryan

  • Anonymous's avatar
    Anonymous
    5 years ago

    Not sure if my explanation was poor or it is a difficult problem issue to solve in PowerBI? But this was taking too long and there was a simplier route to a solution outside of PowerBI. My solution was create the Dues and DuesOffer relationship in SQL. Following SQL query gave me the DUES to DUESOFFER record relationship i was seeking and then imported into PowerBI using custom SQL query as 'DuesOffer2Dues' table. Then created a relationship in PowerBI model between 'DuesOffer2Dues' and 'Activity' table using the DuesOffer2Dues.D-SEQN = Activity.SEQN. 

    SELECT DUES.ID, DUES.ACTIVITY_TYPE AS 'D-ACTIVITY_TYPE', DUES.SEQN AS 'D-SEQN',
    	   DUESOFFER.ACTIVITY_TYPE AS 'DO-ACTIVITY_TYPE', DUESOFFER.SEQN AS 'DO-SEQN'
    FROM
           (SELECT ID, ACTIVITY_TYPE, MAX(SEQN) AS SEQN
            FROM	Activity a 
            WHERE	ACTIVITY_TYPE = 'DUESOFFER' AND UF_1 = 'Vgabf2020'
    		GROUP BY ID, ACTIVITY_TYPE
    		) AS DUESOFFER
    LEFT JOIN
           (SELECT ID, ACTIVITY_TYPE, MAX(SEQN) AS SEQN
            FROM       Activity a
            WHERE      ACTIVITY_TYPE = 'DUES'
    		GROUP BY ID, ACTIVITY_TYPE
            ) AS DUES ON DUESOFFER.ID = DUES.ID

     

5 Replies

  • v-deddai1-msft's avatar
    v-deddai1-msft
    Icon for Community Support rankCommunity Support

    Hi Anonymous ,

     

    Would you please explain more about the logic of 49 as total revenue for a given promotion code in your example. I failed to understand what's your need by your description.

     

    Best Regards,

    Dedmon Dai

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi v-deddai1-msft ,

       

      Apologies for missing mark first time around. I'll try to explain the $49 result better here. The data is not great, and there are two problems I need to solve. Problem 1) Identify the relevant "DUESOFFER" and "DUES" activity for each member ID. I've done this by creating a two measures. Each pulls the max SEQN number for each respective ("DUESOFFER" and "DUES") ACTIVITY_TYPE  by ID that has a THRU_DATE > Today. These two measures are in the sample data below, LatestDUESOFFER_SEQN and LatestDUES_SEQN. Problem that I'm trying to solve in this post 2) Sum the AMOUNT for the latest "DUES" activity (LatestDUES_SEQN is "1232404" in sample data) where the corresponding LatestDUESOFFER_SEQN is not null and has a UF_1 = "Vgabf2020". I can't sum on the "DUESOFFER" record AMOUNT, because it's recorded as a $0 value. I need to sum on the LatestDUES_SEQN ("1232404" in the sample data) record AMOUNT. I've reordered the sample data with the SEQN, LatestDUESOFFER_SEQN and LatestDUES_SEQN as this might make more sense. 

      SEQNLatestDUESOFFER_SEQNLatestDUES_SEQNIDLAST_NAMEFIRST_NAMEPRODUCT_CODEACTIVITY_TYPETHRU_DATETRANSACTION_DATEUF_1AMOUNT
      12324051232405 900263805NonyaBusinessABCDUESOFFER9/30/2021 0:009/20/2020 0:00Vgabf2020$0
      1232404 1232404900263805NonyaBusinessABC-1YRDUES9/30/2021 0:009/20/2020 0:00Approved$49
      1123159  900263805NonyaBusinessABC-1YRDUES8/31/2020 0:008/1/2019 0:00Approved$43
      969559  900263805NonyaBusinessABCDUESOFFER7/31/2019 0:007/6/2018 0:00WOODBOOK$0
      969557  900263805NonyaBusinessABC-1YRDUES7/31/2019 0:007/6/2018 0:00Approved$43
      • v-deddai1-msft's avatar
        v-deddai1-msft
        Icon for Community Support rankCommunity Support

        Hi Anonymous ,

         

        Would you please try the below measure?

         

        DuesOfferRevenue = 
        VAR LatestDuesOfferID =
        IF(NOT(ISBLANK('Activity'[LatestDUESOFFER_SEQN])), SELECTEDVALUE ( 'Activity'[ID] ), "NoDuesOffer")
        
        VAR LatestDuesID =
        IF(NOT(ISBLANK('Activity'[LatestDUES_SEQN])), SELECTEDVALUE ( 'Activity'[ID] ), "NoDues")
        
        RETURN IF(LatestDuesID = LatestDuesOfferID, SUMX(FILTER(ALL(Activity),Activity[SEQN] = LatestDuesID), Activity[AMOUNT]), BLANK())

         

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

         

        Best Regards,

        Dedmon Dai