Getting measure to only show positive values in matrix Power BI - powerbi

I'm new to DAX. Looking for your expertise. Looked all over the web but couldn't find a proper solution.
I have a matrix that shows several factories and it's inventories, the rows being factory and SKU. I've created a measure that's the difference between two other measures. The overall idea is: I have a inventory quantity and an orders volume. I defined a "balance" measure that's inventory - sales. That is, what is still left for sale.
Normally it works fine, but when I have a negative value in balance, things get messed up. The subtotal in the factory row sums all the numbers, including negatives, giving me a balance subtotal that is smaller than it's supposed to be. I can't count the negatives. So I've tried to solve this by defining the measure as
Balance = IF([Inventory]>[Sales],[Inventory] - [Sales],BLANK())
Now this works fine visually to display only the positive values in balance, but still sums up the negatives in the factory subtotal.
How can I make this measure to only show and sum up the positives?
I appreciate your help.
P.S.: the inventory and sales measures are basically SUM's of different tables for simplicity and understanding by my part
Matrix with measures. The columns are "Estoque" as inventory, "Embarque programado total" as sales and "Saldo Disp. Venda" as the balance

Create a measure with the below code.
Balance = CALCULATE(SUM([Inventory]) - SUM([Sales]), FILTER(Table1, [Inventory]>[Sales]))

As it turns out, I was able to find a solution through trial and error. I changed my measure to (FatoEstoque is the table that contains the inventory numbers):
Balance= CALCULATE([Inventory]-[Sales],FILTER(FatoEstoque,[Inventory]>[Sales]))
Although I'm not even sure why it works (lol), it works! So I'm not complaining. This measure only sums up the positive values, showing the correct subtotal as I needed.
Filtering the Inventory table to values where [Inventory]>[Sales] works, but adding another filter with the Sales table and the same condition don't.
Here is an image comparing the results. The new measure is called SaldVender2

Related

Modify measure to only affect Totals in Matrix Table

