Skip to content
Reading settings
ExcelAdvanced~60 min practiceLesson 23 of 34

Clean sales with Power Query and add model measures

Refresh a cleaning query, then calculate DAX measures in Windows Power Pivot.

Content reviewed · How we check lessons

Loading your progress…

In this lesson

Load clean sales rows, relate products and check regional totals before and after refresh.

Example

One cleaning query for the next sales update

Create RawSales with OrderID, ProductID, Quantity, UnitPrice and Region: O1 P100 2 30 " west "; O2 P200 1 180 "EAST"; O3 P100 3 30 "West". Create Products with ProductID and Product: P100 Keyboard; P200 Monitor. The three line amounts are 60, 180 and 90, totaling 330. The quotes show surrounding spaces in Region; do not enter the quotes.

How it works

Power Query saves the cleaning steps and applies them again on refresh. Edit the source records or query steps rather than repairing the loaded output by hand.

A DAX measure calculates within a Data Model report’s filters. Put it in Power Pivot, not a worksheet cell. The same measure can return a regional or product total depending on the report layout.

Steps

  1. Convert the sales source to a table named RawSales. In a supporting edition, choose Data > Get Data > From Table/Range, or the available table query command, to open Power Query Editor.

  2. Set OrderID and ProductID to Text, Quantity to Whole Number and UnitPrice to a decimal numeric type. Select Region and apply Trim, then Capitalize Each Word. The results should be West, East and West.

  3. Add a custom column named LineAmount with the expression below. Check 60, 180 and 90 and sum the output to 330.

    [Quantity] * [UnitPrice]
  4. Name the query SalesModel and use Close & Load to place its output in a worksheet table. On Mac or Web, use the load and refresh options supported for your edition and source. You can continue with the worksheet result if Power Pivot authoring is unavailable.

  5. For the Windows Power Pivot extension, make Products a table and choose Power Pivot > Add to Data Model. Under Data > Queries & Connections, right-click SalesModel > Load To and select the Data Model option; you can retain its worksheet output too. Open Power Pivot > Manage > Diagram View and relate Products[ProductID] to SalesModel[ProductID]. Check that each Products key occurs once.

  6. Choose Power Pivot > Measures > New Measure. Set Measure Name to Total Sales and Formula to =SUMX(SalesModel,SalesModel[Quantity]*SalesModel[UnitPrice]). Use Check Formula and place the measure in a model PivotTable by Region.

  7. Create West Sales with Formula =CALCULATE([Total Sales],SalesModel[Region]="West"). It replaces a filter on the same SalesModel[Region] column, so a report selection on that field will not narrow this measure to another region.

  8. Create Region Share with Formula =DIVIDE([Total Sales],CALCULATE([Total Sales],ALL(SalesModel[Region]))). Put SalesModel[Region] in Rows and check West 150/330 and East 180/330.

  9. Add O4 P200 2 180 West to RawSales. Refresh the query, model and reports, then compare the record count and totals. The new line adds 360; leave the loaded output to the query.

Try it yourself

  1. Inspect the original model by Product and explain why Keyboard totals 150 while Monitor totals 180. On editions without Power Pivot, reproduce those product totals with a worksheet PivotTable and identify that as the alternative method.
  2. After the added row, test Region Share under the fact Region field and compare the two rows with the grand total. Keep an audit note describing the source record count, query output count and refresh date.
Show expected result
  • The original query has three rows totaling 330; original West is 150 and East 180. Model product totals are Keyboard 150 and Monitor 180.
  • After O4, four rows total 690, with West 510 and East 180. Region Share totals 100 percent across those two region rows, allowing for displayed rounding.

If something goes wrong

A join or relationship gives an unexpectedly large total.
Check Products[ProductID] for duplicate keys and inspect the relationship or join. The products side should identify one product per key in this model.
The DAX formula gives a name error in a worksheet cell.
Create it under Power Pivot > Measures > New Measure using the stated table names. A worksheet formula and a model measure run in different contexts.
The query stops refreshing after the workbook moves.
Inspect the source step, connector and credentials for the current platform. This exercise uses workbook tables; an external path or connector needs its own setup. Keep credentials out of shared workbooks.

Quick check

Try these questions after the exercise. Check the result in your app before marking the lesson complete.

1. What is the original Total Sales measure result?
2. Where should the supplied DAX measure be created?
3. What is West after adding O4?

Choose an answer for all 3 questions to check your understanding.

Optional: try it with Copilot

If you have Copilot, try this after the exercise. Keep the original file and compare its suggestions with your own result.

Review SalesModel and the three measures. Explain the Products relationship and how the Region filter affects West Sales and Region Share. Compare the totals before and after O4.

More from Microsoft

These Microsoft guides cover the tools used in this lesson.

Loading your progress…