Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now
Hello everyone. I started to migrate some Excel reports to Power BI and one of those was the Retention Rate.
It was written in SQL and basically I have one table with all the information I want. The SQL code to generate this report is something like this:
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='#TotalOrders' and xtype='U')
CREATE TABLE #TotalOrders
(
email VARCHAR(50),
orderdatetime VARCHAR(50),
shippingcountry VARCHAR(50),
orderreference VARCHAR(50),
fraudulent BIT
)
INSERT INTO #TotalOrders
SELECT email, orderdatetime,shippingcountry,orderreference,fraudulent FROM ORDERS
GO
DECLARE @date AS Date
DECLARE @months AS INTEGER
-----------------------------
SET @date = CONVERT(DATETIME, '2020-08-01')
SET @months = -1
SELECT @date AS 'Date',
COUNT( distinct total.email) AS total,
COUNT( distinct retained.email) AS retained,
SUBSTRING(CAST(ROUND((1.0 * COUNT( distinct retained.email))/ (1.0 * COUNT( distinct total.email)
),4)*100 AS VARCHAR(20)),0,6) + '%' AS pcn
FROM #TotalOrders AS total
LEFT JOIN #TotalOrders AS retained
ON ( total.email = retained.email AND
CONVERT(datetime,retained.orderdatetime, 103) BETWEEN DATEADD(Month,@months,@date)
AND @date AND retained.fraudulent = 0)
WHERE (CONVERT(datetime, total.orderdatetime, 103) BETWEEN
DATEADD(Month,2*@months,@date) AND DATEADD(Month, @months,@date))
AND total.fraudulent = 0
DROP TABLE #TotalOrders
And the result is something like this:
Date total retained pcn
2020-08-01 19354 1618 8.360%
Explaining the SQL code, we have 2 variables, one for the date and the months.
So, in this example, we get the emails from orders from June (months * 2) and check in July (months) how many customers bought again.
If the months variable was -2 we would go 2 months before august (April and May) and we checked in June / July how many emails repeat themselves.
How can I build something similar to this using DAX? I know it has outstanding capabilities, but I'm still getting used to all the syntax and methods.
The model on Power BI has the same names and for the variables, the date would be the MIN(date) - which will get the first day of the month) and for the variable months I'm thinking about using a parameter.
Appreciate it everyone that could lend me a hand with this! Have a good day everyone!
Thank you, both of you. The pbix file can be downloaded here.
Basically this sample file only has the date when the order was placed and the user email (which was hashed). Also there's a column called frauduent, which should be filter as 0, pretty simple.
Once again, thank you for your help guys!
Don't use a date hierarchy on the orderdatetime field. Instead, create a calculated column that only has the date value of orderdatetime, and then add a proper calendar table that holds the required granularity (day, month etc)
Hello lbendlin.
I fixed my sample file. Actually our original file has the data table like you said and I had forgotten to create the same table on this one. The link file is the same and can be downloaded here.
Really hope you guys can help me on how to build the retention rate!
Hi @Anonymous
You might consider providing your dummy pbix that would be helpful for us to investigate it further.
You can upload it to the onedrive for business and share the link here. please don't forget to disclose the expected results and remove the confidential info.
please provide sample data in usable format and show expected outcome .
Check out the October 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
113 | |
89 | |
84 | |
76 | |
64 |
User | Count |
---|---|
135 | |
112 | |
98 | |
98 | |
92 |