Power BI: Changing multiple slicer relationship to be OR not AND - powerbi

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:

Related

Double Dynamic Measures in PowerBi visual

I am attempting to create a slicer, which would switch between measures in a visual.
Due to some limitations, I'm not using Field parameter. Instead, I'm using a measure name table + DAX expression used as value in visual, meant to switch between measure.
Issue I'm facing:
For some cases, I need to show two measures in a visual, with legend for each measure. If i simply select two measures in slicer, I'm getting a sum of two measures.
Is this possible to achieve?
Current DAX expression two switch between measures:
Selected Measure =
IF (
HASONEVALUE ( 'Measures Table'[Measure] ),
SWITCH (
VALUES ( 'Measures Table'[Measure] ),
"Total", [Total],
"New", [New] ,
"Direct", [Direct],
"Indirect", [Indirect]
),
[Total]
)
How do I combine "Direct" and "Indirect" in one slicer position, to display both measures in visual with legend?

What is the difference between SUMX(ALL...) vs CALCULATE(SUMX.., ALL..)?

Following are 2 measures:
SUMX ( ALL ( SALES ) , SALES[AMT] )
CALCULATE ( SUMX ( SALES, SALES[AMT] ), ALL (SALES) )
Similarly for the following 2 measures:
SUMX ( FILTER ( SALES, SALES[QTY]>1 ), SALES[QTY] * SALES[AMT] )
CALCULATE ( SUMX ( SALES, SALES[QTY] * SALES[AMT] ), FILTER ( SALES, SALES[QTY]>1 ) )
Both above examples clear the natural filters on the SALES table and perform the aggregation.
I'm trying to understand what is the significance/use case of using either approach maybe in terms of logic or performance?
In DAX you can achieve the same results from different DAX queries/syntax.
So based on my understanding both the DAX provide the same result :
SUMX ( ALL ( SALES ) , SALES[AMT] )
CALCULATE ( SUMX ( SALES, SALES[AMT] ), ALL (SALES) )
And the 1st one is a more concise way to achieve way rather than the 2nd one in all cases/scenarios.
Currently when I tested these out with <100 records in a table ; the performance was the same for both the measures.
But ideally the 1st scenario would be quicker then the 2nd one which we can test out by >1 million record through DAX studio.
Can you please share your thoughts on the same?
The first uses a table function to return the whole sales table and then iterate. The second iterates over the sales table in the context of calculate which removes any filters that were present on the sales table.
SUMX ( ALL ( SALES ) , SALES[AMT] )
CALCULATE ( SUMX ( SALES, SALES[AMT] ), ALL (SALES) )
In these two DAX functions, ALL() is doing two very different things and it is unfortunate the same name was used. In the first one, ALL() is being used as a table function and returns a table. In the second one, ALL() is being used to remove filters and could be replaced with REMOVEFILTERS() (the first one cannot be replaced this same way).
This is a lengthy and detailed topic and I suggest you make a cup of coffee and have a read here: https://www.sqlbi.com/articles/managing-all-functions-in-dax-all-allselected-allnoblankrow-allexcept/
To summarise the article, ALL() and REMOVEFILTERS() are not the same. ALL() can be used where REMOVEFILTERS() is used but not vice versa.
CALCULATE ( SUMX ( SALES, SALES[QTY] * SALES[AMT] ), FILTER ( SALES, SALES[QTY]>1 ) )
This DAX uses calculate to change the filter context and remove any existing filters. The important thing is that it is removing existing filters.
They mainly achieve the same result (most of the time) but there is still more nuance though. In DAX, there are always multiple ways of achieving the same outcome. More importantly, DAX is always dependent on the evaluation context. Writing SUM(SALES[AMT]) can return different numbers depending on context. If it was in table with colour, it would return the sum per colour at each line and a total. If it were by country, it would return a total by country and a total. i.e. the exact same formula returns different results depending on context. In this simplistic example, they are essentially the same though.
The second example would also never be written it this way as you should never filter entire tables (especially fact tables). You would filter the column instead. e.g.
SUMX(
FILTER(VALUES(Sales[Quantity]),
Sales[Quantity]>1), Sales[Quantity] * Sales[SalesAmount]
)
This whole video is an excellent watch but if you watch from 45:33, you can see a good explanation of the difference between removing filters and returning a table which is the essence of your question. You also need to understand expanded tables which is explained earlier in the video. youtube.com/watch?v=teYwjHkCEm0&list=WL&index=2
At the risk of stating the obvious, you are wrapping a function (SUMX) inside a process represented by CALCULATE function.
It is an actual process, which will attempt a context transition.
Beyond the performance implications of forcing extra processing, the answer to your question heavily depends on how and where these measures get injected into the model, as it determines if the context transition would occur.
For reference, here are just some of the relevant SQLBI articles: https://www.sqlbi.com/articles/introducing-calculate-in-dax/
https://www.sqlbi.com/articles/understanding-context-transition-in-dax/

Can you update a DAX function from an unrelated slicer?

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).

DAX SUMMARIZE miss applied slicers

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?

PowerBI how to use one slicer with two columns (dates)

I have a table with columns 'date from' and 'date to', and visualize them as bars using asTimeline visual.
I want to add a slicer which will work on both of these fields simultaneously. Currently I have two slicers working independently on each of those fields:
This is not really intuitive. Since start and end both define a period in time, if the period is inside the slider selection, it should be included. But slicers work on only one field. So I probably need to perform some DAX magic to create a field based on those two, but I don't know where to begin.
First create an measure to check a row overlaps the your date range:
Date Included =
IF (
FIRSTNONBLANK ( DateTable[Start Date], 1 ) <= MAX ( 'Calendar'[Date] ) &&
FIRSTNONBLANK( DateTable[End Date], 1 ) >= MIN ( 'Calendar'[Date] ),
"Include",
"Exclude"
)
and , add above Measure as a filter on your visualisation, where Date Included is Include
Than you can filter your Calendar table to single value, or range.
Also,only overlapping rows from your fact table will be displayed.
The problem can be solved by using a separate calendar-table and a measure: PowerBI filter- selected Date between Start and End date
Another way to solve this, and keep using the two slicers more intuitively, is described here: https://radacad.com/from-and-to-date-slicers-in-power-bi-filtering-based-on-two-fields. The solution describes how to set the properties of those timeline-slicers so that it makes a bit more sense for the user (set the "Start of employment" slicer as Type - After and the "End of employment" as Type - Before).