Calculate Number Months between 2 dates DAX - powerbi

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?

Related

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])

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 monthly Avg of daily percentage values

We have a report in Compliance% which is calculated every day. I have a requirement to calculate the Monthly Average of these daily Compliance% values.
PowerBI currently gives incorrect total value in a table format, I have attached the sample file. It is giving 56.90 in the grand total whereas I want to calculate sum(Compliance%)/count(day or month).
Could someone please help.
[
I think you need to introduce a new measure:
mDay =
IF(
HASONEFILTER(Table1[Compliance %]),
SUM(Table1[Day]),
FORMAT(
SUMX(Table1, Table1[Compliance %]) / COUNT(Table1[Day]),
"0.00%")
)
Here's the result for the month of February:
Here's another one if you also include a few values from March:
As you can see the measure responds to your table filters.
I hope it helps.

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.