I have created a '3-days moving average'-measure in power BI.
It all works fine as long as I have a DateColumn in my FactTable and use that as the axis.
Is there a way to re-write my code in DATESINPERIOD so that the measure gets the date from my DateDIM-table via DateID??
maybe with Lookupvalue or Related?
FactTable
CALCULATE(
SUMX(FactTable;[Average Sales per day])/3;
DATESINPERIOD(
FactTable[Date];
LASTDATE(FactTable[Date]);
-3;
DAY
)
)
Something like this should work.
You will need to rename fields and tables to match the one in your model.
CALCULATE(
SUM('Sales SalesOrderHeader'[SubTotal]),
FILTER (
ALL ( 'Calendar'[Date] ),
'Calendar'[Date] <= MAX ('Calendar'[Date])
&& 'Calendar'[Date] > ( MAX('Calendar'[Date])-3)
)
)
Related
We're a trucking company. I have a measure that gives me a total number of drivers within a date range context. I really need an average. My thought was to use AVERAGEX to create the average utilizing the count measure I created.
I have not had much luck. There is enough internet resources to say it should be possible, but I can't seem to put the components together.
This is my Driver Count measure.
Driver Count (Company) =
CALCULATE (
COUNTROWS ( Drivers ),
FILTER (
VALUES ( 'Drivers'[Hire Date] ),
'Drivers'[Hire Date] <= MAX ( Dates[Date] )
),
FILTER (
VALUES ( 'Drivers'[Termination Date] ),
OR(
Drivers[Termination Date] >= MIN ( Dates[Date] ),
ISBLANK ('Drivers'[Termination Date] )
)
),
FILTER(
Drivers,
Drivers[Activated] = "Y"
),
FILTER(
Drivers,
Drivers[Type] = "Company"
)
)
And this is the driver table I'm working with...
Any resources you can point me to in order to learn what I need to know would be greatly appreciated. I would also welcome alternative methods (like making transforming that driver count measure into a driver average measure).
I tried this adapted from a similar problem solved here on Stack Overflow, but I have to be honest and did not expect it to work.
Avg Driver Count (Company) =
AVERAGEX(VALUES(Drivers[Code]),
[Driver Count (Company)])
It resulted in '1.00' for every date range. That makes sense to me, actually. So, I suspect I have the wrong value in 'VALUES'.
I need to write a DAX measure that calculates (e.g., "Count Rows"), but only when another measure value is evaluated (e.g., filtering "[Sales]>100"). So if-- in the context of the selected filters-- Sales is great than 100, then the measure is executed only for those rows.
The measure I have defined works in the context of lower smaller grain. But the totals do not sum correctly.
Any suggestions?
DAX Measure
License Usage =
// Users with active viewership in 3 months
IF (
NOT ( ISBLANK (
CALCULATE (
[Activity Date NEWEST],
KEEPFILTERS ( DATESINPERIOD ( dimCalendar[Date], TODAY (), -90, DAY ) )
)
)), 1
)
Activity Date NEWEST =
MAX('PBI Activity'[Date])
Okay, I figured something out that works.
DAX
License Usage =
// Users with active viewership in 3 months
CALCULATE (
[Count Users],
FILTER ( 'PBI Activity', 'PBI Activity'[Date] >= TODAY () - 90 )
)
Count Users = COUNTROWS('Users')
Also, I later came across this article which looks like it also does what I was hoping to do: Execute calculate expression over filtered rows based upon measure filter.
Reference: Specifying multiple filter conditions in CALCULATE - SQLBI
DAX
DEFINE
MEASURE Sales[Big Sales Amount] =
CALCULATE (
[Sales Amount],
KEEPFILTERS (
FILTER (
ALL ( Sales[Quantity], Sales[Net Price] ),
Sales[Quantity] * Sales[Net Price] > 1000
)
)
)
EVALUATE
SUMMARIZECOLUMNS (
Sales[Quantity],
"Sales Amount", [Sales Amount],
"Big Sales Amount", [Big Sales Amount]
)
I tried to make a ranking on a date so that every beginning of a month it resets but I can't get it. the furthest I got is that every month simply gets an cumulative ranking based on RANKX on power bi which includes a month and a year.
Has anyone needed such a ranking and succeeded?
A generalised set-up for a Calculated Column would be along the lines of:
=
VAR ThisMonth =
MONTH( Table1[Date] )
RETURN
RANKX(
FILTER( Table1, MONTH( Table1[Date] ) = ThisMonth ),
Table1[Date],
,
ASC
)
Have a simple, 2 table model: [Sales]*-[Calendar] with sales that span from 1/1/2021 to yesterday. Day grain. Calendar is a standard date dimension.
Need: I want a line chart that shows the sales trend for each day since the first of the month, 3 months back, to yesterday.
I want this line chart measure to ignore the influence from all Calendar[Date] slicers/filter that happen to be on the report.
I understand I can use the "edit interactions" feature and stop the Date slicers from affecting the trend line - and that works - with this code.
However, I want to learn how to ignore these external filter affects within DAX & not rely on using the edit-interaction feature.
I've gotten this far. This code calcs the measure I want, but a date slicer on the report affects it and I don't want it to. What am I missing?
Total $ Last 3 Months Trend =
VAR __monthsBack = 3
VAR __endDate =
CALCULATE ( MAX ( Sales[SalesDate] ), ALL ( 'Calendar' ) )
VAR __endDateMinus_NMonths =
EDATE ( __endDate, - __monthsBack )
VAR __firstDate =
DATE ( YEAR ( __endDateMinus_NMonths ), MONTH ( __endDateMinus_NMonths ), 01 )
RETURN
IF (
MAX ( 'Calendar'[Date] ) <= __endDate
&& MAX ( 'Calendar'[Date] ) >= __firstDate,
CALCULATE (
[Total $],
FILTER (
'Calendar',
'Calendar'[Date] >= __firstDate
&& 'Calendar'[Date] <= __endDate
),
REMOVEFILTERS ( 'Calendar'[Date] )
),
BLANK ()
)
Request: Calculate the number of Purchased Orders past due. Recalculate when filters applied.
Background: Have 3 tables:
Purchase_Orders containing order [Promise Date],
Received_Orders containing [Received Date],
PO_DIM containing unique Purchase Order Numbers.
Relation: **Purchase_Orders -1 PO_DIM 1- Received_Orders
Measure Sudo Code:
1. CountRows
2. Filter for rows where Purchase_Orders[Promised Date] is not Blank()
3. Filter for rows where Received_Orders[Received Date] is Blank()
4. Filter for rows where Purchase_Orders[Promised Date] < Today()
How would you solve?
I would roll the 3 Filter requirements into one FILTER function, using RELATED to test columns from other tables, something like:
Count PO Pastdue =
CALCULATE (
COUNTROWS ( 'PO_DIM' ),
FILTER (
'PO_DIM',
NOT ( ISBLANK ( RELATED ( Purchase_Orders[Promised Date] ) ) )
&& ISBLANK ( RELATED ( Received_Orders[Received Date] ) )
&& RELATED ( Purchase_Orders[Promised Date] ) < TODAY ()
)
)