Use DAX to calculate sales for last full month in period - powerbi

My filter context contains all dates in a financial year (eg. 1-Jul-2017 - 30-Jun-2018). I would like to calculate sales for the most recent full month in the selected financial year.
Eg. For the current financial year 1-Jul-2017 - 30-Jun-2018, I would like to calculate total sales for August, since it is the most recent full month in the period as of today (15-Sep-2016). I have tried the following:
Prem $ Last Closed Month = CALCULATE(SUMX(SalesFlat, [gross_amt_plus_lhc_annual]), DIM_DATE[MONTH_END_DATE] < NOW()) - CALCULATE(SUMX(SalesFlat, [gross_amt_plus_lhc_annual]), DATEADD(DIM_DATE[MONTH_END_DATE], 1, MONTH) < NOW())
But am getting the error "A function 'DATEADD' has been used in a True/False expression that is used as a table filter expression. This is not allowed."
Any ideas?
Thanks.

Try using PREVIOUSMONTH(<dates>) function
Prem $ Last Closed Month = SUMX(PREVIOUSMONTH(<dates>),calculate(sum(TotalSales))
Replace TotalSales with your measure for sales.

Maybe this will help. If I start with this table, named "SalesFlat"...
...and I add a column using this code...
MostRecentFullMonth = SUMX(FILTER(SalesFlat,EOMONTH(SalesFlat[MONTH_END_DATE],0)=EOMONTH(NOW(),-1)),SalesFlat[gross_amt_plus_lhc_annual])
...I get this result:
Since today is 16 September 2017, the most recent full month was August 2017. The total gross_amt_plus_lhc_annual for August 2017 was 43 + 66 + 98 + 58 + 9 = 274.
You could also use this to set up a measure, instead of a column, and you can use it in your PowerBI dashboard like with this card:

Related

How to get week number per month on Power Bi?

I have a a column called Date originated that hold the a date like "Tuesday, March 21, 2022".
I want to get that week number per month but the following gives me incorrect numbers.
The following DAX: Week of Month = 1 + WEEKNUM('ECR'[Date Originated]) - WEEKNUM(STARTOFMONTH('ECR'[Date Originated]))
gives me 1 for that date.
Some entries are correct like for Wednesday, April 6, 2022 I do get 1
The problem is not the DAX expression, but the date column, which you didn't share with us.
STARTOFMONTH is not the right choice of function here, since that function does not by default return the first date of a given month; rather, it returns the first date of the month for the supplied dates (within the current context).
Here you are supplying only one date, and so, for example:
=STARTOFMONTH(21/03/2022)
will simply return 21/03/2022.
You should be using
=
1 + WEEKNUM( 'ECR'[Date Originated] )
- WEEKNUM( EOMONTH( 'ECR'[Date Originated], -1 ) + 1 )

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.

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

Power BI : Monthly averages

I know similar questions have been asked and answered previous to this, but for the life of me, cannot get any of those working with my limited knowledge on Power BI.
I have a table which contains 8 weeks worth of data which for most parts, spans 3 months. I currently calculate the daily average which is the "total" divided by the "total in". However what I need to do, is to display the average based upon the calendar month. Therefore, September will have a difference average to that of October and that of November.
Here's a sample of the data:
Date,Total In,Total Out,Daily Average,Total,Month
27 September 2017,10773,264,97.61,11037,September
28 September 2017,11198,382,96.70,11580,September
29 September 2017,17753,1122,94.06,18875,September
30 September 2017,9568,649,93.65,10217,September
28 October 2017,11434,938,92.42,12372,October
29 October 2017,1541,60,96.25,1601,October
30 October 2017,918,4,99.57,922,October
31 October 2017,8565,24,99.72,8589,October
01 November 2017,11452,635,94.75,12087,November
02 November 2017,7785,531,93.61,8316,November
So for November I would like to have the figure as (11452 + 7785) / (12087 + 8316) * 100 = 94.29%. Obviously, that figure would be present for all dates in November as an extra column. For October it would be (11434 + 1541 + 918 + 8565) / (12372 + 1601 + 922 + 8589) * 100 = 95.63%. I would then use the extra column and plot it on a line/bar chart.
So the above data would become:
Date,Total In,Total Out,Daily Average,Total,Month,Monthly Average
27 September 2017,10773,264,97.61,11037,September,96.00
28 September 2017,11198,382,96.70,11580,September,96.00
29 September 2017,17753,1122,94.06,18875,September,96.00
30 September 2017,9568,649,93.65,10217,September,96.00
28 October 2017,11434,938,92.42,12372,October,95.63
29 October 2017,1541,60,96.25,1601,October,95.63
30 October 2017,918,4,99.57,922,October,95.63
31 October 2017,8565,24,99.72,8589,October,95.63
01 November 2017,11452,635,94.75,12087,November,94.29
02 November 2017,7785,531,93.61,8316,November,94.29
I am having trouble getting my head around the SUMMARIZE functions etc in order to get this working without any help. So any help and explanation would be greatly appreciated.
Thanks.
Create a DAX using the EARLIER function should do the trick:
Month Average =
VAR sum_total_in =
CALCULATE(
SUM('table'[Total In]),
FILTER(
'table',
'table'[Month] = EARLIER('table'[Month])
)
)
VAR sum_total =
CALCULATE(
SUM('table'[Total]),
FILTER(
'table',
'table'[Month] = EARLIER('table'[Month])
)
)
RETURN sum_total_in / sum_total
So basically it sums the Total In and Total within the same month and returns the division.
Results:
P.S. You'll need to add the year to the filter as well if you have data across years with the same month.
I see you're asking your question from a DAX perspective, but here's a Power Query based answer that's quite easy. Starting with your table in Power Query (Power BI's Query Editor):
Select the Month column, then Transform -> Group By, and set up the dialog box like this...
and click OK.
You'll see this:
Then click the button at the top of the
AllData column to expand the nested tables, and set up the dialog box like this...
and click OK.
You'll see this:
Now click Add Column -> Custom Column, and set up the dialog box like this...
and click OK.
You'll see this:
Now select the first three columns (Month, Sum Total In, Sum Total) and click Home -> Remove Columns.
You'll see this:
You can double-click the Month.1 column title and rename it if you like.