Incorrect Totals in Power BI Table for MAPE - powerbi

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

Related

power bi measure with 2 conditions 2 calculate

I am trying to create a measure which will calculate the Total Project Revenue
while I have 2 different projects. For each project there is a different calculation:
for Hourly project the calculation should be: Income * BillHours
for Retainer project the calculation should be: Income*TotalWorkinghours
I wrote the below DAX:
enter code here : Total project revenue = IF(max(DimProjects[ProjectType])="Hours",
max(FactWorkingHours[Income])[BillHours],max(FactWorkingHours[Income])*
[Total Working Hours])
the rows are calculated correctly but the total in the table is wrong
what should I fix in DAX so the total of all raw will correct as well.
The total Revenue should be 126,403.33
Thank you in advance
you can find here the table with the results
It's hard to say exactly what your measure is doing because, as written here, that is not a valid measure. I pasted it into the DAX Formatter, which I would recommend for formatting and pasting here into code blocks, and the measure was invalid. It would also be helpful to post the other measures this measure references, eg. [Bill Hours] and [Income Hours].
That being said, I think I can kind of tell what's going on. Your total is probably wrong because the filter context at the total level is being based of the condition where:
MAX ( DimProjects[ProjectType] ) = "Retainer" (or some other value not in your shared snippet)
That is because when you consider the MAX of a string, the higher alphabetical order is considered. Therefore, "Retainer" > "Hours". So at the total level, your table is outputting—more than likely, I can't be certain without more information—the false condition of your measure:
MAX ( FactWorkingHours[Income] ) * [Total Working Hours])
There is a better way to handle your intended outcome. IF statements, in the way you are attempting to use it, are rarely used in a calculated measure. You may be better off trying a calculated column without using the MAX functions. Again, I can't give an exact code recommendation without more context. Hope that sends you in the right direction!

How to create a measure that goes first through equipment and then sums it all

I have a database that has some values as "Date", "StopedTime", "PlannedProductionQtt" and "PlannedProductionTime". These values are sorted by equipment, as the little example below.
What I need to do is divide PlannedProductionQtt by PlannedProductionTime and then multiply by StoppedTime. After this, I want to make a graph that shows it day by day.
At first I thought it was easy, made a new measure PlannedProductionQtt/PlannedProductionTime = SUM(PlannedProductionQtt)/SUM(PlannedProductionTime) (assume it worked without the table name).
And then I did another measure Impact = SUM(StoppedTime)*PlannedProductionQtt/PlannedProductionTime.
When I plotted a clustered column chart with this measure in values and a the day for the axis, at first I thought I had nailed it, but no. The BI summed all of PlannedProductionQtt and divided by the sum of all PlannedProductionTime for the day, and multiplied by the sum of the StoppedTime of that day.
Unfortunately, this gives me wrong results. So, what I need is a measure (or some measures) that would make it equipment by equipment and the sum it by day.
I don't want to make new tables or columns for theses calculations because I actually have 32 items of equipment, 3+ years of data, more than 1 classification of StoppedTime and the databases for PlannedProduction use more than one line per day per equipment.
To make it clear I added one column as Impact to show the difference.
So, if I sum the column Impact per day, I would have for day 1,2 and 3 the results 110725, 61273 and 220833.
However, if I sum first all the PlannedProductionQtt for day 1, divide it by the sum of PlannedProductionTime of day 1 and multiply it by the sum of StoppedTime of day 1 (which is how PowerBi is calculating) I will have 146497.
I inserted the difference in the table below to make the differences clear:
As Jon suggested in a comment, here is what solved my needs:
measure_name = SUMX( source_table , DIVIDE ( source_table[PlannedProductionQtt] , source_table[PlannedProductionTime] , 0 ) ) * SUM( source_table[StoppedTime] )
You have two different types of data you want to divide there, time and int, so you would probably need to unify that. Easiest way to do it would be from the Transform data panel, selecting the column and changing its
format
The division is done fairly easily, can you try creating a new measure as follows
measure_name = CALCULATE(
DIVIDE(<source_table>[PlannedProductionQtt],
<source_table>[PlannedProductionTime],
0)
* <source_table>[StoppedTime]
)
Then it's only a matter of using it as values in a graph and the 'Date' column in x axis.

