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!
Related
got stuck with this one for a while. Need to repeat 165608547,90 in a Separate Column. Simple AVERAGEX(Sales[Turnover]) does not work. Thank you for any help
This would be best achieved through using a DAX measure.
Avg Cost = CALCULATE(AVERAGE('YourTable'[Amount]), ALL('YourTable'))
As per the comments added, if you want existing slicers to be respected, use ALLSELECTED in place of ALL. In some cases ALLEXCEPT could also be a solution.
Note, this must be a measure and not a column.
This measure will provide the average for everything in the table when it is added as a field in the matrix/table. The ALL keyword removes any filters applied to the data during the calculation.
See DAX ALL
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.
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])
I run a simple query in SQL:
select count(*) FROM Abattoir.RecordScan_BloodPit
JOIN Abattoir.RecordScan RS ON RS.ScanId = RecordScan_BloodPit.ScanId
WHERE rs.HarvestDate = '07/16/2018'
which gives me a correct number of rows (say 2000)
I then go to my measure editor in Power BI, and enter:
CALCULATE(COUNT(BloodPit[ScanId]))
and get an insane number of around 254000000.
I don't understand why counting on a field so simply would give me a completely Different number.
New to DAX so my Google strings might not be the best when conducting searches.
Here is what the report looks like. Please note the nubers inside the rectangle are what I'm trying to calculate. The numbers vary from day to day...
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.