PowerBI Matrix Visual - Replacing Blank with Zero (harder than I thought) - powerbi
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!
Related
Power BI - Use Column name to create Row and attach Value from that Row to it
I have a table from a survey that reports the score given to a specific employee, and various columns are there to hold each score for each question. Like this table below: Now, what I want to do is make a row for each question, and in the original table, each question is a column. And I'd like for example, John, to have 1 entry for each question, and the average of that score stored next to each question like in this table here: . This shows clearly what I'm aiming for. I believe I need some sort of pivot or unpivot table going on, but I'm not too sure on the Power BI DAX syntax for creating a new table. I currently have a table that provides each Employee once, and columns showing their average score for each question, but that is a bit harder to dice up the way I want to. Code pasted below: ReportTable = SUMMARIZE( ALL ( '360Sample'[Name], '360Sample'[Relationship to person being reviewed]), '360Sample'[Name], '360Sample'[Relationship to person being reviewed], "Employee Satisfaction", DIVIDE(CALCULATE(AVERAGE('360Sample'[Treats others with respect/Truly values employees]))+AVERAGE('360Sample'[Encourages and supports staff in developing their skills])+AVERAGE('360Sample'[Provides effective mentoring]),3), "Quality Product", DIVIDE(CALCULATE(AVERAGE('360Sample'[Consistently strives to provide products above industry quality standards]))+AVERAGE('360Sample'[Takes ownership of project outcome])+AVERAGE('360Sample'[Ensures quality control always happens on products]),3), "Client Satisfaction", DIVIDE(CALCULATE(AVERAGE('360Sample'[Fosters open, honest, and consistent communication with clients]))+AVERAGE('360Sample'[Responds quickly to client questions and concerns.])+AVERAGE('360Sample'[Successfully communicates contractual needs and requirements with the Client, including schedules and scope and fee increases]),3), ) Only difference here is that I have an extra attribute of "Relationship" which I'll also need to include but is less important for now. It makes a row of each employee for every unique Relationship, which is 2.
Hello You need to first use the "unpivot" in Power Query to convert your table into this shape: It is not so hard. Like this: Then use this DAX Code: ReportTable = ADDCOLUMNS ( SUMMARIZE ( 360Sample, 360Sample[Name], 360Sample[ScoreNum] ), "ScoreAvg", CALCULATE ( AVERAGE ( 360Sample[Score] ) ) ) And It produces:
Trying to replace data based on attributes from two other columns
I need to change the inventory category for a couple of account numbers and only for a couple of companies. The inventory category for these accounts are mapped based on the account number but need to be changed specifically just for two companies. I've tried to filter by the company number and then find/replace, which worked fine, but then I can't unfilter to bring back the rest of the companies. I can't change the category for just those account numbers because it is only different for just those two companies.
Lisa, Here's perhaps a simpler approach than where your current way is taking you. If I begin with this table: Then I add a column (Add Column -> Custom Column) with the following: The formula uses an if statement to determine whether each row has a specific Account (Acct. 4) AND Company (Co. 8). If so, then 99 is returned as a new category value for that row of the new column. If not, then the original Inventory Category is returned as a value for that row of the new column. (Obviously, you would edit this formula accordingly, to support your account, company, and new inventory category values.) Here's the result: Then I could delete the original Inventory Category column and rename the remaining New Inventory Category column to Inventory Category.
Power BI - How can I create a measure aggregation from two tables?
I've got two tables Fauna_Afetada (animals affected) e Municipios (all cities of the country). A fauna afetada tem a sigla_uf (state initials which the city belongs to) e nome_municipio (name of the city), as well as the Municipios table. I'd like to create a measure counting the number of deaths (Situação_Int = 0 in table Fauna_Afetada, specify that the animal died) by city (table Municipios). The way to join these tables is by using two columns (the sigla_uf and nome_municipio), because each Municipio in Fauna_Afetada might appear several times. How do I do that? I'd like to create a measure with the name of the city with the highest number of deaths. Anyone could help? The data model is below.
It seems it is a data modeling issue in the first galce, The model is full of bidirectional relationships that lead to not expect calculations behaviors. Can you share the file or part of it
How to hide blanks in a matrix visualization with hierarchical rows
I've built a table of data following this helpful guide: https://www.daxpatterns.com/parent-child-hierarchies/ I'm following it exactly but I'll still explain things here so you don't have to go through the whole article if you don't want to. I have a table of Names with corresponding keys, and ParentKeys forming hierarchies. I added a column for the path, columns for each level of the path, depth of hierarchy and an IsLeaf column: If I want to make a matrix and include City (from another table), all hierarchies will expand to the maximum length, and blanks are filled in with the "parent's" name: The DAX Patterns website explains how to get around this. First add these two measures: BrowseDepth = ISFILTERED (Nodes[Level1]) + ISFILTERED (Nodes[Level2]) + ISFILTERED (Nodes[Level3]) MaxNodeDepth = MAX (Nodes[HierarchyDepth]) And then you can factor that into calculations with this measure: Sales Amount Simple = IF ( Nodes[BrowseDepth] > Nodes[MaxNodeDepth], BLANK (), SUM (Transactions[Amount]) ) If this is the only value on a matrix visual, it turns out fine: But if I add any other values, I get expanded hierarchies and blanks again: My problem would be solved if I could filter out blank values, but that filters out the entire hierarchy. Do I have to make a measure using the Sales Amount format above for every value I want to include? I'm trying to add things like addresses that can't be aggregated.
Basiacally yes, you have to re do the measure. However you can embed existing into this patern which makes it a little easier.
Power BI Dashboard where the core filter condition is a disjunction on numeric fields
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!