In Power BI, is there a way to have a DAX function that uses the value currently selected in a slicer that has independent data?
For example, if I have a table where each row is a customer, and I have a separate table with a list of colors (which doesn't have any logical join with the customers - it's just a list of colors) - is there a way to set it up so that when the slicer selects "Red", the customer's name will be shown as "Bob Smith Red", if the slicer selects "Orange", the customer's name will be shown as "Bob Smith Orange", etc.
Yep. You can read the value of a slicer in with SELECTEDVALUE.
Color = SELECTEDVALUE ( SlicerTable[Color] )
Note that SELECTEDVALUE ( X ) is a shortcut for IF ( HASONEVALUE ( X ), VALUES ( X ) ).
Bear in mind that this will only work with measures, not calculated columns, since calculated columns are unaffected by any filtering (they are only computed once when the data loads).
Related
I have two columns Month and ID a want distinc count per each month, but once I use data slicer to show data it shows as count distinct per select period
For example I have
I have
I used :
COUNT_ID = distinctcount('ID'[DATA])
once I select in data slicer 202207 & 202208 I get distinct values 4:
WANT
but I want result 5 because
202207 distinct values 2
202208 distinct values 3
I want to use this logic for whole data set once I select specific period.
Has anybody can help with?
Ids =
SUMX (
VALUES ( 'Table'[Month] ),
CALCULATE ( DISTINCTCOUNT ( 'Table'[ID] ) )
)
This is my first question here.
This time I want to count values that appear in different columns. Each one corresponds to the values from a row, but there is only 1 column that they have in common (not shown in the picture). I need a measure to show a count of each word described in those cells.
For example, in this case (please ignore blanks, it's a test), the measure should give a count for the word 'Desodorante' as 3, 'Cabello' as 2, and the rest 1. These words are pre defined and there are no random values accepted.
I may say that I want to state each of these words as a kind of category. I would like to make a slicer out of them too.
example
I believe a workaround is to create a calculated table, stating as columns each of these values and allocating a count of each value from these 4 columns?
A Power Query solution to this problem has been posted here: PowerBI - Count instances of string in multiple columns
A DAX solution to this problem would be to create a new calculated table by appending the 4 columns using UNION() and SELECTEDCOLUMN() and then getting the count via SUMARIZE():
The 2 calculated Tables:
Mesa =
FILTER(
UNION(
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto2]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto3]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto4]
)
),
[Producto] <> BLANK()
)
Producto Count =
SUMMARIZE(
'Mesa',
'Mesa'[Producto],
"Count", COUNT('Mesa'[Producto])
)
I do habe a slicer which filters a simple table. The slicer works perfectly fine, but obviously if the slicer is not active, all rows in the table are shown. Is there a option where the table is going to be empty when nothing is selected in the slicer? I know I can add another option to the slicer which empties the table, but I am looking more for a "default option".
You can create a measure which only returns a value if the slicer field is filtered:
Filtered MyMeasure =
IF (
CALCULATE (
ISFILTERED ( 'Dimension Table'[Sliced Field] ),
ALLSELECTED ( 'Dimension Table'[Sliced Field] )
),
[MyMeasure],
BLANK()
)
I have a slicer, called COUNTRY and applied to table MY_TABLE. When I calculate a measure, everything works as expected:
-- calculates distinct count only for COUNTRY = x
Some Measure = DISTINCTCOUNT('MY_TABLE'[SOME_COLUMN])
The problem is SUMMARIZE ignores slicer selection:
-- calculates distinct count accross all countries: x, y, z, etc.
Calculated Table =
RETURN SUMMARIZE(
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT('SOME_TABLE'[SOME_COLUMN])
)
How to make SUMMARIZE take into account slicers?
Only Measures are "responsive", calculated tables and columns get calculated and created once, when the data are loaded.
Note that if a calculated table is used inside a measure it will behave correctly, but as you may know, a measure must return a scalar value and not a table. (ie you can use summarize inside a measure, you can then filter the obtained table and return the sum of one column)
Of course, you can filter calculated table with a slicer. If you can, go for SUMMARIZECOLUMNS because this function is better optimized then SUMMARIZE, and has arguments for filtering.
Filtering SUMMARIZECOLUMNS
If you want to stick to SUMMARIZE, you can filter your table by wrapping it with CALCULATETABLE.
Calculated Table =
CALCULATETABLE (
SUMMARIZE (
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT ( 'SOME_TABLE'[SOME_COLUMN] )
),
Dim[Color]
= SELECTEDVALUE ( Slicer[SlicerValues] )
)
Should FILTER be used inside or outside of SUMMARIZE?
In Power BI you can have multiple slicers based on different criteria in the data, e.g. one on shape and one on colour. The interaction between or among these slicers means that if I were to select "circle" and "red" then the results would be about red circles only. It filters to those entries where BOTH slicers are met. Is there a way to set Power BI slicers so that if I selected "circle" and "red" I would get both all red and all circle results (e.g. only one slicer needs to be satisfied)?
Here I'd like to get rows 1,4,5,6 returned.
Any help would be hugely appreciated!
Edited. As Alexis points out in the comments, I had this wrong at first. Thanks for the gentle nudge!
EitherCondition =
VAR MatchesColour =
CALCULATE (
COUNTROWS ( 'FactTable' ),
KEEPFILTERS ( TREATAS ( VALUES ( 'ColoursDisconnected'[Colour] ), 'Colours'[Colour] ) )
) > 0
VAR MatchesShape =
CALCULATE (
COUNTROWS ( 'FactTable' ),
KEEPFILTERS ( TREATAS ( VALUES ( 'ShapesDisconnected'[Shape] ), 'Shapes'[Shape] ) )
) > 0
RETURN
MatchesColour || MatchesShape
With no other details of your data model, I'm assuming a simple dimensional model with a 'Colours' dimension of unique colours and a 'Shapes' dimension of unique shapes, each connected in a 1:N relationship with a fact table named 'FactTable'. As Alexis pointed out in comments, to drive this behavior, we need to build disconnected tables to drive the slicers.
Thus we have slicers populated from disconnected tables, and we have the table visual created with the related dimensions.
The first VAR says whether there is any data in the fact based on diconnected colour context. The second does the same for shape. If either is true, we return TRUE.
You can filter your visual on [EitherCondition]=True to remove the other values. Or you could build from here into a more complex measure.
The key part is that filter context in DAX is always a logical AND situation. The only way to get logical ORs is to evaluate multiple expressions and come up with a way to combine them that works in your situation.
Here's the model diagram:
And the measure in action: