Power BI Dashboard where the core filter condition is a disjunction on numeric fields - powerbi

We are trying to implement a dashboard that displays various tables, metrics and a map where the dataset is a list of customers. The primary filter condition is the disjunction of two numeric fields. We want to the user to be able to select a threshold for [field 1] and a separate threshold for [field 2] and then impose the condition [field 1] >= <threshold> OR [field 2] >= <threshold>.
After that, we want to also allow various other interactive slicers so the user can restrict the data further, e.g. by country or account manager.
Power BI naturally imposes AND between all filters and doesn't have a neat way to specify OR. Can you suggest a way to define a calculation using the two numeric fields that is then applied as a filter within the same interactive dashboard screen? Alternatively, is there a way to first prompt the user for the two threshold values before the dashboard is displayed -- so when they click Submit on that parameter-setting screen they are then taken to the main dashboard screen with the disjunction already applied?
Added in response to a comment:
The data can be quite simple: no complexity there. The complexity is in getting the user interface to enable a disjunction.
Suppose the data was a list of customers with customer id, country, gender, total value of transactions in the last 12 months, and number of purchases in last 12 months. I want the end-user (with no technical skills) to specify a minimum threshold for total value (e.g. $1,000) and number of purchases (e.g. 10) and then restrict the data set to those where total value of transactions in the last 12 months > $1,000 OR number of purchases in last 12 months > 10.
After doing that, I want to allow the user to see the data set on a dashboard (e.g. with a table and a graph) and from there select other filters (e.g. gender=male, country=Australia).

The key here is to create separate parameter tables and combine conditions using a measure.
Suppose we have the following Sales table:
Customer Value Number
-----------------------
A 568 2
B 2451 12
C 1352 9
D 876 6
E 993 11
F 2208 20
G 1612 4
Then we'll create two new tables to use as parameters. You could do a calculated table like
Number = VALUES(Sales[Number])
Or something more complex like
Value = GENERATESERIES(0, ROUNDUP(MAX(Sales[Value]),-2), ROUNDUP(MAX(Sales[Value]),-2)/10)
Or define the table manually using Enter Data or some other way.
In any case, once you have these tables, name their columns what you want (I used MinNumber and MinValue) and write your filtering measure
Filter = IF(MAX(Sales[Number]) > MIN(Number[MinCount]) ||
MAX(Sales[Value]) > MIN('Value'[MinValue]),
1, 0)
Then put your Filter measure as a visual level filter where Filter is not 0 and use MinCount and MinValues column as slicers.
If you select 10 for MinCount and 1000 for MinValue then your table should look like this:
Notice that E and G only exceed one of the thresholds and tha A and D are excluded.