I'm new to PBI Desktop & StackOverflow in general. If anyone can help me out with this situation, much appreciated.
I created this matrix table where it displays the count of male/female students in a student division (I just made up these numbers). I created a measure called 'Percentage of males' that calculates the number of male students and divide it by the total number of students in each division.
The calculation wasn't, the hard part, but the issue is when I place the measure in the values field. Instead of just wanting to show it for totals only, it appears under the female & male subcategories as well (refer to image). Does anyone know if there's a way to modify the measure so that it only affects the total?
I tried looking at some videos, but couldn't really find one for this situation.
Proportion of Males = DIVIDE(
COUNTX(
FILTER(sheet2, Sheet[M/F]="M"), Sheet2[M/F], COUNTA(Sheet2[M/F]), 0)

Incorrect Totals in Power BI Table for MAPE

I'm fairly new to Power BI and struggling with an issue around totals in a table.
I am trying to calculate Mean Average Percentage Error (MAPE) using the following calculation:
[ABS(Actuals - Forecast)/Actuals]
Below is my dataset:
The total in the 'MAPEX' Column is actually the sum of the totals in 'AbsErr' / 'Actuals' columns: (1457.27 / 2786.27 = 0.52).
What I actually need to show is the sum of the values in 'MAPEX' which totals 5.88.
The 'MAPEX' column is a Measure with the following definition:
MAPEX = DIVIDE([AbsErr], sum(CUBE_PeriodicData[Actuals]),0)
I do not need to show the correct total in the 'Total' row in the table, it can be placed elsewhere in the report as a card, I would just like to know if there is a function in DAX that I am unaware of which will total the values in the column vertically?
Seymour's answer looks to be good, but I'm here to add a little that granularity matters in this scenario.
Assuming you have a star schema like this, it is pretty straightforward you can define measures Total Forecast, Total Actual, Absolute Error, and Absolute Percentage Error with below formulas.
Total Forecast = SUM ( Forecast[Forecast] )
Total Actual = SUM ( Actual[Actual] )
Absolute Error = ABS ( [Total Forecast] - [Total Actual] )
Absolute Percentage Error = DIVIDE ( [Absolute Error], [Total Actual] )
Here is what you will get so far.
Here, you are asking how to calculate the sum of Absolute Percentage Errors.
By definition, Absolute Percentage Error shows the value of Absolute Error divided by Total Actual regardless of the drill-down level. Therefore at the grand total, it shows 0.52 which is Absolute Error (1,457.27) divided by Total Actual (2,786.27). If you want it to calculate differently in the grand total level, you need to explicitly implement this logic.
Your requirement would be stated more explicitly like below:
Calculate the values of Absolute Percentage Error in the granularity of each ItemName, Year, and Month.
And add them up.
The function you will need to implement this logic is SUMX. Also, you may explicitly use SUMMARIZE to make sure you are calculating Absolute Percentage Error in the specific granularity.
MAPEX = SUMX (
SUMMARIZE (
Forecast,
'Product'[ItemName],
'Calendar'[Year],
'Calendar'[Month]
),
[Absolute Percentage Error]
)
I have been emphasizing about the granularity so far. This is because if you are not conscious of the granularity, the result may look strange in some cases.
In the above image, MAPEX looks to be the same as Absolute Percentage Error except for the grand total. However, if you drill-down by Quarter instead of Month, you will notice it is not the same at all.
Absolute Percentage Error is showing the quotient of Absolute Error and Total Actual at quarterly level, whereas MAPEX is still summing up monthly values of Absolute Percentage Error, even though Month is not being displayed in the table.
So, my final word is, whenever you invent a new measure like MAPEX, you always need to ask yourself if it makes sense or not for every possible granularities.
One way to solve this would be to use a custom column titled MAPEX instead of a measure that does your calculation. If there is a particular reason you need to use DAX please feel free to let me know and I may be able to figure something out.
Column = ABS(([Actuals]-[Forecast])/[Actuals])
EDIT: Just in case, the way you create a new column is with this button in the view tab.
Alternatively, you can create the custom column from within the query editor which appears to be working for me.
Go with this
VAR _mytable = SELECTCOLUMNS(FactTable, "MAPE", ABS(Actuals - Forecast)/Actuals))
Return
Sumx(_mytable, [MAPE])

How do you create a calculated cumulative total value column in a power bi matrix visualization?

I am pretty new to Power BI, but I'm learning alot fast. I do have one challenge at this point that has me stumped and I'm really not finding anything online that relates very closely.
I have created a basic matrix visualization that gives me sales totals by UPC code. The UPC code is in the Rows and the sales total is a Values column. I added a second instance of the sales as another Value to be calculated as a percent of the column total.
After sorting, I get a nice pivot table style look at the top selling UPC codes.
For the life of me, I can't figure out how to add a calculated column that will give me a cumulative total of the percent so I can do a Top 80 (80/20) analysis. I'm hoping someone can point me in the right direction.
To be completely clear, I want a column to the right of the '%CT Sold Total' column that gives me '14.40%' on the first row, '27.46%' (14.40% + 13.06%) on the second row, '36.41%' (27.46% + 8.95%), and so on.
Thanks in advance!
Finally wrapped my head around this (powerbi.tips/2016/10/pareto-charting) and it is exactly what I was trying to do.

Measure to sum another aggregated measure's data

