Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Dynamic comparison calculations in DAX |
---|
This blogs shows a clever way in which you can use a slicer to choose which values to compare in a measure. |
Comparing performance across any dimension can be messy, as if you're not careful you'll require multiple visuals (one for each element of the dimension table whose values you're comparing). In this blog we will let our end user decide for which year to compare sales:
Check if this is the effect you want by clicking here to try the report.
The first thing to do is create a table with a unique list of the values you are interested in. This can be a static table (for which you type in data) or a live connection:
This example is using a query to the same source as my data, but with the duplicates and extra columns removed. I've called the table Live Slicer table.
If you try to use the same model table or column for the slicer as for the data in the visual then the visual will be filtered (which we don't want)! Your data model should thus include a disconnected slicer table, like this:
The Purchase table contains all of the sales data, while the Calendar table splits the dates into Year, Month etc. Notice that the slicer table is separate: it doesn't directly filter anything.
Now to create a slicer with the column from the Live slicer table table (and a visual with the same column from the data table):
The results aren't very exciting without a measure to tie the visuals together!
Now create a measure to calculate the total sales for the column year, but subtracting the selected year's sales:
Comparison to selected =
VAR SelectedValue =
-- Returns the year chosen in the slicer table
VALUES('Live slicer table'[YearNumber])
VAR OriginalColumnValue =
-- Total sales for each year for the filter context
SUM(Purchase[Quantity])
VAR TheSlicerCalculation =
-- Replace the row/column filter with the year chosen by the user
CALCULATE(
SUM(Purchase[Quantity]),
'Calendar'[YearNumber] = SelectedValue
)
-- Take the sales of the year chosen by the user away
-- from the current column/row sales
Return OriginalColumnValue - TheSlicerCalculation
Adding this measure to the visual gives the desired effect:
All that is left to do is change the titles - and perhaps add some conditional formatting.
The title measure works using the same idea as the calculation above. Use VALUES to return the only year that is left:
Chart Title =
-- The "" indicate literal text to be joined
-- on to the remaining year
"Year sales less " &
VALUES('Live slicer table'[YearNumber]) & " sales."
This can be applied by turning the Title property to ON and then hovering to the right of the text box and clicking the dots that appear.
Choose the measure Chart Title as the Title text field.
This trick also works for creating slicers to change the aggregate being used: swap between SUM, AVERAGE, MIN or MAX to get truly dynamic visuals!
Some other pages relevant to the above blog include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.