Sum up revenue for the last twelve months up until the first day of current month - yearmonth

I would like to extract a sum of revenue.
The sum should contain revenue for the twelve months passed up until the beginning of current month.
Following that, the sum of revenue will change only when another full month has passed.
I have browsed around a number of suggestions but they don't give me the required data. The last one gives me all revenue up until June 1, 2021 (i.e. twelve months prior to this month). And by all revenue, I mean ALL REVENUE generated prior to that date. Not just 12 months worth, but years...
SELECT DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 12, 0) AS Period, SUM(SalesAmt) AS Sales, SalesRep, CurrencyTypeID
FROM dbo.SalesData
GROUP BY SalesRep, CurrencyTypeID, SalesID

Related

DATEADD to calculate PYTD (Jan 1 till today's date) in DAX Power BI

I have a daily sales data for 3-4 years. I want to create the YTD and Prior Year To Date sales measure that will be updated daily. That is it should always be from beginning of the year (selected) to TODAY or the last day of the data (1 day lag from today and max date).
I used Sameperiodlastyear but it is problematic at the beginning of the month as it compares say Jan 1, 2022-June 8 2022 with Jan 1, 2021 with June 30, 2021.
Any suggestion how I can create a modified prior year to date measure to address this nuance?
This is a standard solution for the case. First, you get all dates, with a DATESYTD() function, for the current year or last visible year up today or last visible day, then you offset it.
SAMEPERIODLASTYEAR(DATESYTD(‘Date’[Date]))
It is equal to
DATEADD(DATESYTD(‘Date’[Date]),-1,YEAR))
Try this if you want to get exact days set:
VAR FirstDayThisYear =
SAMEPERIODLASTYEAR(STARTOFYEAR(‘Date’[Date])
VAR LastDayThisYear =
SAMEPERIODLASTYEAR(
LASTDATE(‘Date’[Date])
)
VAR SetOfDates=
DATESBETWEEN(
‘Date’[Date]
,FirstDayThisYear
,LastDayThisYear
)
RETURN
SetOfDates

Power Bi DAX: Total for the month

I need to make a total for each of the months based on the working days.
So far i have working days set as 1 if the day in the month is during Monday - Friday and 0 for Saturday and Sunday. I now need to total up the 1's and make it so that it is a single value for the month.
E.g Going the weekdays is 1 and weekend is 0, January will have 22 days on each row on the table in the data mode - January 1 = 232 January 2 = 22 etc. so i can use it to divide against my target which is set to the 1st of every month.
How can i do this??
try this. i haven't tested it.
=GROUPBY( FILTER(table, table[Working_Day]=1) , table[Month], ["new col", SUMX( CURRENTGROUP(), table[Month_Order])])
filter gives working day 1's data as table,then group by performed by month, then month order has been added and returns table.

DAX Looping Variable

I'm building a Power BI dashboard that includes actual cost vs budget. Summarizing the cost is not a problem. The budget amount is entered in a table as a weekly amount, for example $1,000. We use a custom accounting calendar that dictates the number of weeks per month. The second month of each quarter is 5 weeks while all other months are 4 weeks. Here's what it looks like:
January is 4 weeks
February is 5 weeks
March is 4 weeks
April is 4 weeks
May is 5 weeks
June is 4 weeks
July is 4 weeks
August is 5 weeks
September is 4 weeks
October is 4 weeks
November is 5 weeks
December is 4 weeks
I need a looping variable in DAX that will simply count the number of weeks based on the months chosen with the month slicer. Then I can multiply the number of weeks by the weekly budget amount.
Here's a practical example:
If I choose Jan, Feb, Mar, Apr then the number of weeks should be 17 weeks. And the budget amount should be $17,000.
I figured this out in Crystal Reports. Here's the formula I wrote:
local datetimevar startdate:= {?BegMonth};
local numbervar numofWeeks;
While startdate <= {?EndMonth}
Do(
if month(startdate) in [2,5,8,11] then
numofWeeks := numofWeeks + 5
else
numofWeeks := numofWeeks + 4;
startdate := DATEADD("m",1,startdate);
);
numofWeeks
I've tried several approaches with DAX but I got nothing to work.
DAX doesn't exactly do looping. It does aggregations on filtered columns.
If you don't have a Month column, then create a calculated column.
TableName[Month] = MONTH ( TableName[Date] )
Once you have that, adding up weeks can be done with an iterator function.
Weeks =
SUMX (
VALUES ( TableName[Month] ),
IF ( TableName[Month] IN { 2, 5, 8, 11 }, 5, 4 )
)
The VALUES function is a list of unique values in the filter context, so if you selected Jan - Apr, then it would be {1,2,3,4}. Then SUMX iterates ("loops") over those four values and add 5 or 4 depending on the condition specified in the IF function.

Grand Total not working for sales per day last year function

I am trying to do a sales by day comparison where I compare the sales this year with the sales of the same day last year as a date of the week.
So I would be comparing Monday March 25 2019 with Monday march 24 2018 etc
Here is the formula I’m using for last year’s sale
Amount per Day LY = CALCULATE([Amount TY], FILTER(all(Dates), Dates[Date] = MAX(Dates[Date])-364))
However, my total isn’t working right for my sales last year. It will just be the total for 1 day (and that day seems to change as my date range increases)
Because you are having a one to one, you can do this with LookupValue:
SalesLY = LOOKUPVALUE(Sheet1[Sales];Sheet1[Date];DATEADD(Sheet1[Date];-364;DAY))
However if you have more rows for the same date, this will not hold, in this case you need to sum all the dates together,
SalesLY = CALCULATE(SUM(Sheet1[Sales]);FILTER(Sheet1; DATEADD(Sheet1[Date];364;DAY)= EARLIER(Sheet1[Date])))
Using DateAdd fixed my problem
Amt per Day LY = CALCULATE([Amount TY], DATEADD(Dates[Date], -364,DAY))

Compare totals for the same partial date range year-over-year in DAX / Power BI

I'm trying to create a table which shows a sum of monthly values for one year compared to the last year's totals (structured as the screenshot below):
Monthly Comparison
However, the caveat I'm dealing with is comparing the most current month, which will always contain partial month data (unless it's the last day of the month), to the same date range of the previous year. In the screenshot I attached, our data for January 2018 only goes through January 22nd. However, it's comparing it to the full month of January from 2017, whereas we want that total to be January 1st - 22nd, 2017: Value That Needs to be Updated.
I've tried messing around with various MTD and cumulative totals, but I can't seem to get the logic to work while keeping the aggregation to the monthly level. Any idea what type of logic needs to used in order to compare year-over-year totals, but only do a partial sum for the same date range of a month that is currently in progress?
Thanks in advance.
In my short example, this seems to work:
Total Sales LY 2 =
VAR MaxDate = EDATE(CALCULATE(MAX(Sales[Date]);ALL(Sales));-12)
RETURN
CALCULATE(
[Total Sales];
FILTER(SAMEPERIODLASTYEAR('Date'[Date]);'Date'[Date]<=MaxDate)
)
I calculate Total Sales for the same period last year, with the max of the last available sales date this year.
Total Sales LY = Comparing last year full month (wrong)
Total Sales LY 2 = Comparing last year month, with max of last sales date
PBIX file