Rolling Average Calculation per month - Dax Measure - dax

I need to calculate ASMP which is Average Shipped per Month per Period.
Count of shipped / 12 (months in year), calculated each month or for period.
Count of records in a period on from table1, where 'ship_date' is not empty / divided by 12 (months in a year).
This is a rolling calculation. As the period length is increased, the count increases.
Example: Jan count is 5, Feb count is 6, Mar count is 2. If the calculation is executed in March, the sum is 13 / 12 = ASMP of 1.08. In April the count is 5, which increases the count. The ASMP for April is 1.5.
Any assistance on creating a calculation would be great

In PBI Desktop go to the Modeling tab and select Quick measures. Then in the Calculation dropdown select Rolling average, assign the Base value and Date fields and set Period to Months. That's all it needs!

Related

Cumulative sum with time-intelligent slicer using dax in powerbi

I'm new to Power BI, and I'm working on this measure to show cumulative sum on specific time period.
The case and what I want:
I have one Calendar table [DateKey], and main data table [Data], they have relationship based on column date.
I need to show a visual of line & clustered column chart and a time slicer, in which cumulative sum of revenue based on the slicer.
For eg: I have revenue table for July, I put out the DateKey[Month] column as slicer, when I choose July, the visual would show the cumulative sum of Revenue in July (from 1st to 31st July)
When I choose August, the visual would show cumulative sum Revenue in August (from 1st to 31st August)
What I tried: I used the following DAX
Cumulative Sum Rev = CALCULATE(
SUM(Data[Revenue]),
FILTER(
ALL(Data),
Data[Date]<=MAX(Data[Date])
)
)
Actual outcome: It did create a cumulative sum line, but it was fine for July. If I choose August in slicer, it would be cumulative sum from July to August. What I expected is the cumulative sum will begin from August, not from July.
I tried another solution, which is using ALLEXCEPT instead of ALL, but it does seems not working for date column (you can see my measures in the pbix files already have it, but to filter another column [Lead_type], which works perfectly fine for another slicer)
Cumulative Sum Rev = CALCULATE(
SUM(Data[Revenue]),
FILTER(
ALLEXCEPT(Data,Data[Lead_type]),
Data[Date]<=MAX(Data[Date])
)
)
Please help to show me where I was wrong.
Here is the link to my pbix file, it include what my visual would be and my measure:
https://1drv.ms/u/s!As4H0zrXywmbhaVFDprZ6RJmFUMbbg?e=l4Wxe5
What you are after is a "Month-to-date" measure. This is a built-in time intelligence function in DAX - you can also make your own version using the correct variables and DAX syntax - this carries more flexibility. However, for this case, this should do the trick:
First add a measure that sums the Revenue column. It is always smart to have these simple aggregating measures - when you use these measures in other measures, they are wrapped in a CALCULATE statement automatically, and this avoids some confusion down the road:
Revenue Amount := SUM ( Data[Revenue] )
Then add a TOTALMTD measure, which takes the first measure as the first argument, and your date column as a second argument. Note: the date column is in your DateKey table - do not use the date column in your fact table for this:
Cumulative sum =
TOTALMTD (
[Revenue Amount] ,
DateKey[Date]
)
You should also read up a bit on calendar tables and the difference between creating a table using ADDCOLUMNS and adding individual calculated columns on top of a calculated table. I would start here: https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

DAX Query ( Using FIlter and MAX function ), calculate Total Sales for the last running 30 days

