The request

From Laura Simmons, Category Finance Partner
Your goal
Add a calculated column called Line Cost to order_items: the line's quantity times its product's unit_cost, fetched from products with RELATED. The page is filtered to completed orders, and the matrix shows the given Cost measure (SUM of your column) by products[category].
- Add the column
Line Costtoorder_itemsnot done yet - The Total row right toonot done yet
Your calculated column
- Visual
- A matrix: rows = your new column on order_items, values = [Cost]
- Slicers
- orders[status] = completed
- Measures in the report
Cost = SUM(order_items[Line Cost])
Data pane: tables and columns
- order_items order_item_id, order_id, product_id, quantity, unit_price, discount_pct, line_total
- orders order_id, customer_id, order_date, status, channel, shipping_fee, coupon_code
- products product_id, name, category, subcategory, unit_price, unit_cost, launched_on, is_active
- customers customer_id, first_name, last_name, email, city, state, signup_date, segment, referral_source
- returns return_id, order_item_id, returned_on, reason, refund_amount
- Date Date, Year, Quarter, Month Number, Month, Year Month, Year Quarter, Day, Weekday Number, Weekday
Loading the editor…
Reference solution
| order_item_id | order_id | product_id | quantity | unit_price | discount_pct |
|---|---|---|---|---|---|
| 1 | 1,001 | 121 | 9 | 31.99 | 0 |
| 2 | 1,001 | 154 | 3 | 11.99 | 0 |
| 3 | 1,002 | 154 | 4 | 11.99 | 0 |
| 4 | 1,002 | 194 | 1 | 149 | 0 |
| 5 | 1,002 | 193 | 2 | 19.99 | 0 |
| 6 | 1,002 | 196 | 2 | 25.99 | 0 |
| 7 | 1,002 | 156 | 1 | 53.99 | 0 |
| 8 | 1,003 | 134 | 1 | 92.99 | 10 |
Mote checks your result
Press Check. Mote fills in your answer and compares it with what the report needs.
What Laura gets, and why
The lesson
The quantity is on the order line and the cost is on the product. A calculated column on
order_items can reach across the relationship and bring the cost over, one line at a time.
What you are working in
This is a practice report built for this site; Power BI Desktop runs only on Windows. Your
column goes in order_items. The page is filtered to completed orders, and the matrix shows
Cost = SUM(order_items[Line Cost]) by products[category], so it fills in as soon as your
column exists.
In Power BI Desktop: select order_items in the Data pane, Table tools → New column, type
the formula and press Enter. RELATED only works if the relationship from order_items to
products exists in Model view.
RELATED: from the many side to the one side
In a calculated column on order_items, each row is one order line. Its own columns are
available directly; columns of other tables are not, because DAX doesn’t know which of their
rows you mean. RELATED tells it: follow this row’s relationship to the “one” side and read the
column there.
Line Cost = order_items[quantity] * RELATED(products[unit_cost])
For a line with product_id 17 and quantity 2, RELATED finds product 17 and returns its unit
cost; the line cost is twice that. 30,000 lines, 30,000 lookups, done once at refresh.
RELATED works in one direction: from the many side to the one side, where there is exactly one
row to read. Going the other way, from a product to its many lines, needs RELATEDTABLE, which
returns a table you then aggregate.
Blank costs
Nine products have no unit_cost in the data. For their lines, RELATED returns a blank, and a
number multiplied by blank is blank in DAX, so those lines have no cost and SUM skips them. That
is honest: the cost is unknown, not zero. A finance report should say which products are missing
a cost rather than quietly count them as free.
Column, then measure
The column does the per-line arithmetic; the measure aggregates it. You could also skip the
column with an iterator, SUMX(order_items, order_items[quantity] * RELATED(products[unit_cost])),
which computes the same thing inside a measure without storing a column. You’ll write those in
the advanced DAX chapter.
Common mistakes
- No RELATED.
order_items[quantity] * products[unit_cost]fails: in a row oforder_itemsthere is no single product cost to read. - The selling price instead of the cost.
order_items[unit_price]is what the customer paid. - RELATED the wrong way. In a column on
products,RELATED(order_items[quantity])fails: a product has many lines.
Lantern lit!
Practised: calculated columnrelatedrelationshiprow contextcost of goods sold
- Solved
- No hints
- First run