I am working on a report that has data by month. I have created a measure that will calculate a cost per unit which divides the sum of dollars by the sum of production volume for the selected month(s):
Wtd Avg = SUM('GLData - Excel'[Amount])/SUM('GLData - Excel'[Production])
This works well and gives me the weighted average that I need per report category regardless of if I have one or multiple months selected. This actual and budget data is displayed below:
If you take time to total the actual costs you get $3.180. Where I am running into trouble is a measure to sum up to that total for a visual (This visual does not total sadly). Basically I need to sum the aggregated values that we see above. If I use the Wtd Avg measure I get the average for the total data set, or .53. I have attempted another measure, but am not coming up with the correct answer:
Total Per Unit Cost = sumX('GLData - Excel','GLData - Excel'[Wtd Avg])/DISTINCTCOUNT('GLData - Excel'[Date])
We see here I return $3.186. It is close, but it is not aggregating the right way to get exactly the $3.180:
My Total Per Unit Cost formula is off. Really I am simply interested in a measure to sum the post aggregated Wtd Avg measure we see in the first graph and total to $3.180 in this example.
Here is my data table:
As you probably know already, this is happening because measures are dynamic - if you are not grouping by a dimension, they will compute based on the overall table. What you want to do is to force a grouping on your categories, and then compute the sum of the measure for each category.
There are 2 ways to do this. One way is to create a new table in Power BI (Modeling tab -> New Table), and then use a SUMMARIZE() calculation similar to this one to define that table:
SUMMARIZE('GLData - Excel',[Category],[Month],[Actual/Budget],"Wtd Avg",[Wtd Avg])
Unfortunately I do not know your exact column names, so you will need to adjust this calculation to your context. Once your new table is created, you can use the values from that table to create your aggregate visual - in order to get the slicers to work, you may need to join this new table to your original table through the "Manage Relationships" option.
The second way to do this is via the same calculation, but without having to create a new table. This may be less of a hassle. Create a measure like this:
SUMX(SUMMARIZE('GLData - Excel',[Category],[Month],[Actual/Budget],"Wtd Avg",[Wtd Avg]),[Wtd Avg])
If this does not solve your issue, go ahead and show me a screenshot of your table and I may be able to help further.

COUNT the number of times a DAX measure is > X

New to DAX.
I have a rather (from my point of view) complicated tables relationship model :
Model
It's a knowledge database in my company.
There are many "compétences" for each employee in table DONNEES. These "compétences" are categorized in the LMC table which is a bridge table between COMP & METIERS defining the "compétences" needed for a "métier".
One "competence" can be in multiple "métiers"
In table "DONNEES" I have all employees, plus standards profils for each "métier". This table defines the level (1 to 5) for each "competences".
My matrix in Power Bi works like a charm. I can sum all "competences level" for each métiers.
I've added a measure, which normalizes this sum over the sum gotten for each standard profil (this sum is "hard coded" in METIERS, not calulated)
Grade = CALCULATE(SUM(DONNEES[Niveau]); LMC)/CALCULATE(SUM(METIERS[Niveau_type]);LMC)*5
And it works (even if the "total" columnn isn't correct)
//EDIT//
With the following modifications, it works :
Grade = CALCULATE(SUM(DONNEES[Niveau]); LMC,DONNEES,METIERS)/CALCULATE(SUM(METIERS[Niveau_type]);LMC,DONNEES,METIERS)*5
(I've added all tables from my many to many relationship)
The grand total column is now ok.
//EDIT//
Now, I want to compute a the number of employee that fulffil a specific grade for each métier.
I've thought to create a binary measure that checks that :
gradefulfilled= IF(DONNEES[Grade]>4,5;1;0)
An then use the total row to have the number of employees in the company able to do that "métier"
It doesn't work. The total sticks to zero, even if the gradefulfilled binary is correct for everyone.
//EDIT//
The total does'nt stick to zero, it can be 1 if there's one employee OK, but sticks to 1 if more than one employee.
//EDIT//
You can propose either a totally different way to compute what I want (number of employees able to do a "métier" or explain to me where I made a mistake in my computations.
Hurray !
My bad. Problem seems solved.
The problem seems related to the way grand total is calculated in pivot table (the sum of the measure calulated for each line, not on the sum of each measure in the matrix)
So I've used a sumx to get a proper sum :
Nbgradefulfilled = sumX(NOMS;DONNEES[gradefulfilled])