Creating a Slicer that also contains a DAX Measure
I got an interesting question from Vijay asking, “How do we do a slicer with data count?”
This is what Vijay was looking to achieve as shown below and I always enjoy a good challenge.
As I always like to do, is to show how I got to the required result.
- I currently have got data from my website, which I look at daily to see how my blog posts are tracking.
- In my dataset I like to see where in the world people are coming from to visit my blog or website.
- Based on the requirement from Vijay I created a slicer which takes the total sessions per country, which I can then use as a Slicer on my data.
The first thing that I did was to create a calculated table which will have the information I require for the Slicer.
In Power BI Desktop I went to the Modeling Ribbon and clicked on New Table
This will be the Country and Sessions. I created it with the following DAX Syntax below.
Slicer Table = ADDCOLUMNS ( SUMMARIZECOLUMNS ( 'All Web Site Data'[Country] ), "Sessionszz", [Sessions] )
-
Line 1
- What I have done above is created a table called Slicer Table
-
Line 2-3
- This is where I added the ADDCOLUMNS Syntax for my table.
- NOTE: Thanks to Maxim for commenting and letting me know that I did not need the original CALCULATETABLE
-
Line 4
- I then used the SUMMARIZECOLUMNS DAX function and put in the ( ‘All Web Site Data'[Country] column, as per my requirement to get a Count of Sessions per Country.
-
Line 5
- This is where I created my Column Name called “Sessionszz” and my measure called [Sessions]
- NOTE: When I use measures I only use the Square brackets.
-
Line 6-7
- I then close off my previous DAX functions.
The result is I now have the following calculated table.
How I had the data in order to create a new calculated column which will have the Country with the Total Sessions. I did it with the following DAX Syntax below.
Country with Sessions = 'Slicer Table'[Country] & " (" & 'Slicer Table'[Sessionszz] & ")"
And the result was I now had my new column in my table showing both the Country and the Total Sessions
Next, I created a relationship from my Fact table ‘All Web Site Data’ to my new calculated table called ‘Slicer Table’ on the Country Column.
I went to the Modeling tab again, and this time clicked on Manage Relationships
I then clicked on New and selected the following as shown below.
I then click Ok and Ok again to come back to Power BI Desktop
Now I was able to create the Slicer, which shows the Country and the Total Sessions.
I have selected the Country “Afghanistan” which has got a total of 4 sessions, and in my table, I can see the sessions and the date when they occurred.
Each time the dataset is refreshed the calculated table will also be refreshed which will ensure that the slicer values are up to date.
This was a great exercise and I personally think something quite handy to have when slicing data.
As always if you have any questions or comments please leave them in the area below.
Hi Gilbert
Nice trick! But why you use CALCULATETABLE? I think that this is enough:
SUMMARIZECOLUMNS ( ‘All Web Site Data'[Country] ,
“Sessionszz”,
[Sessions]
)
Thanks Maxim, I have updated the blog post.
It is great to always be learning!
I hope you checked it before 🙂
Hi Gilbert, Maxim , isn’t SUMMARIZECOLUMNS a bit “heavy” function to use inside ADDCOLUMNS? A simple ALL() would do the same:
Slicer Table =
ADDCOLUMNS ( ALL ( ‘All Web Site Data'[Country] ), “Sessionszz”, [Sessions] )
or for that matter:
Slicer Table =
SUMMARIZECOLUMNS ( ‘All Web Site Data'[Country], “Sessionszz”, [Sessions] )
I doubt it’ll matter in your case but still.
That aside I think it’s marvelous idea to publish these DAX tricks for everyone to enjoy and learn from.
Hi there,
Will from the Microsoft Power BI Team suggested just adding in a calculated column on the table, which would then have the same outcome. I plan on updating this blog post to reflect this.
Thanks for reading my blog it is appreciated.
Gilbert, this is one of the most useful tips I’ve seen, and I have been using these tools a long time. Nice solution when screen real estate is limited.
Hi Ed,
Thanks for the kind words they are appreciated and glad to see that it has assisted you.
[…] Creating a Slicer that also contains a DAX Measure (@GilbertQue) […]
Nice thinking… great blog..
Thanks for the kind words
Hi, really nice tip.
But, when I try to make a relationship between my fact table and my new calculated table I got this error:
A circular depency was detected: “calculated”table_name[table_name-etcetc]
Do you know why this comes up?
Hi there,
Please have a look at the relationships and ensure that the filter direction is not set to BOTH, which could be causing the error.
Is there any way to then:
1. Sort the slicer by count
2. Have the slicer be dynamically updated by another slicer.
thank you
Hi there
Yes you can possibly sort it by count, where you can have a sort by column based on the count?
The Slicers should change based on other selections as long as there is a relationship between the data?
Hi Gilbert,
I’m sorry for your solution is not really takes measures on slicers. You are making measures static on process time. But the most important thing is keeping measure dynamic and using on a slicer,
For example, if you see Albania 13 with no filters, when user selects year (2018), you must see updated Albania – 7 or something like that.
Disappointed but thanks anyway.
If you a have solution for this scenario i will be happy to hear.
Regards,
Hi Sedat,
Thanks for the comment and you are indeed 100% right that I can only calculate it at process time.
What you could only possibly do at the moment is to create a measure and use it in the Title with the Conditional formatting for the item that you are selecting?
Very neat and extremely useful
Thanks for the comment, I hope that it helped you!
Hi Gilbert, Nice Post. Is it possible to get the .pbix file to play around with further.
Hi there
I cannot find it when doing a quick search.
Is there anything that needs clarification from the blog post?
Was having problems creating the intial slicer table. Have two columns, state and county and was wantining to display county of county per state i.e.,
Florida (14)
Georgia (6)
Maryland (4) … etc
In other words, display the count of child in a parent/child hierachy.