DAX AVERAGE % SAVED IN A MATRIX FOR GROUP AND SUB-GROUP (ENTITY X USERS) Powerbi - powerbi

I need the average % discount of a group of condominiums taking into account the following rule
Add the monthly average totals of discounts for each person, and divide by the total nr of months of all the people in the condominium, that is, if person A made transactions in 4 months, and B made it in 2 months, and C made it in 4 months, the total number of months is 10. So, to get the average, just take the accumulated total of the % saved and divide by these 10 months.
I have the correct average for people (sum of monthly averages, divided by the number of months), using the following DAX codes
Redução Tx do Usuário =
AVERAGEX(
KEEPFILTERS(VALUES('Dcalendario'[MesAno])),
CALCULATE([% ECONOMIZADO USUARIO])
)
where % user saved is the following DAX measure
% ECONOMIZADO USUARIO =
IF(
NOT(HASONEVALUE(clientes[Nome])),
AVERAGEX(
VALUES(clientes[Nome]),
[media economizado tx]
),
[media economizado tx]
where the average saved is the division of the penultimate column by the second, on 11.08.2021, the value is 0.10 divided by 325.00 = 0.10%
In the case of the image that I present to improve understanding, the % discount for Alcantara (condominium) is presented as 1.5%, when the correct one would be 1.12%
The total average of users, in this case, is 11.25%, divided by 10 months, which is the sum of the months in which transactions occurred by all users of this condominium, giving an average of 1.12%, the final objective is to have the average of each condominium and of the other condominiums that participate in the calculation, keeping the information of the average per user, and per month (both are already ok).
Value is wrong at condominium level
In this case, for the Alcantara calculation to be ok, I have to add the % of Aline + the % of Edevilson and 2 other people, which will total 11.25 and divide by 10 months (Aline's 4 months, Aline's 2 months Edevilson and 2 other people with 2 months each).
How should the DAX code be correct, so that the group total is ok, keeping the average number of people who are already ok?
And how can I get the total of these months (sum) to present on a card, I want the grand total of months of all transactions (sum of the months of each user).

Related

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, calculate average over specific weekdays in the last month

I have the following data:
My goal is to create the following measure:
For a selected day (which the user will select), calculate the average of 4 previous rates on the same week day (in other words, look at 4 weeks, take the rate of each day where the week day is the same as the selected and calculate the average over the 4). The 4 days do not include the selected day
I first did the following, which turned out to be wrong:
Measure 1:
Success Count 4 previous weeks =
var selectedwwekDay = SELECTEDVALUE(Transactions[Weekday])
var selectedDate = SELECTEDVALUE(Transactions[Date])
return
CALCULATE(COUNTROWS(Transactions), Transactions[Result ] = "Success", Transactions[Weekday] = selectedwwekDay, Transactions[Date] < selectedDate, Transactions[Date] > selectedDate - 30)
Measure 2:
Count 4 previous weeks =
var selectedwwekDay = SELECTEDVALUE(Transactions[Weekday])
var selectedDate = SELECTEDVALUE(Transactions[Date])
return
CALCULATE(COUNTROWS(Transactions), Transactions[Weekday] = selectedwwekDay, Transactions[Date] < selectedDate, Transactions[Date] > selectedDate - 30)
Mearue 3 to calculate the average:
Last 4 weeks average =
DIVIDE( aMeasures[Success Count 4 previous weeks], [Count 4 previous weeks])
Problem with this measure is that it takes weighted average. If I had more transactions on a specific day, the average will be impacted, while I don't want it.
Example:
If user choses date 3/20/2022, the relevant data is:
My measure is basically calculating the average over all 4 weeks, regardless of the rate per day:
5 Success/ 6 Total = 83%
But I am interested in taking the average of rate over 4 days:
Average (100%, 100%, 100%, 50%) = 87.5%
How would I do it?
I think I need to create a separate table that will hold one record per day with the success rate, but not sure how to do that.
Thanks!

Year over year running month total percentage change in power bi

I've been struggling with this for a while. I need to calculate running month total percentage change year over year. I got it working but its not getting the running total for the month to do the calculation. By the way this is on the graph. Here are my calculations below:
how graph looks
ImprovementIncidentPercentage = 1 - DIVIDE([CurrYearIncidents],[LastYearIncidents],0)
CurrYearIncidents = CALCULATE(count(VW_RI_INCIDENTONLY_PBI[Incident]),
Filter(VW_RI_INCIDENTONLY_PBI,VW_RI_INCIDENTONLY_PBI[EVENTDATE].[Year] =('newsecondyear'[SecondYearValue])))
LastYearIncidents = CALCULATE(count(VW_RI_INCIDENTONLY_PBI[Incident]),
Filter(VW_RI_INCIDENTONLY_PBI,VW_RI_INCIDENTONLY_PBI[EVENTDATE].[Year] = 'newfirstyear'[FirstYearValue]))
The second and first year value is used as slicers for them to be able to pick two years they want to see the percentage improvement on:
slicers to pick what two years to do percent improvement on
I know something has to change CurrYearIncidents and LastYearIncidents so it does the calculation for running total for the month not the monthly total. I am just not sure how to make it work. Here is the close look at the issue.
show as table graph of error
For January the calculation is correct 1-(10/14) = 28.6%
For February it should be 1-(30/29) but instead it does monthly total for february and doesn't add up january so it does 1-(20/15)
To get accumulation values month over month I use this measure:
Cumulative events = TOTALYTD ( count (VW_RI_INCIDENTONLY_PBI[INCIDENT]), VW_RI_INCIDENTONLY_PBI[EVENTDATE].[Date])
Please help!

DAX: How to count how many months have sales in a period

In my fact table (fTable) the columns I have are dates, region and sales.
dates
region
sales
-----
------
-----
I am visualizing the data in a pivot table with regions as rows and months as columns (I have a date table (dDate) with a months column in my model)
I am looking for a way to dynamically change the denominator in an averaging measure if a certain region doesn't have sales in a given month. Right now my denominator is hard-coded as 6, because I am averaging 6 variables in my nominator, but any one of them could be 0 if I don't have any sales in a certain month, in which case my denominator needs to change to 5, 4 or less depending on how many months I don't have sales in. So I am looking to count how many of the past 6 months have sales and sum that as the denominator.
I have managed to count months with sales this way:
Denominator:=
var newTable = Summarize(fTable,fTable[date (month)], fTable[region],"Sales",[Sum of Sales])
var MonthsWithSales = Countrows(newTable)
RETURN
MonthsWithSales
I've tried to RETURN
Calculate(SUMX(newTable,MonthsWithSales), Dateadd(dDate[Date],-6,MONTH)
but it yields a wrong result.
Any suggestions?
Thanks
Based on my sample, we can use function VALUES & COUNTROWS inside CALCULATE to get what we need:
Measure = CALCULATE( COUNTROWS(VALUES('Table (2)'[Month])), ALL('Table (2)'[Month]) )

Calculate Number Months between 2 dates DAX

I am doing a rolling 12 month avarage over a specifik sum that are a calculated column if that matter? and my count over 12 month returns 355 day?
My sum column is [Volume]
I have a Calendar Table that are set up accordingly to a hierarchy as picture show below and more,
my code for the rolling avarage is,
12M tonnage Avarage =
CALCULATE(
SUM(STOWAGEACTUAL[VolyMton]);
DATESINPERIOD('Calendar'[DATE]; LASTDATE('Calendar'[DATE]);12;MONTH)
)
/
CALCULATE(
DISTINCTCOUNT('Calendar'[DATE]);
DATESINPERIOD('Calendar'[DATE];LASTDATE('Calendar'[DATE]);12;MONTH
))
All my values that i have in my SUM column looks like this,
And my Rolling AVG abowe calculates to this,
After some error searching i find that this piece of code,
DISTINCTCOUNT('Calendar'[DATE]);
DATESINPERIOD('Calendar'[DATE];LASTDATE('Calendar'[DATE]);12;MONTH
)
Gives me a Distinct Count value of 356. And when build my report to look on Days. My Rolling Average does compute correctly.
BUT, this is not what i want since i want this DAX to return 12 and i cant figure out why is doesnt?