Forum Discussion

brdrok's avatar
brdrok
Helper I
2 years ago
Solved

Need help converting SQL query into DAX

I have the following SQL query that I would like to convert into DAX but keep running into stumbling blocks   SELECT DISTINCT InvestorCompanyCode into #investmentVehicleCodes FROM StdParamete...
  • Sahir_Maharaj's avatar
    2 years ago

    Hello brdrok,

     

    Here's a suggested DAX translation of your SQL code:

    EVALUATE
    
    VAR distinctCompanyCodes =
        CALCULATETABLE (
            VALUES ( StdParamNRT[InvestorCompanyCode] ),
            StdParamNRT[CompanyGroup] = "CWT"
        )
    
    VAR payments =
        CALCULATETABLE (
            'PowerBI vFactGL',
            'PowerBI vFactGL'[T2_SourceCode] = "PAYMENTJNL",
            'PowerBI vFactGL'[t2_g_laccountno_] = "20200",
            'PowerBI vFactGL'[T2_PostingDate] >= DATEVALUE ( "4/1/2023" ),
            'PowerBI vFactGL'[T2_PostingDate] <= DATEVALUE ( "4/30/2023" ),
            distinctCompanyCodes
        )
    
    VAR expenses =
        CALCULATETABLE (
            'PowerBI vFactGL',
            'PowerBI vFactGL'[T2_PostingDate] >= DATEVALUE ( "4/30/2022" ),
            'PowerBI vFactGL'[t2_g_laccountno_] = "52300",
            'PowerBI vFactGL'[T2_SourceCode] = "GENEXP",
            distinctCompanyCodes
        )
    
    VAR results =
        FILTER (
            payments,
            VAR currentDescription = 'PowerBI vFactGL'[T2_Description]
            VAR currentAmount = 'PowerBI vFactGL'[T2_Amount(CCY)]
            VAR currentSourceName = 'PowerBI vFactGL'[SourceName]
            VAR currentCompanyCode = 'PowerBI vFactGL'[T2_CompanyCode]
            RETURN
                COUNTROWS (
                    FILTER (
                        expenses,
                        'PowerBI vFactGL'[T2_Description] = currentDescription
                            && 'PowerBI vFactGL'[T2_Amount(CCY)] = currentAmount
                            && 'PowerBI vFactGL'[SourceName] = currentSourceName
                            && 'PowerBI vFactGL'[T2_CompanyCode] = currentCompanyCode
                    )
                ) > 0
        )
    
    RETURN
        results

    The key is in the results variable where for each record in payments, we are checking if a corresponding record exists in expenses that matches on multiple columns. Should you require my further assistance, please do not hesitate to reach out to me.