How to calculate the cumulative total of an average (calculated column) - powerbi

I'm having a hard time to calculate the running / cumulative total of average values...
My idea is to emulate a sales campaign for a product that never had one, using the average by day of all the products from an specific brand; problem is the code I made is only repeating the numbers from the average, it's not summing them up:
This is how it's now:
This is how it should be:
I managed to filter the values I want to calculate the average for, and that's for an specific brand (this is not going to be replicated to all products, and in the end I want to merge this in another calculated column which will calculate the goal for all the products (including the exception, which are the ones that never had a sales campaign before).
here's the code for the average:
AllPlutoProductsAverage = MAXX(FILTER('f_Leads(teste2)', [percentage] = EARLIER(d_CT_Leads_RT[percentage]) && 'f_Leads(teste2)'[group_name]="Pluto"), 'f_Leads(teste2)'[registers_per_day] )
And here's the code for the cumulative/running total column:
VAR _NoCampaign_RT =
CALCULATE(
MAX( 'd_CT_Leads_RT'[AllPlutoProductsAverage ] ) ,
FILTER( 'f_Leads(teste2)' ,
AND( 'f_Leads(teste2)'[group_name] = d_CT_Leads_RT[group_name] ,
'f_Leads(teste2)'[course] = d_CT_Leads_RT[course]
) &&'f_Leads(teste2)'[course_closed_percentage] = d_CT_Leads_RT[percentage]
)
)
Any ideas on how I fix that...?
Thanks in advance!! :))
I tried:
quick measure for running totals, didn't work
custom code for running totals, still repeated the values
creating a separate measure for average and then mentioned it on the column's running total code, same result
tried building up a running total code by adding the average calculation into it as a variable, didn't work either
tried exporting the calculation to a measure, didn't work either
And by 'didn't work' I mean the column No_PreviousCampaign still shows the repeated values from AllPlutoProductsAverage

Related

How to have the table total the sum of my measure which has an IF condition

I'm creating a table that automatically calculates the bonus for each salesperson based off their sales figures. I'm struggling with the measure
Whale Accounts =
IF('Oct-Dec Refresh 1.5.23'[YearOverYear Variance] >
800000 && 'Oct-Dec Refresh 1.5.23'[Percentage Difference] >= 1.1, 2500, 0)
Any account that sold over $800,000 and had a sales growth increase of 10%, receives $2500 in bonuses. I created the measure and it shows $2,500 for each account that meets this criteria, but the table doesn't sum it, how can I get the table to sum the total?
Also, both YearOverYear Variance and Percentage Difference are measures. I have attached an image for clarification
I tried creating custom columns, using SUM, CALCULATE but I haven't been able to figure it out.
You need a summing iterator over your accounts for this. Something along the lines of:
Bonus =
SUMX (
VALUES ( 'Table'[Master Bill To] ) ,
[Whale Accounts]
)
Where the account column is the one you are using in the visualization!

Why I get different Total, but same values?

I needed to replace this measure:
CALCULATE([GM % YTD], SAMEPERIODLASTYEAR('Date'[Date]))
By this one:
VAR VAR1 = ADDCOLUMNS( VALUES(Revenue[Key_Client]),
"Col1", CALCULATE([GM % YTD], SAMEPERIODLASTYEAR('Date'[Date]),
REMOVEFILTERS(Revenue[Type],Revenue[SectorType]))
)
RETURN AVERAGEX(VAR1, [Col1])
Both measures point to GM % YTD, which is:
CALCULATE([GM %], DATESYTD('Date'[Date],"31/05"))
I get this, when I display them side by side:
The values are ok, my problem is with the Total. I am unable to find how/where is the aggregation on the left column done... How is that 73,2% achieved? It doesn't seem to be average...
Also… how can I force the measure on the right to do the same aggregation?
In the ADDCOLUMNS version, you are iterating over each Revenue[Key_Client] and only averaging after the [GM % YTD] has been calculated for each one separately. For a single client, there's only one thing to average, so the value isn't affected by that step.
Generally, you want to compute the measure over all clients together rather than averaging the individual numbers together to get a standard weighted average rather than an average where all clients are weighted equally.

Calculate daily average including zero in Power BI