To my knowledge, there is no such built-in slicer feature in Power BI at the time being. There is however a suggestion in the Power BI forum that requests a functionality like this. If you'd be willing to use the Power Query Editor, it's easy to obtain the values you're looking for, but only for hard-coded values for your limits or thresh-holds.
Let me show you how for a synthetic dataset that should fit the structure of your description:
Dataset:
CustomerID,Country,Gender,TransactionValue12,NPurchases12
51,USA,M,3516,1
58,USA,M,3308,12
57,USA,M,7360,19
54,USA,M,2052,6
51,USA,M,4889,5
57,USA,M,4746,6
50,USA,M,3803,3
58,USA,M,4113,24
57,USA,M,7421,17
58,USA,M,1774,24
50,USA,F,8984,5
52,USA,F,1436,22
52,USA,F,2137,9
58,USA,F,9933,25
50,Canada,F,7050,16
56,Canada,F,7202,5
54,Canada,F,2096,19
59,Canada,F,4639,9
58,Canada,F,5724,25
56,Canada,F,4885,5
57,Canada,F,6212,4
54,Canada,F,5016,16
55,Canada,F,7340,21
60,Canada,F,7883,6
55,Canada,M,5884,12
60,UK,M,2328,12
52,UK,M,7826,1
58,UK,M,2542,11
56,UK,M,9304,3
54,UK,M,3685,16
58,UK,M,6440,16
50,UK,M,2469,13
57,UK,M,7827,6
Desktop table:
Here you see an Input table and a subset table using two Slicers. If the forum suggestion gets implemented, it should hopefully be easy to change a subset like below to an "OR" scenario:
Transaction Value > 1000 OR Number or purchases > 10 using Power Query:
If you use Edit Queries > Advanced filter you can set it up like this:
The last step under Applied Steps will then contain this formula:
= Table.SelectRows(#"Changed Type2", each [NPurchases12] > 10 or [TransactionValue12] > 1000
Now your original Input table will look like this:
Now, if only we were able to replace the hardcoded 10 and 1000 with a dynamic value, for example from a slicer, we would be fine! But no...
I know this is not what you were looking for, but it was the best 'negative answer' I could find. I guess I'm hoping for a better solution just as much as you are!

Related

Conditionally format a table based on average of dataset, but adhering to slicer selections

I have a simple CSV data set such as this.
ID,MainCategory,SubCategory,Type,Value
1,E,E1,Demo,5
2,N,N3,Install,2
3,E,E1,Demo,4
4,E,E2,Install,7
5,D,D1,Install,3
6,S,S2,PM,4
7,N,N2,Install,7
8,N,N2,Demo,1
9,E,E2,Demo,2
10,D,D2,Install,6
11,D,D3,PM,4
12,S,S1,PM,8
13,N,N1,Install,5
14,S,S3,Install,8
15,S,S1,Demo,9
16,E,E3,Demo,5
17,N,N2,Install,3
18,E,E2,PM,6
19,D,D2,PM,6
20,N,N3,Demo,6
21,S,S2,Demo,7
22,E,E3,Install,2
23,S,S1,Install,4
24,S,S2,PM,8
25,D,D1,Install,5
In my Power BI Desktop, I'd like to load this into a table, and conditionally format the Value column based on whether the value in each row is greater than or less than the average for the currently selected data set.
For instance, the average of Value considering the entire table is 5.08, so if there are no filters applied (as in, all my slicers are set to select nothing), I'd like all rows whose Value is 6 or more to be background colored in one color, and the others in another color. For this, I created two measures like so:
AvgOfVal = DIVIDE( SUM(G2G[Value]), COUNTA(G2G[ID]) )
BGColor = IF(SUM(G2G[Value]) > [AvgOfVal], "Light Pink", "Light Blue")
Then I tried to apply the BGColor measure for conditionally formatting the background, but this doesn't work as expected, and instead produces the result below.
I realize that this is due to the fact that the measure is calculated per row, so when conditional formatting is applied, as seen in the AvgOfVal column in the table, it calculates average per row instead of for the entire data set. How can I calculate a measure that takes into account the entire data set (considering slicers), and do the conditional formatting as I need.
Please keep in mind that if a user were to select a slicer filter (say, MainCategory = D), then I want the conditional formatting to reflect this. So in this case, given that AvgOfVal = 4.80 for MainCategory = D entries, I'd like all rows whose Value >= 5 to be in one color, and others in another color.
I realize that this is due to the fact that the measure is calculated per row
Yes. The key is understanding how that happens. When the measure is calculated a "context transition" happens and the current row is added to the filter context.
So what you want is a calculation that removes the row filter that was added in the context transition. So you need ALLSELECTED(), which does precisely that. eg
AvgOvVAl = CALCULATE( AVERAGE('data'[Value]), ALLSELECTED() )
Removing the "innermost" filter which in this case is the filter on the row, but leaving all other filters, ie filters added on the report, page, visual, or filters coming from interactions with other visuals like slicers.

Tableau: percentage of total for specific dimension / COUNTD IF statement

I have an employee dataset and I'm trying to display the proportion of BAME staff only as a KPI. My dataset has duplicate lines (staff who have more than 1 role) and so I have to use a distinct count of their staff IDs (i.e. cannot use measure values or SUM) as I want to measure ethnicity per person not per role. The only way I can get the % of BAME across the population is by having all other categories visible however I am looking for a way to display the % of BAME across total only. For example in a table calc BAME = 12%, Non-BAME=85% and Unknown=3% - I want to keep the 12% only to put on my dashboard.
I've not needed to do this in Tableau before and I'm quite new to the software. Is there a way to do this? I thought that perhaps I would need to do an IF statement similar to Excel but the below syntax returns an error.
IF [Ethnic Group]="BAME" THEN COUNTD([Staff ID]) / COUNTD([Staff ID]) END
Thank you so much in advance!
COUNTD(IF [Ethnic Group]="BAME" THEN [Staff ID] END)/COUNTD([Staff ID])

PowerBI Matrix Visual - Replacing Blank with Zero (harder than I thought)

I am trying to replace blanks with zero in a matrix visual, but the traditional method of adding +0 is causing another problem. I have the case described below in detail. Thank you so much for any help anyone may be able to offer!
I have a (fictitious) company with 60 employees located in 5 regions (Midwest, Northeast, Pacific, and Southwest). Each employee holds an occupational type (such as chemist, auditor, geologist, truck driver, etc.). Across the entire company, there are 18 different occupational types.
Additionally, each region considers some of the occupations as critical and others as non-critical and the critical vs. non-critical occupation types vary by region. If the occupation is critical for a particular region, the occupational title (e.g. chemist) should appear in the visual and if the occupation is non-critical, the generic title ‘Non-Critical’ should appear instead of the occupational title.
To accomplish this, my PowerBI model has two related tables – employee list (dimension table/many) and occupation list (fact table/one). Each employee on the employee list has a match code that is related to the match code on the occupation list to determine if the occupation is critical or non-critical for that employee’s region. If the occupation is critical, the related field (that will be used on the row field of the visual will be the occupational title. If non-critical, the related field will be the generic title ‘Non-Critical’.
Here’s an example of three records from the employee list fact table:
Image A
And here’s an example of some records from the occupational list dimension table:
Image B
The purpose of the visual is to show the count of employees onboard at two points in time (called FY20 and FY21) by occupational type with a slicer to filter by region.
The employee count is produced using the measure =COUNTROWS(Employee List)
Everything works great at this point. Here is an example of the visual filtered to Midwest, which correctly shows the Midwest Region’s 10 critical occupations broken out by occupational title and the employee counts. (non-critical count also correctly shown)
Image C
And as a second example, here is the view filtered to the Pacific Region showing the Pacific’s 3 critical occupations (non-critical also correctly show):
Image D
My only goal with this visual is to display zero instead of a blank for those cases where there are no employees. When I modify the measure to:
=COUNTROWS(Employee List) + 0
I get the following result (filtering to Midwest for example):
Image E
So, the result is that the formula did replace the blanks with zeros, but now all the entire company’s 18 critical occupations are displayed and not just the 10 for the Midwest. The counts are still correct for the Midwest, but I only want to show the Midwest occupations as they were appearing correctly before I added +0 to the measure. If I try to simply filter them out at the visual level, then they will stay filtered when I switch region where they should be unfiltered.
It seems the behavior is that a blank being replaced by a value (0) means that when there is a combination for which there is no data (such as Midwest/Chemist), the visual will still show 0 as a result for that combination.
I’m looking for anything I can do to replace blanks with zero and not displace the occupation types that don’t apply for the region. I would appreciate any assistance as I’ve been thinking about this for hours and have hit a wall.
Thank you!
I suggest a measure on the following form, written verbosely:
# Employees w/ zeroes =
VAR _employees = [# Employees]
VAR _totalEmployees = CALCULATE ( [# Employees] , REMOVEFILTERS ( 'Employee List'[Year] ) )
RETURN
_employees + IF ( ISNUMBER ( _totalEmployees ) , 0 )
This will first check that the occupation type has employees for the selected filter context, and only tack on a zero if so. The column specified in REMOVEFILTERS() must correspond to whatever you are using in your visualization - it is used to modify the filter context.
It looks like a fairly simple (if possibly temporary) solution is available for this problem by using conditional/advanced filtering on the visual. I set the advanced filter to show when the value is not 0 and this seemed to take care of it. Thank you for the DAX code and I will explore those options as well.
Thanks again!

Obtain MAX of column and display for each row

I'm trying to obtain the MAX of a particular column in a Power BI Report and place this as a new Measure within each ROW of the same dataset. Please see the example below.
Is this possible in DAX and via DirectQuery/LiveConnection? The report is pointing to a tabular model but due to outside factors the measure must be created in the report.
Thanks
You can accomplish this a few ways. Essentially, you need override the filter context so that the MAX function isn't just running over whatever slice you're showing in the visual. Using CALCULATE or the iterator function MAXX, set the wrap the table in the ALL() function to override the context and calculate max over all rows.
= CALCULATE(MAX([Calendar`Year]), ALL('Smithfield_Fiscal_Calendar'))
or
= MAXX(ALL('Smithfield_Fiscal_Calendar'), [Calendar`Year])
To get the breakout by date, you'll need to include a Date table in your model. PowerBI makes this possible with a few different DAX options. As an example, go to your Model tab, click 'New Table' and put in the following expression:
MyCalendar = CALENDAR(DATE(2019,1,1), DATE (2019,1,10))
This is a little trivial -- you'd want to use a useful range of dates but this one matches your example above. Next, add a column to [MyCalendar]
CalendarMonthYear = month([date]) & "-" & year([date])
Go to your budget table and add a similar field
BudgetMonthYear = month([date]) & "-" & year([date])
Go into your Model view and create a relationship between CalendarMonthYear and BudgetMonthYear. This will associate every date in the date table with the particular budget row from your budget table.
Hope it helps.

Country is selected as "All" calculate a specific column value only

Need some advise on how to filter data when in "Country" column is selected as "All" then consider only specific value which is in different column(Raw) else calculate respective values(column Coverage) for respective Countries
There are 3 columns - Country, Coverage and Raw
Basically when selected as "ALL" it considers total of coverage column but I want it to consider a single value from Raw column
Note - I'm using this in Line chart in Power BI
Thank you in advance!
You can create measures that have different behaviors for different filter conditions using functions like ISFILTERED or HASONEVALUE. For example:
Measure = IF(ISFILTERED(Table1[Country]), <Calculation 1>, <Calculation 2>)
or
Measure = IF(HASONEVALUE(VALUES(Table1[Country])), <Calculation 2>, <Calculation 1>)