Left join in tabular model

2019-08-29 04:59发布

问题:

I have 2 tables as below.

BILLING
AccountId   RatingDate                  Load
1280582     2018-01-04 15:20:13.000     130.000
421947      2018-01-04 11:44:58.000     176.000
1033717     2018-01-04 10:52:23.000     234.000

RATING
AccountId   RatingDate                  RatingMethod
1280582     2018-01-04 15:20:13.000     A
421947      2018-01-04 11:44:58.000     A
1033717     2018-01-04 10:52:23.000     A

I need to create a measure in Billing table to calculate the sum of Load column. I have the SQL query to perform the same but I am not able to create the measure with the same logic.

SELECT SUM(b.Load) 
FROM Billing b
LEFT JOIN Rating r
ON b.AccountId = r.AccountId
AND b.RatingDate = r.RatingDate                               
WHERE r.RatingMethod = 'A' 

Can someone help me with the dax formula for creating the measure?

回答1:

There are a few ways to approach this including

  1. Do the left join in the query editor.

  2. Create bridge tables or unique indexing columns to relate your two tables.

  3. Write a measure using the LOOKUPVALUE function.

If you're doing a bunch of other stuff, you'll probably want to consider option #1 or #2, but for just this particular measure, I'll show you #3.

LoadSum = CALCULATE(SUM(Billing[Load]),
                    FILTER(Billing,
                           LOOKUPVALUE(Rating[RatingMethod],
                                       Rating[AccountID], Billing[AccountID],
                                       Rating[RatingDate], Billing[RatingDate])
                           = "A"))

The above sums over the [Load] after filtering rows where [RatingMethod] = "A" in the Rating table (and matching on the ID and date columns).


For #1, select your Rating query and click Merge Queries (Home tab).

Select the columns you want to join one (Ctrl+Click in the same order on both of them). Keep the default Left Outer Join and hit OK.

Once they're merged, you'll have a new column named Billing. Click the expand icon in the top right corner of the column and select which columns you want to expand (select Load only).

Once expanded, your table should look like this:

RATING
AccountId   RatingDate            RatingMethod  Load
1280582     2018-01-04 15:20:13   A             130
421947      2018-01-04 11:44:58   A             176
1033717     2018-01-04 10:52:23   A             234