AVERAGEX with a measure - powerbi

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'.

Related

Count the number of occurrences after associated date of certain value in Power BI

I'm conducting an exercise around examining test results after tutoring has occurred. Essentially looking at the rates of "pass" post tutoring within the context of a given student. Where the ultimate outcome would be:
pass rate after tutoring = [count passes]/[count test date] WHERE test date > tutoring date.
For example:
Ideally, the final output of the measure would be = 1 (1/1)
Would anyone be able to point me in the direction of achieving this through a Power BI measure?
I've attempted the following to get the single oc with no luck:
Measure 3 = CALCULATE(COUNT(Table[Test Pass?]),FILTER(Table,Table[Test Date]>CALCULATE(Min(Table[Tutoring Date]),FILTER(Table,Table[Tutor (?)] <> BLANK ))))
Where I would then use the student column in a matrix with the measure to group pass rates post tutoring by student
I have used this simple flat table data model:
You can calculate this with a measure that needs to be evaluated with your Student column:
Pass Rate After Tutoring =
VAR _tutor_date =
CALCULATE (
MAX ( 'Table'[Tutoring Date] ),
ALLEXCEPT ( 'Table', 'Table'[Student] )
)
VAR _tests_post_tutor =
CALCULATE (
COUNTROWS ( 'Table' ),
ALLEXCEPT ( 'Table', 'Table'[Student] ),
'Table'[Test Date] > _tutor_date
)
VAR _successes =
CALCULATE (
COUNTROWS ( 'Table' ),
ALLEXCEPT ( 'Table', 'Table'[Student] ),
'Table'[Test Date] > _tutor_date,
'Table'[Test Pass]
)
RETURN
DIVIDE ( _successes, _tests_post_tutor )
But this assumes that students are only tutored for one specific test, and are tutored once.

DAX Measure (e.g., "Count Rows") based upon filtered measure value (e.g., "[Sales]>100")

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]
)

Days between orderdates per Customer and first order value

I am new to DAX and have 2 questions I would like answered for a project. I am working on figuring out what the first value per customer is so what amount does the customer come in with.
In addition I would like to know what is the time between customer's orders does the customer come back to buy something after a day, week or only after a year. And if a customer comes 5 times what is the time between the orders each time.
Data set
enter image description here
Now I tried the following but then I only get the first date back each time but then each customer gets the very first order date that exists.
First Order = CALCULATE(FIRSTDATE(Text[Datum].[Date]),VALUES(Text[Datum].[Date]))
How can I solve this two question with DAX
You can try the following:
Initial value for customer measure
Initial value =
-- find customers first appearance date
VAR first_date =
CALCULATETABLE (
FIRSTDATE ( 'Table'[Date] ),
-- remove all filters on 'Table'[Date]
REMOVEFILTERS ( 'Table'[Date] )
)
RETURN
CALCULATE (
SUM ( 'Table'[Value] ),
first_date
)
First order date measure
First Order =
CALCULATE (
FIRSTDATE ( 'Table'[Date] ),
REMOVEFILTERS ( 'Table'[Date] )
)
Days passed since last visit measure
Days passed =
VAR last_visit =
CALCULATE (
MAX ( 'Table'[Date] ),
'Table'[Date]
-- from example expecting Customer ID & Date form unique combination,
-- otherwise use MAX() instead
< SELECTEDVALUE ( 'Table'[Date] )
)
RETURN
DATEDIFF (
last_visit,
-- same here as above with SELECTEDVALUE()
SELECTEDVALUE ( 'Table'[Date] ),
DAY
)
The result will then look like this (represented in a table visual):

Moving Average with FactTable (dateID) & DateDIM PowerBI

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)
)
)

DAX: Average distinct values per day

I've got some categories spread over a number of days. The same category can occur several times on the same date. How can I get the average number of distinct categories per day?
Dataset:
Date,Category
11.10.2018,A
11.10.2018,B
11.10.2018,C
12.10.2018,A
12.10.2018,A
12.10.2018,A
13.10.2018,B
13.10.2018,B
Table from data view:
Table visualization:
My attempt:
I'm able to get distinct values per day as a measure using dist = CALCULATE(DISTINCTCOUNT(Table1[Category]);DISTINCT(Table1[Date]))
So what I'd like to end up with is the average of dist in the table above which is 1.67. But how do you do that? I've tried different combinations with AVERAGE, AVAREGEX, VALUES and CALCULATE, but with no luck. And the more I try, the more I convince myself that DAX is useless (even though I know deep down it can't be). Thank you for any suggestions!
Use SUMMARIZE to calculate the distinct count for each date, then you can use AVERAGEX to iterate over each date value:
dist:=
IF (
HASONEFILTER ( Table1[Date] ),
DISTINCTCOUNT ( Table1[Category] ),
AVERAGEX (
SUMMARIZE (
Table1,
Table1[Date],
"Daily Average", DISTINCTCOUNT ( Table1[Category] )
),
[Daily Average]
)
)
EDIT: You don't really need the IF function - it seems to perform just as well using simply:
dist:=
AVERAGEX (
SUMMARIZE (
Table1,
Table1[Date],
"Daily Average", DISTINCTCOUNT ( Table1[Category] )
),
[Daily Average]
)