How do I calculate standard deviation from a measure? - powerbi

I am trying to calculate the standard deviation from a hospital Average Daily Census report. The report has by floor and by unit. The raw data is midnight census events for each patient...hundreds every day. I also have a filter on the report for different clinical services so the standard deviation needs to calculate "on the fly" as I change the filter.
The first picture below shows the results unfiltered. The second shows the results with some services selected.
I have found one way to calculate deviation but it has to be from a specific field. Since my ADC itself is calculated, this does not work.
I also saw how you can create a table (DAX?) but have not been able to get that to work and not sure it can be dynamic and calculate after filtering.
Is what I am trying to do even possible in Power BI?
Thanks

It sounds like you want the standard deviation of ADC over time at a daily granularity.
If this is correct, the basic approach is to calculate the measure for each day and then take the standard deviation on that set. In DAX, this will look something like this:
StdDevADC =
STDEVX.S (
SUMMARIZECOLUMNS ( DateTable[Date], "ADCThisDate", [ADC] ),
[ADCThisDate]
)
Even if this isn't exactly what you need, it should give you an idea of how to approach this. You need to calculate [ADC] for each element of the dimension you want to take the standard deviation over and then use the iterator version of the Standard Deviation function to calculate over that table/list you just calculated.

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!

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.

I wanted to calculate monthly weighted average in powerBI

I have a table that has PIMS code of crudes and first day of the month, its cost(Rs/MT) and quantity Thousand MT to be processed.
I need to calculate the weighted average of cost(Rs/MT) based on PIMS code and for that month only.
In the table, as you can see there are double entries of PIMS code with different quantity and price but with the same date and that difference needs to be considered while doing the average so I want to get a weighted average.
You can create a Weighted Average measure using the Quick measure functionality. You can do a basic google search for this as well.
Here is the documentation for creating a Quick Measure.
The following should work (Assuming your table is called 'Data')
Weighted Avg:=
VAR TotalUnits=SUM(Data[Quantity (TMT)])
VAR TotalCost=SUMX(Data,Data[cost(Rs/MT)]*Data[Quantity (TMT)])
RETURN
TotalCost/TotalUnits
Screenshot below to show examples with some dummy data

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 to build a matrix, similar to the table from Google analytics

In PowerBI I'd like to build Non-standard matrix very similar to the report in Google Analytics.
What do I have now:
I want to change my subtotal to measure, which is calculated as the difference in percentage of the two values
What I want to get:
In Power BI, there is no way to override the subtotals of a matrix with a calculation. Part of the challenge is that you know there are only two date ranges, but as far as Power BI is concerned, there could be any number of date ranges.
It's difficult to tell from your question exactly what input you have and what output you're looking for. Further, the numbers in your screenshots are obscured. However, one consideration would be to solve the problem using measures (i.e. a measure representing the first date range, a measure representing the 2nd date range, and then a measure calculating the difference between them). You may need to change the layout of your visual a little to make this work and the specific design would depend on how static your date ranges are.