'How to JOIN summarized data from two queries into new table in DAX Power BI
I have 2 queries:
Premium:
and Losses:
How can I simply summarize data from Premium query and LEFT JOIN it to summarized data in Losses query using DAX?
In SQL it would be like that:
declare @PremiumTable table (PolicyNumber varchar(50), Premium money)
insert into @PremiumTable values
('Pol1', 100),
('Pol1', 50),
('Pol2', 300),
('Pol3', 500),
('Pol3', 200),
('Pol4',400)
declare @LossesTable table (PolicyNumber varchar(50), Losses money)
insert into @LossesTable values ('Pol1',115),
('Pol1',25),
('Pol2',0),
('Pol3',110),
('Pol3',75)
select p.PolicyNumber,
sum(p.Premium) as Premium,
sum(l.Losses)as Losses
from @PremiumTable p
LEFT JOIN @LossesTable l on p.PolicyNumber = l.PolicyNumber
group by p.PolicyNumber
Result:
I tried using NATURALLEFTOUTERJOIN but it gives me an error:
*An incompatible join column, (''[PolicyNumber]) was detected. 'NATURALLEFTOUTERJOIN' doesn't support joins by using columns with different data types or lineage.*
MyTable =
VAR Premium =
SELECTCOLUMNS(
fact_Premium,
"PolicyNumber",fact_Premium[PolicyNumber],
"Premium", fact_Premium[Premium]
)
VAR Losses =
SELECTCOLUMNS(
fact_Losses,
"PolicyNumber", fact_Losses[PolicyNumber],
"Losses", fact_Losses[PaymentAmount]
)
VAR Result = NATURALLEFTOUTERJOIN(Premium,Losses)
RETURN Result
Solution 1:[1]
There are a few interdependent "bugs" or limitations around the use of variables (VAR) and NATURALLEFTOUTERJOIN which makes this a weird case to debug.
Some notable limitations are:
VAR:
Columns in table variables cannot be referenced via TableName[ColumnName] syntax.
NATURALLEFTOUTERJOIN:
Either:
The relationship between both tables has to be defined before the join is applied AND the names of the columns that define the relationship need to be different.
Or:
In order to join two columns with the same name and no relationships, it is necessary that these columns to have a data lineage.
(I'm a bit confused because the link mentioned do not have a data lineage; while official documentation said only columns from the same source table (have the same lineage) are joined on.)
Come back to this case.
SUMMARIZEshould be used instead ofSELECTCOLUMNSto obtain summary tables forPremiumandLosses, i.e.:Premium = SUMMARIZE( fact_Premium, fact_Premium[PolicyNumber], "Premium", SUM(fact_Premium[Premium]) ) Losses = SUMMARIZE( fact_Losses, fact_Losses[PolicyNumber], "Losses", SUM(fact_Losses[Losses]) )When we apply
NATURALLEFTOUTERJOINto the above two tables, it'll return errorNo common join columns detectedbecause of they have no relationship established.
- To resolve this, we can make use of
TREATASas suggested in this blog post. But to useTREATAS, we have to reference the column names inPremiumandLossestable, so we can't useVARto declare them, but have to actually instantiate them.
To conclude, the solution would be:
- Create calculate tables for
PremiumandLossesas mentioned above.
Use
TREATASto mimic a data lineage and joinPremiumtable withLosses_TreatAsinstead.MyTable = VAR Losses_TreatAs = TREATAS(Losses, Premium[PolicyNumber], Losses[Losses]) RETURN NATURALLEFTOUTERJOIN(Premium, Losses_TreatAs)
Results:
Solution 2:[2]
There's a sleazy hack that can successfully work around this awful limitation (what were the product designers thinking?).
If you add zeros (e.g. + 0) or concatenate an empty string (e.g. & "") to each join column within SELECTCOLUMNS, it breaks out of the data lineage straitjacket and runs the NATURALLEFTOUTERJOIN just using column names.
You can use this in a Measure to run dynamic logic (based on the query context from filters etc), not just while creating a calculated table.
Here's an tweaked version of your code:
MyTable =
VAR Premium =
SELECTCOLUMNS(
fact_Premium,
"PolicyNumber",fact_Premium[PolicyNumber] & "",
"Premium", fact_Premium[Premium]
)
VAR Losses =
SELECTCOLUMNS(
fact_Losses,
"PolicyNumber", fact_Losses[PolicyNumber] & "",
"Losses", fact_Losses[PaymentAmount]
)
VAR Result = NATURALLEFTOUTERJOIN(Premium,Losses)
RETURN Result
H/T to example #7 on this page, which shows this in code (without really explaining it). https://www.sqlbi.com/articles/from-sql-to-dax-joining-tables/#code7
Solution 3:[3]
Hello I suggest you this way:
in PowerQuery, built up a table with policyNumber like that:
- Duplicate Premium table, and remove the premium column on the duplicate. Call it PremiumPol
- Duplicate the Losses table, and remove the losses column on duplicate. Call it LossesPol
- Then use the button Append Query, to Append PremiumPol and LossesPol. Call it policynumber
- Last remove duplicate from the appended tables
- Then click on close and Apply
Check that your model is like that:

Then, to add losses and premium on a policy base is trivial, go on and select a table visual and these fields:

the result is like this:
Hope that helps!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mike Honey |
| Solution 3 | Nelson Gomes Matias |








