Power BI slicer OR condition - powerbi

The interaction between two slicers in Power BI gives me output with AND condition.
Example: If I selected the year 2020 and company ABC, the output would be all the data from company ABC in the year 2020.
But I want the two slicers to work with OR condition.
I have used this Dax
Include = (MAX(Table1[Column1]) = SELECTEDVALUE(Col1[Column1])) +
(MAX(Table1[Column2]) = SELECTEDVALUE(Col2[Column2]))
But the problem with above Dax I have not selected anything in slicer ( ALL by default) it is showing me a blank visual. What am I doing wrong?

let me guess you have a table "or_slicer_main_table" with Year, Company and some other columns. Now create 2 new table where the first one will contain the distinct list of Year from table "or_slicer_main_table" and the second one will contain distinct Company list from that same table.
New custom Table 1:
or_slicer_year_list =
SELECTCOLUMNS(
'or_slicer_main_table',
"YEAR", 'or_slicer_main_table'[year]
)
New custom Table 2:
or_slicer_company_list =
SELECTCOLUMNS(
'or_slicer_main_table',
"company", 'or_slicer_main_table'[company]
)
Do not establish any relation between those 3 tables.
Step-1: Create Year slicer using the newly created "or_slicer_year_list" table.
Step-2: Create Company slicer using the newly created "or_slicer_company_list" table.
Step-3: Create these following 5 measures in your table "or_slicer_main_table"
1.
year_current_row = max('or_slicer_main_table'[year])
2.
year_selected_in_slicer = SELECTEDVALUE(or_slicer_year_list[YEAR])
3.
company_current_row = max('or_slicer_main_table'[company])
4.
company_selected_in_slicer = SELECTEDVALUE(or_slicer_company_list[company])
5.
show_hide =
if(
[year_selected_in_slicer] = [year_current_row]
|| [company_selected_in_slicer] = [company_current_row],
1,
0
)
Now you have all instruments ready for play. Create your visual using columns from the table "or_slicer_main_table"
Final Step: Now just add a visual level filter for the measure "show_hide" and set value will show only when "show_hide = 1".
The final output will be something like below image-

Can you try using "IN VALUES" instead of "SELECTEDVALUE"
So your DAX should be
Include = (MAX(Table1[Column1]) IN VALUES (Col1[Column1])) +
(MAX(Table1[Column2]) IN VALUES (Col2[Column2]))
SELECTEDVALUE function returns the result only if single value is selected in slicer in case of multiple selection it will return Blank(). Thats in the case when nothing is selected (which is similar to all selected) has multiple values in set and so SELECTEDVALUE fucntion will return Blank(). This can be handled by using "IN VALUES" function which can return a set of all selected values.

Related

Hide item in slicer from end user (Power BI)

I have a data set on sales with a column named "Processes". This column has four entries: "Budget", "Forecast 1", "Forecast 2" and "Realized".
I have then inserted a slicer on this column, and I would like to always have "Realized" selected and then removed from the filter such that the end user
only can filter on "Budget", "Forecast 1" and "Forecast 2"
can't deselect "Realized".
Is this possible?
There is no direct option for your purpose. But you can achieve the requirement with some workaround as stated below-
Step-1: Create a new Disconnected (not related to any table) table new_slicer_table_name contains only three value "Budget", "Forecast 1" and "Forecast 2".
Step-2: Create a slicer using Processes column from that new table. You will have 3 values in the slicer list now.
Step-3: Put a note below the slicer like "*Realized Default Selected"
Step-4: Now create a measure as below-
show_hide =
var current_row_process = MIN(your_data_table_name[Processes])
return
IF(
current_row_process IN ALLSELECTED(new_slicer_table_name[Processes])
|| current_row_process = "Realized",
1,
0
)
Step-5: Add a visual level filter for the last created measure show_hide and show values only when show_hide = 1
This will now give you your expected output.

Filter values based on other column in Power BI

