Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Group by with join on 2 columns

Hi, 


What's the best way to achieve the below in PowerBI (using merge is considered). Effectively using aggregates with group by and joining across 2 tables (Contracts and Receipts).

 

SELECT c.Age, c.ProductionDate, SUM(r.HT)
FROM Contracts c
JOIN Receipts r ON c.ContractRef =r.ContractRef AND EOMONTH(c.ProductioNDate) = EOMONTH(r.TransactionDate)
WHERE c.Status = 'AFN'
AND c.EffectiveDate <= c.ProductionDate
AND r.Status = 'P'
GROUP BY c.AGE, c.ProductionDate

 

Thanks

  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi Anonymous ,

     

    Please refer to below measure and see if the result achieve your expectation.

    Measure = 
    VAR a =
        CALCULATE (
            SUM ( Transactions[HT] ),
            FILTER (
                Transactions,
                EOMONTH ( Transactions[TransactionDate], 0 )
                    = EOMONTH ( MAX ( Contracts[ProductionDate] ), 0 )
                    && Transactions[Status] = "P"
                    && MAX ( Contracts[ETAT] ) = "AFN"
                    && MAX ( Contracts[EffectiveDate] ) <= MAX ( Contracts[ProductionDate] )
            )
        )
    RETURN
        IF ( ISBLANK ( a ), "-", a )

     

    Best Regards,

    Jay

    Community Support Team _ Jay Wang

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

5 Replies

  • JarroVGIT's avatar
    JarroVGIT
    Icon for Resident Rockstar rankResident Rockstar

    It would really help not to give some SQL statement but actually to give the table structure, some sample data and the desired outcome based on the sample data you provide. That way, we can help you out on this issue 🙂

     

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    You could try creating relationship between "Contracts" and "Receipts", then create a measure like below:

    CALCULATE(SUM(HT),FILTER(ALLEXCEPT('Contracts','Contracts'[AGE],'Contracts'[ProductionDate]),"Conditions"))

    The measure above is for reference only, cannot be verified without data support.

    Please share some sample data to us if you don't have any Confidential Information.

     

    Best Regards,

    Jay

    Community Support Team _ Jay Wang

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for your replies. Please refer to sample data below:

       

      Contracts (c)

      ContractRefETATEffectiveDateProductionDateAGE
      ABCAFN04/03/201401/06/20193
      DEFAFN16/06/201001/06/20192
      GHEAFN23/11/201102/07/20192
      HIJDEF01/05/201701/06/20192
      KLMAFN09/09/201101/06/20191

       

      Transactions

      ContractRefHTTransactionDateStatus
      ABC   154.7905/06/2019P
      ABC   427.8402/06/2019P
      GHE   278.6605/07/2019P
      HIJ   363.3504/06/2019P
      KLM   225.4405/06/2019P

       

      Result

      AgeReportingDateSUM HT
      101/06/2019     225.44
      201/06/2019               -  
      201/07/2019     278.66
      301/06/2019     582.63

       

      Appreciate if the solution could be displayed as the table above (i.e. not simply a dax expression to calculate the aggregate?)

       

      Thanks in advance. 

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous ,

         

        Please refer to below measure and see if the result achieve your expectation.

        Measure = 
        VAR a =
            CALCULATE (
                SUM ( Transactions[HT] ),
                FILTER (
                    Transactions,
                    EOMONTH ( Transactions[TransactionDate], 0 )
                        = EOMONTH ( MAX ( Contracts[ProductionDate] ), 0 )
                        && Transactions[Status] = "P"
                        && MAX ( Contracts[ETAT] ) = "AFN"
                        && MAX ( Contracts[EffectiveDate] ) <= MAX ( Contracts[ProductionDate] )
                )
            )
        RETURN
            IF ( ISBLANK ( a ), "-", a )

         

        Best Regards,

        Jay

        Community Support Team _ Jay Wang

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