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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
msprog
Helper II
Helper II

RELATEDTABLE and distinct

HI,
I need some help here.

I have a simple powerbi model with 2 tables, Product and Sales.

 

The Sales table has fields: OrderNo, LineNo, ProductKey , Qty, etc
The Product table has ProductKey, ProductName, Description.

There is a 1 to Many relationship between Product and Sales table.

 

Now, On the Product table, in powerBI, i want to have a calculated Column called NumberofOrders.
NumberofOrders is the distinct count orders in which the product has been sold. Remeber a Product may appear more than one line items within an order, hence only the distinct count of orders.

 

How can i achieve this? I know we need to use RELATEDTABLE.

I am trying something like

=COUNTROWS(distinct(RELATEDTABLE(FactInternetSales)))

but this doesn't work/

Hww do i use a distinct in this case?
Please help
thanks

1 ACCEPTED SOLUTION
vik0810
Resolver V
Resolver V

Hi @msprog,

 

you can use following DAX:

 

NumberOfOrders = CALCULATE(DISTINCTCOUNT(sales[OrderNo]), RELATEDTABLE(sales))

View solution in original post

2 REPLIES 2
vik0810
Resolver V
Resolver V

Hi @msprog,

 

you can use following DAX:

 

NumberOfOrders = CALCULATE(DISTINCTCOUNT(sales[OrderNo]), RELATEDTABLE(sales))

Hi @vik0810

I have a followup question on this if it is ok. 

 

NumberOfOrders = CALCULATE(DISTINCTCOUNT(sales[OrderNo]), RELATEDTABLE(sales))

The solution works fine but as i am reading more about PowerBI , a question arose  on the use of RelatedTable function in this solution, which i am hoping the  you/community can help clarify.  I am wondering why the below code is not working:

 

NumberOfOrders = CALCULATE(DISTINCTCOUNT(sales[OrderNo]), sales)

 

why do we need the RelatedTable when we are using Calculate?

 

THis is my thinking:

I read that the Calculate function converts Row into a filter context and the filter context filters the whole model based on the realtionships.  So we have a 1 to many relationship between Product and Sales.  we are creating a calculated column in Product.  So there is a row context.  Now the use of Calculate should convert the row context into filter, which means the Sales table should get filtered, automatically?  Please help clarify

 

thanks

 

 

 

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.