I have a dataset of patients visiting several categories(SPECIALISM) of a hospital. A visit lasts a couple of hours each day. Each row in my dataset represents an hour that they are present for a certain hospital specialism.
Input
I want to calculate for each hour of the day, the number of patients that are present on average, per specialism, I used the following code (measure):
daggem = AVERAGEX(values('Date'[Date]),[distinctpat])
with distinctpat being a distinct count of patient IDs
This gives me almost the desired result, but the tales of the graph are too heavy (it shows an average of 1 patient during the night, but this 1 patient was there only on 1 specific day, normally it is zero. But the average, as I calculated, it does not include all other nights when there were zero patients. So I would like to obtain an average that is much lower (and more correct)
Output
You probably need an extra table with all the days and times. Based on the reports, you will be able to find the hours without visits
Here is an example of how to create a Date table with hours; Add relationship and use in calculation.
DatesWithHours =
var Dates = CALENDAR("2021-01-01 00:00:00", "2021-01-03 00:00:00")
var DatesHour =
GENERATE(Dates, (ADDCOLUMNS(GENERATESERIES(1,12,1),"Hours", [Date] + TIME([Value],0,0))))
return
DatesHour
That's the problem with AVERAGEX, it ignores blanks. Try a simple division instead.
daggem =
DIVIDE (
COUNTROWS ( TargetTable ),
COUNTROWS ( Dates )
)

Is there a POWER BI or DAX code that can make a cumulative or running total of a column made by measure?

I am currently trying to make a running total for a cumulative plot of some temprature data in Power BI Desktop, but cannot get it to work.
I can see from similar posts on forums that im not the only one. My problem is that I have temperature data from a large table (20-30 degrees) and then made a measure that count the instances of each specific temperature and returns the percentage of the total instances. I want to make a running total which enables me to plot the cumulative distribution.
I have tried ranking the tempertures, and ranking by dates does not make sense. I feel like it is a pretty simple problem but i have not spend 3 days without luck.
I have added some screenshots of my data and progress so far and the code i use to calculate the percentage of the cumulative total, which is the one i want made into a running total.
Hope some of you can help me with some guidance or examples of how to tackle this issue.
Sincerly Lasse
Code
Canvas so far
Data to sum
Fault when ranking
Example of a ranking
Here you can find a good example of running Total
https://www.daxpatterns.com/cumulative-total-excel-2013/
Cumulative Quantity :=
CALCULATE (
SUM ( Transactions[Quantity] ),
FILTER (
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX ( 'Date'[Date] )
)
)
You can also use this pattern to calculate cumulative if you don't have Date column:
[CumulatedSales] =
CALCULATE (
SUM ( Products[ProductSales] ),
ALL ( Products ),
Products[ProductSales] >= EARLIER ( Products[ProductSales] )
)

DAX - Iterating present value calculation

I have a NPV calculation which I've done manually because the XNPV function doesn't seem to work.
The NPV measure I've created looks like this:
'Data'[Undiscounted Value] / (1+'Discount Rate'[Discount Rate Value])^([Component]/12)
I've discovered the error with my calculation lies in the [Component] portion of the measure. The [Component] calculation is as follows:
IF(TODAY() > LASTDATE('Data'[Date]), BLANK(),
DATEDIFF(TODAY(), LASTDATE('Data'[Date]), MONTH))
The [Component] calculation is intended to determine the number of months for the NPV calculation. I'm dividing by 12 because I have an annual discount rate, but my data is in months.
The above calculation works perfectly in a line/bar chart. I have spot tested the months to confirm that the values are correctly discounted.
The PROBLEM occurs when I attempt to AGGREGATE the data. In other words, it works fine in a line chart, but when I just put the calculation on a card visual it uses the LASTDATE for the component and performs the calculation using the sum of all undiscounted values. In other words, it does something like this:
SUM ('Data'[Undiscounted Value] ) / (1+'Discount Rate'[Discount Rate Value])^(MAX(Component) /12)
and then spits out a result which is incorrect.
What it is supposed to be doing is simply taking the sum of all the discounted values. So running the calculation month by month and then adding all the months, but instead it's adding the Undiscounted values and then discounting them over 36 months.
My QUESTion is: is there an alternative function that would tell PowerBI not to use the LASTDATE, but instead iterate over each month (row) and then SUM
I would suggest the following solution structure:
SUMX(
VALUES( 'Data'[Month] )
, CALCULATE( NPV )
)
This will calculate the discount for each month before summing them up.
You may have to adjust your [Component] formula as well.