I have the following fields:
Year
Category
Maker
Month
Month Number
Sales Volume
Sales
Date
So, I have in my dash a filter for "Month Number" and "Year":
My goal is to create two new measure; first with the Rolling Year that need to sum 12 months, ending in the moment that the user select in the mencioned filters. For example if y select Year 2021 and Month 01. The Rolling Year need to sum the sales of a selected category since 2020-02 to 2021-01 (thats mean always 12 months since a pivot month).
For thesecond is exactly the same, a measure called Rolling Last Year, it need to be a rolling sum too, but for the last period in order to compare. Taking the same example if I have the period 2020-02 to 2021-01. The Rolling Last Year for the last period should be 2019-02 to 2020-01.
I tried with this DAX code, that extracted from Microsoft page but without success:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL (Table[Date] ),
AND (
Table[Date] <= MAX ( Table[Date] ),
DATEADD ( Table[Date], 12, MONTH ) > MAX ( Table[Date] ))))
I share you an extract of my base:
Thanks in advance !!!
Based on the table and code you have shared, it is unclear from where the date filters are being applied. In case you have not done it, I strongly suggest to delete the [Month] and [Month Number] field from your Sales table and keep them in a separate Calendar table, from where the filters should be selected. Then, a simple tweak on you current formula should do the trick:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL ('Calendar'[Date] ),
AND (
Table[Date] <= MAX ( 'Calendar'[Date] ),
DATEADD ( 'Calendar'[Date], 12, MONTH ) > MAX ( 'Calendar'[Date] ))))
However you can try with this variation for the code, a little bit optimized so as not to scan your whole Sales table each time:
Rolling Year =
VAR EndSelectedDate = MAX ( 'Calendar'[Date] )
VAR StartSelectedDate =
CALCULATE (
MAX ( 'Calendar'[Date] ),
ALL ( 'Calendar'[Year] ),
'Calendar'[Year]
= MAX ( 'Calendar'[Year] ) - 1
)
RETURN
CALCULATE (
SUM ( Table[Sales] ),
ALL ( 'Calendar' ),
'Calendar'[Date] <= EndSelectedDate,
'Calendar'[Date] > StartSelectedDate
)
Related
With reference to the below sample data, I have a table in Power BI that currently only contains the Bus_Day column. I am attempting to add the MTD column (in red) that counts the monthly cumulative number of Bus_Day. The rows highlighted in yellow are reflective of the requirement that the MTD Day column should reset to 1 for the first Bus_Day per month.
My challenge is the Bus_Day column does not contain contiguous dates, so using DATEDIFF is not an option.
For this type of problem where you want to label non-contiguous dates, RANKX is your go-to unless you can solve it further upstream in your stack.
Here is a DAX column solution that adds a calculated column according to your specifications:
MTD Day =
VAR _yr = YEAR ( [Bus_Day] )
VAR _mth = MONTH ( 'Table'[Bus_Day] )
RETURN
RANKX (
FILTER (
'Table' ,
YEAR ( [Bus_Day] ) = _yr
&& MONTH ( [Bus_Day] ) = _mth
),
[Bus_Day] , , ASC
)
You can also calculate this using a measure, this formula does the trick with your sample data:
MTD Day Measure =
VAR _day = SELECTEDVALUE ( 'Table'[Bus_Day] )
VAR _tbl =
FILTER (
ALL ( 'Table' ) ,
YEAR ( 'Table'[Bus_Day] ) = YEAR ( _day )
&& MONTH ( 'Table'[Bus_Day] ) = MONTH ( _day )
)
RETURN
RANKX (
_tbl,
CALCULATE ( SELECTEDVALUE ( 'Table'[Bus_Day] ) ) , , ASC
)
Result:
I have the following dataset
user_id, login_date
111, 01/02/2021
222, 02/15/2021
444, 02/20/2021
555, 01/15/2021
222, 03/10/2021
444, 03/11/2021
I want to count of the number of unique active user_id in the last 90 days based on the max date of my date slicer. I'd like to solve this without using filters. This also needs to be dynamic as max date can be change from the date slicer.
From what I have understand so far I will need to evaluate for each row if the date difference between the current date of the row and the max date of the slicer is less than 90 days. Then for all the rows where the date diff is less than 90 days I will want to count the distinct number of users.
so basically I will have three layer in my final formula
evaluate the date diff.
filter out rows where the date diff is superior to 90 days.
count the distinct users in the remaining rows.
I've tried many approach and formula. I think that this one is closed to something that could work:
Measure test = CALCULATE(SUMX(DISTINCT(mytable[user_id]),filter(mytable,DATEDIFF(SELECTEDVALUE(mytable[login_date]),[Max range date],DAY)>90)))
this formula return me the following error :
The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
I've also tried applying a if statement to output the date diff as 0 and 1 and hopefully being able to sum this for each unique id with something like this
SUMX( VALUES(my_table[user_id]), IF(DATEDIFF(SELECTEDVALUE(mytable[login_date]),[Max range date],DAY)>90,0,1)
anyway I'm kind of stuck. hopefully my question is clear enough.
You will have to create a disconnected table from which you will use the date column in the slicer, I have prepared a power BI file which included 2 very common scenarios, I hope that helps you.
File - Simon.pbix
Screenshot of the report - https://ibb.co/xjrjVv5
Screenshot of the model - https://ibb.co/Ws1z8D7
DAX Code -
for simon =
IF (
ISINSCOPE ( simon[Login Date] )
|| ISINSCOPE ( simon[User ID] ),
VAR LastVisibleDate =
CALCULATE (
MAX ( 'Simon Date Table'[Login Date] ),
ALLSELECTED ( 'Simon Date Table' )
)
VAR CurrentDate =
MAX ( simon[Login Date] )
VAR TimeJump = 90
VAR Result =
CALCULATE (
DISTINCTCOUNT ( simon[User ID] ),
simon[Login Date] <= LastVisibleDate,
simon[Login Date] > LastVisibleDate - TimeJump,
ALLSELECTED ( simon )
)
RETURN
Result
)
second version:
for simon 2 =
IF (
ISINSCOPE ( simon[Login Date] )
|| ISINSCOPE ( simon[User ID] ),
VAR LastVisibleDate =
CALCULATE (
MAX ( 'Simon Date Table'[Login Date] ),
ALLSELECTED ( 'Simon Date Table' )
)
VAR CurrentDate =
MAX ( simon[Login Date] )
VAR TimeJump = 90
VAR Result =
IF (
CurrentDate <= LastVisibleDate,
CALCULATE (
DISTINCTCOUNT ( simon[User ID] ),
simon[Login Date] <= CurrentDate,
simon[Login Date] > CurrentDate - TimeJump,
ALLSELECTED ( simon )
)
)
RETURN
Result
)
I'm new to DAX. In order to get the previous month's sales, I have used DATESBETWEEN and passed MAX(Date)-1 as the start date and as the end date EOMONTH.
I got the correct results. However, what are the other simpler ways of getting the same?
CALCULATE (
SUM ( AW_Sales[Sales Amount] ),
DATESBETWEEN (
AW_Calender_Lookup[Date],
DATE ( YEAR ( MAX ( AW_Calender_Lookup[Date] ) ), MONTH ( MAX ( AW_Calender_Lookup[Date] ) ) - 1, 01 ),
EOMONTH (
DATE ( YEAR ( MAX ( AW_Calender_Lookup[Date] ) ), MONTH ( MAX ( AW_Calender_Lookup[Date] ) ) - 1, 01 ),
0
)
)
)
update - based on an answer given I added a new measure, but the value is empty.
If you have a Dates table which is basically a calendar table, there is a simple way. Your data/fact table has to be connected to the Dates table using the date column for say date. Now you can create your Date slicer from the Dates table. For say, you selected the month September-2020 from the slicer, this below measure will return the SUM for current month-
total_sales = SUM(table_name[sales])
And this below measure will return the total sales for previous month-
total_sales_previous_month =
CALCULATE(
[total_sales],
PREVIOUSMONTH('Dates'[date])
)
For more information on DAX function PREVIOUSMONTH, you can check this.
I'm trying to get cumulative number of unique IDs in given timeframe.
My DAX look like this:
Cumulative = CALCULATE(SUM(Data[ID]));DATESBETWEEN(Data[ack_date];DATE(YEAR(NOW());4;1);DATE(YEAR(NOW());11;30)))
There is similar measure for Year-1: [YEAR(NOW())-1]
What I want to achieve is area chart showing growing number od IDs in time comparing same periods this and previous year. When I give those measures as Values for chart and "ack_date" as its Axis what I get is values comparison month by month but not cumulative, just value for certain month.
Try this code. Adjust for year -1.
=
CALCULATE (
DISTINCTCOUNT ( Data[ID] ),
FILTER (
ALL ( Data ),
AND (
Data[Ack_date] <= MAX ( Data[Ack_date] ),
AND (
Data[Ack_date] <= DATE ( YEAR ( NOW () ), 11, 30 ),
Data[Ack_date] >= DATE ( YEAR ( NOW () ), 4, 1 )
)
)
)
)
After going through several posts on StackOverflow and the PowerBI forums, I still can't figure out how to calculate a rolling average based on a given period- in my case, a 30-day rolling average.
Most of the posts I've seen advocate something either identical or really similar to this:
Rolling Sum :=
CALCULATE (
[Sales],
FILTER (
ALL ( Sales ),
[Date]
>= MAX ( Sales[Date] ) - 365
&& [Date] <= MAX ( Sales[Date] )
)
)
(code taken from this post)
...and yet, I can't seem to get the proper values.
In my case, I have the following:
"closing date" for a given loan (column)
loan count (measure)
closing length (column)- length of time (in days) to close a loan
What I'd like to calculate is the rolling 30 day average for any given day. I coded the following:
Rolling Average =
CALCULATE (
SUM(Query1[Closing_Length])/[Loan Count],
FILTER (
ALL ( Query1 ),
[Closing Date].[Date]
>= MAX ( Query1[Closing Date] ) - 30
&& [Closing Date] <= MAX ( Query1[Closing Date] )
)
)
To check the results, I used a visual filter to examine one month's worth of data and these are the results:
Note the totals row at the bottom; for this given period, there are 102 loans and it took an aggregate of 3922 days for them to close. The average I'd like to calculate is 3922/102, which should equal approximately 38.45 days. Instead, we see 42.
How can I fix this?
Measure based solution:
Rolling Average Measure =
VAR A =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Closing Length], 2 )
)
VAR B =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Loan Count], 2 )
)
RETURN
A / B
Calculated column based solution:
Rolling Average =
VAR CurrentDate = 'Query'[Closing Date]
VAR T =
FILTER ( 'Query', [Closing Date] <= CurrentDate )
RETURN
ROUND ( SUMX ( T, 'Query'[Closing Length] ) / SUMX ( T, [Loan Count] ), 2 )
Print Screen: