Forum Discussion

gracemanasseh's avatar
gracemanasseh
Frequent Visitor
8 years ago

DAX formula between 2 tables

Hello,

 

I am trying to move a dashboard from Relational DB to Power BI and would really appreciate some help in the below case.

Note that I am restricted to the SQL Server SSAS Tabular 2012 and hence have some limitations on the available functions. 

 

I have 2 Fact Tables:

 

- CustomerTable 1 = Contains list of Customers with CustomerID, ProductCode, PolicyID, IssueDate

- CustomerTable 2 = Contains list of Customers with CustomerID, ProductCode, PolicyID, IssueDate

(note that the 2 tables have different content although similar structure. The ProductCode and IssueDate have a relationship with 2 other tables ProductFilter and DimDate)

 

Filters created on the Dashboard based on the ProductFilter and DimDate dimensions:

- Product 

- From Date and To Date

 

Output needed:

To count the Customers from CustomerTable1 who did a purchase of the selected Products in the filter with Issue Date between From Date and To Date. And at the same time have a purchase in CustomerTable2 with same CustomerID, but different ProductCode (that is not selected in the filter) and having Issue Date < From Date.

Note that the output is to return the list of products from Table2 with the count of CustomerIDs meeting the above query.

 

- The query is to resembles the below in T-SQL Query:

 

SELECT T2.ProductCode, COUNT(DISTINCT T1.CustomerID) CrossSellNb
FROM CustomerTable 1 as T1
INNER JOIN
    (SELECT CustomerID,ProductCode
   FROM CustomerTable2
   WHERE Issuedate < @FromDate
   AND ProductCode not in (@Product)
   GROUP BY CustomerID, ProductCode
)T2
ON T2.CustomerID = T1.CustomerID
WHERE T1.Issuedate BETWEEN @FromDate AND @ToDate
   and T1.ProductCode in (@Product)
GROUP BY T2.ProductCode

 

The below is the DAX I was trying to create on CustomerTable1 fact table, but with no luck. I am not able to drag the Product Code from CustomerTable2 and use the measure I create on CustomerTable1. Besides the DAX below is not returning the correct result.

 

Result:=CALCULATE(DISTINCTCOUNT(CustomerTable1[CustomerCode]), FILTER(ALL(CustomerTable1[CustomerCode]),CONTAINS(CALCULATETABLE(CustomerTable2, Filter(ALL(CustomerTable2), (CustomerTable2[Issuedate]<Min(DimDate[Date]) ) ),CustomerTable2[CustomerCode],CustomerTable1[CustomerCode])), VALUES(CustomerTable1[CustomerCode]))

 

Appreciate any guidance,

Thank You

Grace

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    gracemanasseh,

    Do you connect to SQL Server database or SSAS database in Power BI Desktop? Could you please share dummy data of your facts table and post expected result here?

    Regards,
    Lydia