In Power BI I'm trying to combine and get the unique values of pages in a new table. I need to filter one of the columns, based on a value of another column in that same table. So i want to do something like
FILTER(VALUES('Table1'[URL]), 'Table1'[Correct] = "Yes"
But it's not working, because I can only filter based on calculations, not columns.
The complete line that I have now and is working is:
All pages = FILTER(
DISTINCT(UNION(VALUES('Table1'[URL]),VALUES(Table2[Complete URL]),
VALUES('Table3'[URLs]))), [URL] <> BLANK())
How can I add a filter to one of those tables, based on another column of that table?
You can just replace FILTER with CALCULATETABLE:
CALCULATETABLE(VALUES('Table'[URL]), 'Table'[Correct] = "Yes")
If you need to count the values, just wrap it into COUNTROWS:
Measure = COUNTROWS( CALCULATETABLE(VALUES('Table'[URL]), 'Table'[Correct] = "Yes"))

Dax - dynamic attribute value based on filter parameter

I need to create a calculated column that is based on another column but depends on the date filter the report is run for.
If the item is own for more than a year it is 'Comparable' if less than a year it is 'Non Comparable'.
I have Item, DateOfPurchase in T1 and Date in T2 (Period table)
I have come up with DAX using today() but it only works if we report on today's date.
This didn't work (no idea why)
=if( dateadd( 'Item'[PurchaseDate],1,year)<today(),"Comp","Non-Comp")
This worked but only for current period
=DATEDIFF('Item'[PurchaseDate],today(),MONTH)
= if('Item'[DateDiff]>12,"Comp","NonComp")
However, I can not use that column when running report for a different period, because attribute is not valid for prior periods.
Since calculated columns are computed only once, when the table is processed/refreshed, you cannot use a calculated column for your scenario.
Instead, consider using a disconnected parameter table. In your case, this would be a table with 1 column and just 2 rows: "Comp" and "NonComp". You can create this as a calculated table like so:
ParameterTable = DATETABLE("Value", STRING, {{"Comp", "NonComp"}})
Based on what the user selects on this table - which you can find using SELECTEDVALUE(ParameterTable[Value]) - you apply the relevant logic in a measure instead:
BaseMeasure =
// Whatever you are trying to calculate
SUM(Item[Amount])
Measure =
// This measure will respect the user selection (Comp / NonComp) and the current period:
VAR compValue = SELECTEDVALUE(ParameterTable[Value])
VAR today = MAX('Date'[Period])
RETURN
SWITCH(
compValue,
"Comp", CALCULATE( [BaseMeasure] , DATEDIFF('Item'[PurchaseDate], today, MONTH) > 12),
"NonComp", CALCULATE( [BaseMeasure] , DATEDIFF('Item'[PurchaseDate], today, MONTH) < 12),
[BaseMeasure] // Fallback, in case user didn't select Comp/NonComp
)
If you have multiple base measures in your report, you will need to implement this pattern for each of your base measures.

How to set OR logic between two slicers for the attributes from same dataset?

I'm trying to set OR logic between the two slicer filters. The two slicers are from same dataset. Below are more details:
My source table:
Visual with slicers:
My goal is, if I select any value from slicer 1 and also from slicer 2, my visual should show the results w.r.t both the select values.
For example, if I select A1 from slicer 1 and 200 from slicer 2, the result should be shown as below (Similar to SQL query (Where Column1 ='A1' or Column2=200)
Desired result:
]3
You'll need to create new tables to use for slicers.
Col1 = VALUES(Table1[Column1])
Col2 = VALUES(Table1[Column2])
Change the slicers to filter on these and now define a new measure:
Include = (MAX(Table1[Column1]) = SELECTEDVALUE(Col1[Column1])) +
(MAX(Table1[Column2]) = SELECTEDVALUE(Col2[Column2]))
Then in the visual level filters section, filter for [Include] > 0:

GroupBy and aggregate in DAX

I'm new to DAX queries and trying to create a DAX query equivalent to following SQL query.
SELECT Year, Company, SUM(Actual Sales) as Actual, SUM(Planned Sales) as Planned
FROM Sales
GROUP BY Year, Company
I'm not able to take aggregate of multiple columns after groupby in DAX. How can I resolve this?
There are actually a few ways in Power BI to do this kind of aggregation. Here are three different options with slightly different results from each other.
Option 1: Query Editor
In the query editor window, select Transform -> Group By...
... and configure it as shown below (make sure to click the Advanced option).
This results in the below. Note - this replaces the starting table.
Option 2: Measures
In the regular window (not the query editor used in option 1), click on Modeling -> New Measure...
... to create two measures using the formulas below.
Actual = SUM(Sales[Actual Sales])
Planned = SUM(Sales[Planned Sales])
Once those measures are created, a visual (a matrix in this case) can be created with the desired headers and values.
Option 3: Columns
Similarly to option 2, click on Modeling -> New Column...
... to create two columns using the formulas below.
Actual = CALCULATE(
SUM(Sales[Actual Sales]),
FILTER(
Sales,
Sales[Year] = EARLIER(Sales[Year]) &&
Sales[Company] = EARLIER(Sales[Company])
)
)
Planned = CALCULATE(
SUM(Sales[Planned Sales]),
FILTER(
Sales,
Sales[Year] = EARLIER(Sales[Year]) &&
Sales[Company] = EARLIER(Sales[Company])
)
)
Now the aggregations are in columns that can be used report visuals.