DAX - Iterating present value calculation - powerbi

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.

Related

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

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

DAX - RT Measure calculation based on specific selected date in slicer

i have a problem regarding my calculation. I want to visualize the running total over time (for example 01.07 after hours rounded to the nearest half hour to reduce computing power). I have 90 days of data in my dataset. The Problem i have is that the calculation for the dates long time ago (for example 01.12.2021) are super fast and the calculation for recent dates (05.07.2022) are super slow. I think that the measure calculates the running total for the whole column and then the date filter will be applied, which caused the extreme long calculation time for recent dates. Every day has the same amount of rows. Any idea how i can tell the measure to calculate the running total only for the selected date/date which i picked in my filter?
I used the following measures:
Average Sales =
'Measure Table X'[RT Sales] / 'Measure Table X'[RT Rows]
RT Sales = CALCULATE(
Sum('X'[Sales]),
'X'[Time rounded] <= Max('X'[Time rounded]))
RT Rows = CALCULATE(
COUNTA('X'[Time rounded]),
'X'[Time rounded] <= Max('X'[Time rounded]))```
I'm not sure what you are going to achive, but you can try this, this can work faster.
Average Sales =
CALCULATE(
DIVIDE('Measure Table X'[RT Sales] , 'Measure Table X'[RT Rows])
'X'[Time rounded] <= Max('X'[Time rounded]
)
RT Sales = Sum('X'[Sales])
RT Rows =COUNTA('X'[Time rounded])

Understanding Calculate Measure with variables

I have calculated the next measure that provides the TOTALYTD in the previous year:
It is working as expected. For dates before 01/07/2006, I do not have any value in my second table. However, if I change the formula using a variable, I'm getting this result:
The calculation is performed in the same year (2006). I have been trying to understand the context (kind of new with Dax), but I have not understood this result yet.
When you store a calculation in a variable, it becomes a constant, and can't be re-calculated. That's why DATEDD in your second measure has no effect.
As a good practice, break your calculations into multiple measures. It allows you to re-use them, and makes your code more readable and maintainable. For example:
Sales Amount = SUM(InternetSales[SalesAmount])
Sales YTD = TOTALYTD( [Sales Amount], Calendario[Date])
Sales YTD LY = CALCULATE( [Sales YTD], PREVIOUSYEAR( Calendario[Date]))

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?

Define the granularity for "day" when calculating NumOfDays?

When calculating the average sales per day, I have the following measure for NumOfDays:
NumOfDays = IF (
[Sales Amount] > 0;
COUNTROWS ( Date )
)
What this is doing is removing the number of days for those dates when there are no sales.
Thus, I have the following visual:
As you can see, the total is wrong.
This is due to the fact that the database has more years than those shown in the matrix.
How can I define the granularity for "day" when calculating NumOfDays?
That is, how can I count the rows for those days with sales only?
Thanks!
I recommend two things:
Use CALCULATE() instead of IF, which is the proper DAX way of defining measures with filters
Use DISTINCTCOUNT() instead of COUNTROWS(), to ensure you never run into double counting (in case your data ever becomes more granular)
This calculation may work - but you may need to specify the actual date column inside your Date table.
NumOfDays = CALCULATE(DISTINCTCOUNT(Date[Date]),FILTER(SalesTable,[Sales Amount] > 0))
If there is more to the problem, let me know what exactly you are expecting to see in your matrix.