Days between orderdates per Customer and first order value - powerbi

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

Related

Use RankX based on a measure with dynamic max date

I have a data set of game logs for a basketball season. I want to do a ranking for a statistic comparing all teams, but only up to the current date of that specific row. Since not every team plays every day, the current row should be the max date and it should rank based on the most recent value up to that date. I have a measure that does the correct calculation:
LatestTmDVP =
VAR thisdate =
MAX ( [DATE] )
VAR maxdate =
CALCULATE ( MAX ( [DATE] ), ALLSELECTED ( 'NBA-PLAYER-FEED'[DATE] ) )
VAR result =
CALCULATE (
MAX ( 'NBA-PLAYER-FEED'[TmDVPoToDt] ),
'NBA-PLAYER-FEED'[DATE] = maxdate
)
RETURN
IF ( thisdate = maxdate, result )
If I put this in a report with a date slicer, I can see that it is working correctly:
So I need to get this measure into a column, with the max date set as the date of the current row, and then do a rank based on that, but I just can't get the RankX syntax right for this. Appreciate any help!

Running total with a slicer in Powerbi

I am trying to find the running total by month, it worked using the following measure but only without using a slicer for the category "BLK":
With Date = VAR MaxDate =
MAX ( 'Calendar Table'[Date] ) RETURN
CALCULATE (
SUM ( 'Injuries'[Total] ),
FILTER (
ALL ( 'Injuries'[Classification] ),
'Injuries'[Classification] = "FAC"
),
FILTER ( ALLSELECTED ( 'Calendar Table' ), 'Calendar Table'[Date] <= MaxDate)
)+0
The result was only accurate when I don't apply the Category slicer "BLK", I tried a different measure:
With BLK = CALCULATE( SUM('Oxy Oman Personal Injuries'[Total]),
FILTER (
ALL ( 'Injuries'[Classification] ),
'Injuries'[Classification] = "FAC"
), ALLSELECTED('Blocks'[BLK]) )+0
The above gave me something similar to the actual data, it did not sum up the numbers.. at least it gave me the correct total of a the category selected = 5 while the first measure gave the total wrong = 4.
Here is a screenshot of the results for both measures with and without a slicer:
Without BLK slicer:(With Data gave me what I wanted)
Using one Category in the BLK Slicer:
How can I fix that?

Power BI: Use unrelated date table to create a measure

Hi I have been struggling with this for a bit now and I hope I didn't miss a previous question. I am trying to get a count of the vehicles we have based on an EOMONTH date. We are buying and selling cars on a regular basis and for reporting we need to know how many we had at the end of each month and the report is a rolling 12 months.
I've tried creating the relationship with the purchasedate of the vehicle to the date of my date table but when I create the measure (Used to calculate the number of vehicles purchased but haven't been sold):
SalesBlank = CALCULATE(
COUNT(Vehicles[MVANumber]),
FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate])))
I only get a count of vehicles purchased that month and don't have a sale date - I'm not surprised because my relationship with the date table is the purchase date.
How can I set up a measure to look at the date table and filter the vehicles table with this logic:
purchasedate <= date[EOMONTH] && ISBLANK(salesdate)
Any help would be greatly appreciated!!
Thanks,
Matt
Sample Data and Desired Results
Relationships
If I understand you correctly, you want to get a count of the vehicles on hand at the end of each month. That could be calculated by counting the vehicles with a purchase date less than or equal to the selected end of month and subtracting the count of vehicles with a sale date less than or equal to the selected end of month.
You can create an active relationship between Vehicle[PurchaseDate] and Date[Date]. Then create an inactive relationship based upon Vehicles[SaleDate] and Date[Date].
You could use a measure that is something like this:
Inventory Count =
VAR MaxDate =
MAX ( 'Date'[Date] )
VAR MinDate =
CALCULATE ( MIN ( 'Date'[Date] ), ALL ( 'Date' ) )
VAR Purch =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR Sales =
CALCULATE (
COUNT ( 'Vehicles'[VehicleID] ),
USERELATIONSHIP ( 'Date'[Date], Vehicles[Sale Date] ),
DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
)
VAR MaxPurDt =
CALCULATE ( MAX ( 'Vehicles'[Purchase Date] ), ALL ( 'Vehicles' ) )
VAR MaxSlDt =
CALCULATE ( MAX ( 'Vehicles'[Sale Date] ), ALL ( 'Vehicles' ) )
RETURN
IF (
MIN ( 'Date'[Date] ) <= MaxPurDt
|| MIN ( 'Date'[Date] ) <= MaxSlDt,
Purch - Sales
)
This measure gets a cumulative count of purchases and a cumulative count of sales and then subtracts them. The IF check is to avoid propagation of cumulative totals beyond the maximum date in the Vehicle table.
I'm not sure how to interpret your showing of just 3 months in the desired results. This will produce the same answers as what you have, but without a filter applied to the table, it starts at 3/31/2016 (the date of the first sale).
Edit: There's probably a more efficient way along the lines you were thinking, but it is escaping me at the moment.

The last day of month from the last date with sales

How can I get the last day of month from the last date with sales? I want the result to be returned as a DAX calculated table.
The first step, calculated table:
LastDate = LASTNONBLANK( T[Date], CALCULATE( SUM( T[Amount] ) ) )
It returns a table (one column, one row) with the last date with sales. So far, correct, as expected. Say the last day with sales is 2020-04-15. But I want the end of month so 2020-04-30.
I hoped this should work, but it doesn't.
LastDate =
ENDOFMONTH (
LASTNONBLANK (
T[Date],
CALCULATE ( SUM ( T[Amount] ) )
)
)
It still returns 2020-04-15, instead of expected 2020-04-30.
The problem is caused by using dates from table T. Instead, you need to use dates from the calendar table, because list of dates for the time intelligence functions must be continuous.
Assuming you have a properly designed and connected calendar table 'Date' in your data model, change your DAX to:
LastDate =
ENDOFMONTH (
LASTNONBLANK (
Date[Date],
CALCULATE ( SUM ( T[Amount] ) )
)
)
ENDOFMONTH will use now a different table (Date), because LASTNONBLANK function will keep data lineage to it.

Using summarize and userelationship to generate a sum based on a condition

Story:
I have two date columns in my fact table, one is for orderdate and the second is for orderdate/refund/cancelled date.
I created two relationships between the date table and the fact table.
Active: Date > OrderDate
Inactive: Date > OtherDate
I would like to sum the # of refunds per day using the inactive relationship.
What I tried:
Returns =
VAR summary =
SUMMARIZE (
FILTER ( Query1, Query1[kind] = "refund" ),
Query1[orderId],
"returns", MAX ( Query1[amount] )
)
RETURN
CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[OtherDate], DateTable[Date] ),
summary
)
For some reason, it's using the active date column. Any suggestion on how to fix the above formula?
I'm not sure I understand how you are intending to use the summary variable here, but note that USERELATIONSHIP doesn't affect it at all since it's already computed.
You might not need that variable at all. Try this:
Returns =
CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[OtherDate], DateTable[Date] ),
Query1[kind] = "refund"
)