Now do it in Power BI Desktop
Advanced DAX: Power BI Desktop exercise
The patterns analysts reach for every week: running totals, virtual relationships and the inactive date. You practised this in the browser; now do it in your own copy of Power BI Desktop (free from Microsoft), on the same data, and check your numbers below.
1. Get the data
In Power BI Desktop: Home → Get data → Text/CSV, pick each file, then Load. Check that numbers came in as numbers and dates as dates (Table view, column header icons).
Create the date table: Modeling → New table, paste this, then Mark as date table on the Date column. Sort Month by Month Number and Month Year by Month Key.
Dates =
ADDCOLUMNS(
CALENDAR(DATE(2013, 1, 1), DATE(2014, 12, 31)),
"Year", YEAR([Date]),
"Month", FORMAT([Date], "mmm"),
"Month Number", MONTH([Date]),
"Quarter", "Qtr " & QUARTER([Date]),
"Month Year", FORMAT([Date], "mmm yyyy"),
"Month Key", YEAR([Date]) * 100 + MONTH([Date])
)In Model view, check or create these relationships (many side first):
- financials[Date] > Dates[Date]
- financials[Ship Date] > Dates[Date] (inactive)
2. The tasks
1. Running total
Create Running Sales: everything up to the last date in context, across years. Show it by Year.
2. Sales by ship date
Turn on the inactive Ship Date relationship in one measure, and show it by Year.
3. Targets without a relationship
Targets has no relationship. Use TREATAS so each country row shows its own total target.
3. Check your numbers
Try every task first. Then open each answer: the numbers below come from the same data, so yours should match exactly.
1. Running total
Running Sales = CALCULATE(SUM(financials[Sales]), FILTER(ALL(Dates[Date]), Dates[Date] <= MAX(Dates[Date])))| Year | Running Sales |
|---|---|
| 2013 | 26,415,255.51 |
| 2014 | 118,726,350.26 |
| Total | 118,726,350.26 |
2. Sales by ship date
Sales by Ship Date = CALCULATE(SUM(financials[Sales]), USERELATIONSHIP(financials[Ship Date], Dates[Date]))| Year | Sales by Ship Date |
|---|---|
| 2013 | 25,275,493.21 |
| 2014 | 92,354,666.26 |
| Total | 118,726,350.26 |
3. Targets without a relationship
Country Target = CALCULATE(SUM(Targets[Target]), TREATAS(VALUES(financials[Country]), Targets[Country]))| Country | Country Target |
|---|---|
| Canada | 26,900,000 |
| France | 22,900,000 |
| Germany | 26,400,000 |
| Mexico | 21,200,000 |
| United States of America | 24,300,000 |
| Total | 121,700,000 |
The lessons behind this exercise
- Context transition, for real
- Virtual tables: ADDCOLUMNS and SUMMARIZE
- Running totals and rolling windows
- Pareto and ABC classification
- Inactive relationships: USERELATIONSHIP
- Everyday functions: dates, lookups, text
- Budget vs actual: TREATAS
- What-if parameters
- Window functions: OFFSET, WINDOW, INDEX
- Statistics: median, percentiles, spread
- Average of ratios vs ratio of sums
- Top N and the rest
- Compare with the average