Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Anonymous
Not applicable

Using COALESCE breaks joins

Hi,

 

I've a stupid problem: when I try to use COALESCE on a mesure in order to force a value when there is no data, I end up with a cartesian product when there are multiple tables linked.

 

Simple example :

Table Country :

IDName
ACountry A
BCountry B
CCountry C
DCountry D

 

Table Capital :

IDCapital
ACapital of A
BCapital of B
CCapital of C

 

Table Import :

IDProductQuantity
AX50
AY160
AZ70
CX250
CZ10

 

Here is the model generated by PowerBI :

lennelei_1-1653383299046.png

 

Here is the a table in the report with ID, Capital, Name and Qty (which is correct) :

lennelei_2-1653383379854.png

 

I'd like to have default values when there is no data (not necessary zero but I'll use zero in this example), my first idea was to create a measure with COALESCE :

 

 

 

ImpQty = COALESCE(SUM('Import'[Qty]), 0)

 

 

 

But if I add this measure to the table above, I end up with a cartesian product with the Capital table :

lennelei_3-1653383506821.png

Setting the relation between Import and Country as double-sided doesn't change this.

 

I presume there is a simple solution but couldnt' find it... Any idea please?

 

Thanks!

1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

I think your table visual should be expanded by relationship. I suggest you to try to use virtual table in your code.

New Measure:

ImpQty = 
VAR _SUMMARIZE =
    ADDCOLUMNS (
        Country,
        "Capacity", RELATED ( Capital[Capital] ),
        "Qty", CALCULATE ( SUM ( 'Import'[Qty] ) ) + 0
    )
RETURN
    SUMX ( _SUMMARIZE, [Qty] )

Result is as below.

RicoZhou_0-1653643064790.png

Best Regards,
Rico Zhou

 

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

View solution in original post

10 REPLIES 10
CNENFRNL
Community Champion
Community Champion

DAX is simple, but NOT easy. Even a seemly-easiest measure like yours involves some intricacies under the hood. In your case, no joins were broken at all. It's just the instrinsic mechanism of filter mechanism.

  1. whatever column you put in the viz, it acts as a filter; (a side note: the blank cell of Capital[Capital] kicks in due to Referential Integrety Violation in relation to relationship Country[ID] 1:1 Capital[ID])
  2. filters propagate along the direction of relationship
  3. a measure evaluates under the stacked effects of all possible filters
  4. SUM/MAX/MIN(_table[Column]) ... are sytanctic sugars for SUMX/MAXX/MINX(_table,_table[Column])

after all these complex preceding steps, it finally arrives to evaluate SUMX('Import','Import'[Qty]). When filtered 'Import' is empty, the measure evaluates to empty and it's removed from the viz automatically by the engine.

But you use COALESCE to return 0 by force.

CNENFRNL_0-1653396300615.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Wow! what an explanation @CNENFRNL !

Could you please share more such articles or tips to make my understanding onthis nature of DAX processing.

I consider myself as a sql developer trying to understand DAX , who fails most of the time.

Anonymous
Not applicable

I never thought DAX was easy, on the contrary 😕 

 

As Capital doesn't have as many lines as Country, should the relationship be *:1 ? (I can have no Capital for one Country). I must admit that PowerBI suggested the 1:1 and I didn't think about it.

I did try that and it actually removes the rows with blank Capital but I still have 3 rows par Country.

 

I think I understand *why* I have this result (even if I use "breaks joins" in the title which is not exactly what's done).

But I don't know *how* to deal with this. 

I could use a measure like this one :

 

ImpQty = IF(COUNTROWS(Capital)=0, BLANK(), COALESCE(SUM('Import'[Qty]), 0))

 

It will return BLANK when the SUM is BLANK "because" of a missing Capital but in a more complex table, it will be a nightmare to setup. And Country D will not be displayed as it has no capital.

Hi @Anonymous ,

 

I think your table visual should be expanded by relationship. I suggest you to try to use virtual table in your code.

New Measure:

ImpQty = 
VAR _SUMMARIZE =
    ADDCOLUMNS (
        Country,
        "Capacity", RELATED ( Capital[Capital] ),
        "Qty", CALCULATE ( SUM ( 'Import'[Qty] ) ) + 0
    )
RETURN
    SUMX ( _SUMMARIZE, [Qty] )

Result is as below.

RicoZhou_0-1653643064790.png

Best Regards,
Rico Zhou

 

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

SpartaBI
Community Champion
Community Champion

@Anonymous try this:

ImpQty = COALESCE(CALCULATE(SUM('Import'[Qty])), 0)

 

Anonymous
Not applicable

Thank you for the suggestion but I got the same result 😞

I attached the pbix in the thread

https://filetransfer.io/manage-package/CZeGEQC0

@Anonymous you know what, before solving this with maybe a not necessary DAX complex. Why do you have table country and table Capital as two different tables?
If you have a 1:1 relationship between 2 tables this is 99% means they need to be in 1 table. Is there a reason you didn't do that? If not, this is a very important rule to follow.
Also, I wasn't able to download the file you shared. I can hande with the tabled you put in the first message, but first just let me know my question about the 1:1 split you did. 

Anonymous
Not applicable

Actually, it's because the original model is far more complex and my first idea was to keep the tables separated in order to keep the data in their respective table : my data is about employee ; I have data regarding formation, rh managment, etc.

It's true that some of them have a 1:1 relationship, I didn't think it could be an issue.

I also didn't knew 1:1 relationship was something to avoid!

 

Moreover, I also have others 1:N relationships and I'm not sure I'll not have the same issue if I use COALESCE in this case.

 

I'll have a look at my example and see if I have the same issue with 1:N relationship tables.

@Anonymous it's ok, it's not neccesay the reason, but you need to merge this situations to 1 table unless there are security constraints or maybe one in direct query and the other in import. 1;m is what you want to have, but 1;1 is just mean it should be the same table. Good model= less complex dax to achieve the same result

@Anonymous haha ok, I just tried a quick win. Will take your tables and reproduce in a PBIX and comeback with an answer

Helpful resources

Announcements
March PBI video - carousel

Power BI Monthly Update - March 2025

Check out the March 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors
Top Kudoed Authors