Case – I have to create
a dynamic card and it should display the product name and product price when I choose
a specific product from slicer. We will see what is the card and slicer that I’m
talking about when we move along.
Why this
has happened
I have a product table which has to be presented in a user friendly manner. I wanted something like this to happen if user selects “Pen” from the slicer “One pen is Rs.15”
I have a product table which has to be presented in a user friendly manner. I wanted something like this to happen if user selects “Pen” from the slicer “One pen is Rs.15”
What I
wanted to do next
You can see my product table in the above image, now I want to add a column which displays something similar to “One pen is Rs.15” for each row corresponding to their Product Name and Product Price in this table and I have shown it below.
You can see my product table in the above image, now I want to add a column which displays something similar to “One pen is Rs.15” for each row corresponding to their Product Name and Product Price in this table and I have shown it below.
As
you can see above I have selected the column from examples option which is in
the top left of Add column task bar of the query editor. If you don’t
understand tap the below task bar and see it for yourself.
Once I have placed the example and pressed enter
the following table is my result for it.
Now I’m done with query editor and time to play with the
report. I need to add two visuals in my report one is a “slicer” which is much
like a combo box where it allows me to select any product of my choice and the
other one is a card which is going to display the price of one product.
In the above image you can see some default visualizations which
you can use in your report also both slicer and card which I’m talking about is
also there, try to find it yourself before going to the next line.
Slicer is the first one in the fifth row and card is the fourth
one in the fourth row.
Once I have selected both visuals it will be shown like
the above image where the one in the left is slicer and the one in the right is card.
Then I placed the Product name on the field of slicer like I have
shown below
After placing the Product name, the slicer visual became a
combo box containing all the products name on it like I have shown below.
Can you recall the column from example that we created in the
beginning, now it is time to implement that on the normal card and make it a
dynamic card as shown below.
You can notice when I’m selecting different products then the
content displayed in the card is changing accordingly. I did this by doing a simple thing as shown below.
As you can notice in the above image I simply placed the
column I created using the column from example which is named "Merged" and placed it on the field of
the card.
Secret - Here there is an issue see it for yourself.
Secret - Here there is an issue see it for yourself.
Did you notice that i haven't selected any product from the slicer but still it is showing data for an A4.
As long as a product is selected it displays the data but what happens when nothing is selected, it is going to show the first row of the column “Merged” which we created using column by example since I have selected the option First in my fields to show as default.
As long as a product is selected it displays the data but what happens when nothing is selected, it is going to show the first row of the column “Merged” which we created using column by example since I have selected the option First in my fields to show as default.
How I want
it to be – I want to display “Select a product” when nothing is selected and that is the right thing to do.
My way of
doing it – I thought of creating a new measure and placing it in the
field of card in order to solve this issue and it is below.
You can see a New
Measure option in the second from top right and I wrote a simple dax statement
to solve this issue. The statement is below for you
Measure = IF (HASONEVALUE ('Product'[ProductName]),
SELECTEDVALUE ('Product'[Merged]),
"Select a Product")
The dax statement I implemented above is much similar to an “If
condition”, here I want to check if there is a value for product name by using “Has
On Value” and if there is a value then display the corresponding value you find
in the column by example using “Selected Value” else display “Select a Product”, If you don't understand read it again.
After placing the measure i created to the field of card it worked as I expected it to be when nothing is
selected as shown below.
If you select only one product then it will show the unit price of that product without any issues as shown below.
If you try to select multiple products as shown below then you will be notified to "select a product" which implies that you have selected multiple products so select only one.
What is
happening – Power BI visuals are connected dynamically so if we choose
something specific in a visual all the other visuals will be filtered
accordingly and same thing is happening in this scenario where we choose a
product name from the slicer and it is filtering the “column by example” we
created and finds the row which matches that product and displaying it hence it
is dynamic and easy to track the price of a unit product as I wanted
it to be.
Thanks for sharing this Informative content. Well explained. Got to learn new things from your Blog onpower bi online course
ReplyDelete