DAX suggestions to generate specific weighted average measure in PowerBI? - powerbi

I'm trying to formulate weighted average measures in PowerBi for survey data. I've currently got the following two tables (simplified):
Survey Data
ID How would you score the service? Do you agree with X?
---------------------------------------------------------------------------
23 Fair Agree
24 Poor Strongly disagree
25 Fair Agree
26 Very poor *blank*
27 Very good Strongly agree
Weights
Weight Score Likert
-------------------------------------------------
1 Very poor Strongly disagree
2 Poor Disagree
3 Fair Neither agree nor disagree
4 Good Agree
5 Very good Strongly Agree
There's currently a relationship between 'surveydata'[How would you score the service?] and 'weights'[weight].
The weighted average formula I'm trying to calculate is the following:
(x1 * w1) + (x2 * w2) + ... (xn * wn)
Weighted Ave = ___________________________________________
x1 + x2 + ... xn
Where:
w = weight of answer choice
x = response count for answer choice
For the example above, I would need two measures - a weighted average for 'surveydata'[How would you score the service?] and one for 'surveydata'[Do you agree with X?].
For the example above, the weighted average measure for 'surveydata'[How would you score the service?] should be 2.8.
Note that one of the complications is the fact that there are *blank* cells.
Can anyone suggest a way of doing this in PowerBI with calculated measures (or otherwise)?

You can sum the weights for each row in your SurveyData table and divide by the number rows
Weighted Ave =
DIVIDE(
SUMX(SurveyData, RELATED(Weights[Weight])),
COUNTROWS(SurveyData)
)
Or simply use an average function
Weighted Ave = AVERAGEX(SurveyData, RELATED(Weights[Weight]))

Related

How to calculate ratio for every row in power bi?

Could someone please guide me through this situation?
I have a need to compute for every row the ratio between, volume of the row and sum of volumes of rows of a given transport type.
But after calculating this calculated column, I would like to use it in a table visualization, and be affected by any slicer of it...
Was I clear?
Here follows the example of rows:
Delivery Method Order Nr VOLUME Ratio
Air 50102258 9 33%
Sea 50091716 50 52%
Sea 50092425 47 48%
Air 50102257 18 67%
Here's your measure (a calculated column doesn't work here since you have to aggregate per method):
% Volume =
DIVIDE(
SUM('Table'[VOLUME]),
CALCULATE(
SUM('Table'[VOLUME]),
ALLEXCEPT('Table','Table'[Delivery Method])
)
)
If the answer was useful please accept it and give it an upvote.

How to calculate average win rate among all the student using DAX in PowerBI

Hi everyone,
I have a sample data as shown in the screenshot above. There are 3 students with different ID : student 123, student 234, student 456. The profit column in the table is how much they earn in each trade.
Winning trade = profit > 0
Losing trade = profit < 0
Based on the definition above for the winning trade and losing trade, I want to calculate the average winning rate for all the students.
Student 123 - the winning rate is 50% (2 negative profit and 2 positive profit)
Student 234 - the winning rate is 33.3% (2 negative profit and 1 positive profit)
Student 456 - the winning rate is 100% (0 negative profit and 2 positive profit)
So, the final answer, average winning rate among all the students is:
(50% + 33.3% + 100%)/3 = 61.1%
61.1% is the final output that I want, then I will put this value into a Donut chart. I'm relatively new to DAX, any help or advise will be greatly appreciated!
Please paste text rather than images when providing sample data.
You shouldn't really add averages together like that but if that is definitely what you want, use Measure 2.
If you want a more traditional average to be calculated, use Measure 1.
Measure 1 =
VAR total = CALCULATE( COUNTROWS('Table'), ALLEXCEPT('Table','Table'[Student]))
VAR pos = CALCULATE(COUNT('Table'[Profit]), ALLEXCEPT('Table','Table'[Student]),'Table'[Profit] > 0)
RETURN pos/total
Measure 2 =
VAR students = CALCULATE(DISTINCTCOUNT('Table'[Student]), ALLEXCEPT('Table','Table'[Student]))
RETURN SUMX(VALUES('Table'[Student]), [Measure 1]/students)

Power BI - How to calculate percent difference between two different values of one column within a matrix

Power BI newbie here, which is probably obvious. In the example of a matrix below, Term 1 and Term 2 are values from a variable called "Term" that I've placed in the Columns box. I'd like to add the "% Change" column as seen below, but I'm having trouble doing this. It seems like there would be an easy way to do this either with DAX or built-in options, but I'm unaware due to lack of experience. Any help would be much appreciated. Thanks!
ROW:
Campus
COLUMN:
Term
VALUES:
Count
Campus
Term 1
Term 2
Percent Diff
Camp A
100
900
__%
Camp B
1400
2600
__%
The measure you are looking for is
Percent Diff =
DIVIDE (
SUM ( 'Table'[Term 2] ),
SUM ( 'Table'[Term 1] )
) - 1
The point is that measures only work with aggregations, SUM in this case.
Try this :
= SUM([Term 1])/((SUM([term 1])+SUM([Term 2]))/2)

How does forecast analytic in Power BI work?

I have created the following forecast, Where:
Total Sales = CALCULATE(SUM(Data[Amount]), Data[Operation] = "SALE")
Total Sales Return = CALCULATE(SUM(Data[Amount]), Data[Operation] = "Return")
Sales Return Ratio = DIVIDE([Total Sales Return], [Total Sales])
I need to understand while Sales and Sales Return trends being stable in the forecast, why is Sales Return Ratio is going up?
What is the behind the scene logic for predicting forecast in Power BI?
Microsoft writes about it here.
They use an Exponential Smoothing algorithm. I did a quick comparison and it seems to be the same algorithm used in Excel's FORECAST.ETS() where ETS stands for Exponential Triple Smoothing.
I will not even try to explain that algorithm here, but I found this blog post that looks promising.

How to do column wise intersection with itertools

When I calculate the jaccard similarity between each of my training data of (m) training examples each with 6 features (Age,Occupation,Gender,Product_range, Product_cat and Product) forming a (m*m) similarity matrix.
I get a different outcome for matrix. I have identified the problem source but do not posses a optimized solution for the same.
Find the sample of the dataset below:
ID AGE Occupation Gender Product_range Product_cat Product
1100 25-34 IT M 50-60 Gaming XPS 6610
1101 35-44 Research M 60-70 Business Latitude lat6
1102 35-44 Research M 60-70 Performance Inspiron 5810
1103 25-34 Lawyer F 50-60 Business Latitude lat5
1104 45-54 Business F 40-50 Performance Inspiron 5410
The matrix I get is
Problem Statement:
If you see the value under the red box that shows the similarity of row (1104) and (1101) of the sample dataset. The two rows are not similar if you look at their respective columns, however the value 0.16 is because of the term "Business" present in "occupation" column of row (1104) and "product_cat" column of row(1101), which gives outcome as 1 when the intersection of the rows are taken.
My code just takes the intersection of the two rows without looking at the columns, How do I change my code to handle this case and keep the performance equally good.
My code:
half_matrix=[]
for row1, row2 in itertools.combinations(data_set, r=2):
intersection_len = row1.intersection(row2)
half_matrix.append(float(len(intersection_len)) /tot_len)
The simplest way out of this is to add a column-specific prefix to all entries. Example of a parsed row:
row = ["ID:1100", "AGE:25-34", "Occupation:IT", "Gender:M", "Product_range:50-60", "Product_cat:Gaming", "Product:XPS 6610"]
There are many other ways around this, including splitting each row into a set of k-mers and applying the Jaccard-based MinHash algorithm to compare these sets, but there is no need in such a thing in your case.