HASONEFILTER SUMX total not calculating correctly

I am creating a Power BI measure that sums up averages so I have used the HASONEVALUE SUMX method but the total doesn't match what the actual sum would be if you just add up the information. Here is the measure:
And here is the results:
The total shows 31,654.25 but if you add up the rows you actually get 22,962.33. I am wondering if there is something wrong with my measure or if it is an issue of me not realizing it is pulling in additional information I'm not aware of.
This is calculating the average over all of the selected contracts and then summing that same value for each selected contract. (When you define a variable, it's treated as a constant in the remainder of the measure definition.)
Adding to #Alexis Olson, the average in a row is for the group, the total count is for entire datatable.
Below table is grouped by column A. Sum of averages is not equal to total average

Average of calculated measure PowerBi

I'm new to DAX and Power Query. For days I've been having troubles with what should be a simple calculation. I need a solution for this problem, either using DAX or Power query or both.
I prepared the following example dataset to explain my case:
As you can see, I have a date column which contains date values of the 1st day of every 3 months (representing year quarters). The second column contains integer values.
I need to get the interannual variation of those values, and then, the average over those results.
Calculating interannual variation is pretty simple, I just made a calculated measure with the following formula:
int_variation = IF(
ISBLANK(DIVIDE(SUM(Data[Value]),CALCULATE(SUM(Data[Value]),SAMEPERIODLASTYEAR(Data[Quarter])),BLANK())),
BLANK(),
(DIVIDE(SUM(Data[Value]),CALCULATE(SUM(Data[Value]),SAMEPERIODLASTYEAR(Data[Quarter])))-1)
)
Getting the following results:
Now, the problem comes when I try to get the average of those calculated interannual variations.
I've tried using AVERAGEX DAX function like this:
avg_int_variation = AVERAGEX(Data,[int_variation])
Also tried adding VALUES function as suggested here:
avg_int_variation = AVERAGEX(VALUES(Data[Value]),[int_variation])
But returns a blank value in both cases:
Since the query must be dynamic (that means number of quarters may change) is not that easy as querying a sum of the last 4 values and divide the result by 4. What I need is a formula that takes all values in the dataset, calculate the interannual variation of all of those values(I guess that is done so far) and then return the average for those values regardless of how many values ​​are.
Important facts:
• In addition to the average I also need the standard deviation of the interannual variation values.
• I must use only PowerBi.
Example of the final result I'm looking for (done in Excel):
As I said above, a DAX and/or Power Query solutions are viable. Could you make it?
You can download my sample PowerBi report here if you want to use it.
Thanks in advance.
use the following measures to calculate the avg and standar deviation:
Average:
AVG = AVERAGEX( VALUES( Data[Quarter] ), [int_variation] )
Standar Deviation:
ST = STDEVX.S( VALUES( Data[Quarter] ), [int_variation] )
Hope it helps you.

Annual Percentage Change in Power BI

I am looking to calculate an annual percentage change, from today of a number of stock market indices. The data is a flat structure un-pivoted about the date column.
I am using a calendar with daily values extending to the end of the current year. My current code:
Ann pch =
VAR
__EarliestValue = CALCULATE(SUM('Equity Markets (2)'[Value]),
SAMEPERIODLASTYEAR( 'Calendar'[Date]))
VAR __LastDateValue = SUM('Equity Markets (2)'[Value])
RETURN
CALCULATE(
DIVIDE(__LastDateValue,__EarliestValue)-1)
returns answers which are clearly incorrect.
I am very much a DAX/PBI novice.
DATA:
I also have a Calendar table:
I think that the metric is actually correct, what makes you believe it is not? Can you share your output?
What you need to do now is to put it into a context, ie put 'Calendar[Date]' into the desired visual (table for instance). If you don't do so, you'll calculate the total amount of value divided by the total amount of value for dates prior to one year, which might return quite a weird number. If you do so, on the other hand, you should get your YoY comparison.
Something to note - there are also quick measures:
Check them out, as one of them is actually YoY total, which you might want to try.