I am new to DAX and encountered a measure as below,
30 Day Running Total = CALCULATE([Total Sales],
FILTER (ALL (Dates), Dates[Date]>(Dates[Date]) -30 && Dates[Date] <= (Dates[Date] )))
i.e. to calculate Total Sales for last 30 days in a cumulative way for the data from 1st January 2018 to 30 December 2021, the above measure i am not able to understand
My understanding is as below, please let me know where I am moving in wrong direction
FILTER ( ALL(Dates) -> Removes all filters means take date from minimum to maximum from the complete table and i.e. between 1st January 2018 till 30-december-2021
Dates[Date]>MAX(Dates[Date]) -30 -> "Takes Total Sales from the current row in table minus 30 days".
For example if the DAX calculation is on 30th January 2018 then it considers all the total sales from 1st January 2018 till 30th January 2018
Then why do we need to mention another filter Dates[Date] <= MAX(Dates[Date] )?
Thanks in advance for your time
Regards
Sumit Malik
Sumit your main concern seem to be Point (3)
why do we need to mention another filter Dates[Date] <= MAX(Dates[Date] )?
Your doubt is correct, if the data is clean, you do not need to define that upper-bound filter because theoretically considering sales from 30 days ago, there should not be sales after today.
Unfortunately, often data is dirty and there might be Sales in the future. Therefore, defining an upper-bound is a best-practice to avoid this kind of dirty data issues. Remember that in software engineering you program thinking the worst-case scenario, therefore, defining an upper bound does not harm :)

How to calculate month and any additional days between two dates using google sheet formula?

I want to calculate month between two date and output how month and any additional days by using google sheet
I use this formula to calculate month but i don't know how to calculate any additional days after a complete month.
DATEDIF(AG2,TODAY(),"M")
Quoting this reference of DATEDIF (emphasis mine): https://sheetshelp.com/datedif/
Syntax
=DATEDIF(start_date,end_date,unit)
start_date Date at which to start the calculation
end_date Date at which to end the calculation
unit Type of output. Choices are “Y”, “M”, “D”, “YM”, “YD”, or “MD”.
...
"M" – Number of whole months elapsed between start and end dates
"MD" – Number of days elapsed after the number of months shown with the “M” or “YM” unit. Can’t go higher than 30.
...

Finding closest time before/after another time

I have a database of time's that are not necessarily exact to the specified time. For example, if I want to pick 12:00 PM and my columns have times in column A:
[9:00 AM, 11:55 AM, 2:00 PM, 6:00 PM],
The closest before would be 11:55 AM, and closest after would be 2:00 PM. My attempt at the code:
=MAXIF(A:A, A:A, 12:00)
=MINIF(A:A, A:A, 12:00)
But to no avail, anythoughts?
You want the max where the time is less than or equal to the criteria and the Min for the time greater than or equal to the criteria:
=MAXIFS(A:A, A:A,"<=12:00")
=MINIFS(A:A, A:A,">=12:00")

Explain Apache SOLR boost function

I'm try to implement a logic in APACHE SOLR so that documents older than 2 years should get penalty based on the difference in number of days or months.
I am using this boost function, which I got after googling a lot.
recip(ms(NOW,publicationDate),3.16e-11,1,1) // Currently it is set to use 1 year
Can any please confirm if this penalties old documents or what ?
Thanks
A reciprocal function with recip(x,m,a,b) implementing a/(m*x+b).
m,a,b are constants, x is any numeric field or arbitrarily complex
function.
In case of your parameters, your function will look like this:
f(x) = 1 /(3.16e-11*x + 1)
Function ms returns milliseconds of difference between it's
arguments.
Dates are relative to the Unix or POSIX time epoch, midnight, January
1, 1970 UTC.
Imagine, your publication date is September 1st 2015, ms will get us NOW = 1507725936061 and publication date is 1441065600000 and the whole result will be around 0.3 which will be the score for this document.
For publication date of yesterday, we will get score of 0.99, which leads to the idea, so, this formula will apply penalty to every document not only to ones which are 2 years old. For example, for the same day 1 year ago the score will be 0.5
I could think potentially about sorting by this function (starting from Solr 6)
if(gt(ms(mydatefield,NOW-2YEARS),0),1,recip(ms(NOW,publicationDate),3.16e-11,1,1))
I didn't test it (not sure about NOW-2YEARS part), but basically, i'm doing this:
if mydatefield - NOW-2YEARS greater
than 0 => score will be 1.0
else => I'm calculating reciprocal function
One last remark: there are 3.16e10 milliseconds in a year, so one can scale dates to fractions of a year with the inverse, or 3.16e-11, so for 2 years, you may select something different.

Resources