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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mpcarpenter
Helper I
Helper I

measures using data from more than one table - can it be done without a calculated column?

I'm trying to create a measure which combines 'quantity' data from one table with 'price' data looked up from a product table. I know I can create a calculated column in the sales table....

PriceLookedUp = RELATED('models'[price]) 

 

and then make a measure 

TotValueSold = SUMX(sales,sales[PriceLookedUp]*sales[Quantity])

which works OK, but can make a measure that does this directly (ie. without the calculated column)?

 

Apologies if this  is obvious - I'm fairly new to DAX

Michael

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Yes you can!  You were pretty much there on your own too!

 

Try this:

 

TotValueSold =
SUMX ( sales, sales[Quantity] * RELATED ( 'models'[price] ) )

 

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Yes you can!  You were pretty much there on your own too!

 

Try this:

 

TotValueSold =
SUMX ( sales, sales[Quantity] * RELATED ( 'models'[price] ) )

 

thanks - I'd convinced myself I needed something more complex using a CALCULATE function 

 

[In my original database (of pretend car sales) there was no quantity field (I was assuming the quantity was always 1) and I'd tried...

TotValueSold = SUM ( 'models'[price]  )

which didn't work. What does work, following your suggestion is...

TotValueSold = SUMX ( sales, RELATED ( 'models'[price] ) )

which is interesting as I guess the "SUMX(sales," bit forces it to iterate over the rows in sales, even if no actual fields from the sales tabel are used)

 

 

Michael 

Anonymous
Not applicable

Yes, SUMX() does in fact iterate over every row of the sales table...it also generates a row context.  In that row context, there is a value that relates back to the the Product table and has a corresponding Product[Price].  That is how you're able to generate the correct answer.

 

I'm guessing the straight SUM() didn't work because you have other fields from your Sales table that you want to include in your visuals.  The single-direction relationship would ensure that you got weird results if you tried to slice the Sales measure by a column in your Sales table.  The SUMX() measure will solve that problem, and columns from either Product or Sales can be used on your visuals with no problem.

 

If you want to tune your model and make it run faster, I propose the following changes:

  1. Using a merge queries step in your data model, bring the price column into the Sales table.
  2. Call that column "SalesAmount"
  3. Now you can build a simple measure
TotValueSold= SUM(Sales[SalesAmount])

This measure will run a lot faster than your current SUMX() measure, since it doesn't have to call the Formula Engine on every single row to look up what price it should use for the summing operation.

Thanks

Michael

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors