Forum Discussion

smartpug's avatar
smartpug
New Member
3 years ago
Solved

Calculating values based on two tables

Hi all,

I am a super beginner and have the following case:

Problem: 

We have two tables: Products and Orders. They have a One-To-Many relationship so one Product might be in a few orders - a classic case. Product has a 'Price' column and an important note is that it's not a mandatory field - some products have the price and some products have it blank. I want to calculate a total value for each product based on the row count of related rows from the 'Orders' table. Formula like TotalValue = Product.Price * CountRows(Orders) where Orders.Product = Product

 

Example:
Products:

ProductPrice
Pencil2.00
Crayonnull
Backpack10,00

 

If in Orders we have two Pencils - we will get a total of 4.00, if we have 5 Backpacks - we get a total of 50, and Crayon is skipped.

These values will be displayed to the user as figures for each product having a price and also as a sum of all totals.

 

I bet it's super easy but I'm also an absolute beginner in Power BI and DAX so I would be really thankful for any help!

  • maybe you want a measure, instead of calculated column

    NewMeasure=SUMX(Products,COUNTROWS(RELATEDTABLE(orders))*Products[Price])

5 Replies

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    if you are adding calculated column in Products table, try this code

    NewColumn=COUNTROWS(RELATEDTABLE(Orders))*Products[Price]

  • Hi,
    Thanks for answering. Tried this and unfortunately, I get this error:
    "A single value for column 'Price' in table 'Products' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."

     

    The problem is - when I was using aggregation for the 'Price' - e.g. Max('Price') - it was getting the highest 'Price' from the table and just multiplied it by all rows. 

    • wdx223_Daniel's avatar
      wdx223_Daniel
      Community Champion

      maybe you want a measure, instead of calculated column

      NewMeasure=SUMX(Products,COUNTROWS(RELATEDTABLE(orders))*Products[Price])

      • smartpug's avatar
        smartpug
        New Member

        Yes, this is exactly what I need, thanks! Is it possible to modify this DAX to filter RELATEDTABLE(orders) to include only orders.DeliveryType = "Pickup"?