Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hi!
I am pretty new in power-bi and need help with a formula and logic for a customer analysis. Since I couldn't find any similar issue in the forum I try to explain my situation below.
The company I work for run 3 different brands, lets call them lens1, lens2, and lens3. What I now need to get a grip of is (1) how many customers as we have in each brand (witin the same market), (2) how many customers that belongs to more than one brand, and also (3) whithin which brand the latest purchase took place.
Sample data is attached below:
Sales table
Brand_key | market_key | customer_key | order_number | order_date | latest_order_date |
1 | 11 | 123 | 1001 | 09-jan-18 | 10-dec-19 |
1 | 11 | 123 | 1020 | 10-dec-19 | 10-dec-19 |
2 | 11 | 123 | 1100 | 05-jan-17 | 05-jan-17 |
3 | 11 | 123 | 1200 | 20-jun-18 | 20-jun-18 |
1 | 14 | 125 | 1201 | 11-jan-20 | 11-jan-20 |
1 | 14 | 126 | 1202 | 12-jan-20 | 12-jan-20 |
2 | 14 | 125 | 1205 | 02-feb-20 | 02-feb-20 |
3 | 14 | 127 | 1206 | 10-mar-20 | 10-mar-20 |
1 | 12 | 128 | 1207 | 10-mar-20 | 10-mar-20 |
2 | 12 | 129 | 1208 | 10-jun-20 | 10-jun-20 |
3 | 12 | 129 | 1209 | 10-jul-20 | 10-jul-20 |
1 | 14 | 130 | 1210 | 10-aug-20 | 10-aug-20 |
2 | 14 | 130 | 1211 | 11-aug-20 | 11-aug-20 |
1 | 14 | 131 | 1212 | 12-aug-20 | 12-aug-20 |
2 | 12 | 132 | 1213 | 10-jul-20 | 10-jul-20 |
1 | 13 | 133 | 1005 | 10-aug-17 | 10-jun-20 |
1 | 13 | 133 | 1215 | 10-jun-20 | 10-jun-20 |
2 | 13 | 133 | 1220 | 05-jun-19 | 05-jun-19 |
3 | 13 | 133 | 1225 | 11-mar-19 | 11-mar-19 |
Brand table
Brand_key | Brand_name |
1 | lens1 |
2 | lens2 |
3 | lens3 |
Market table
Market_key | Market_NAME |
11 | FINLAND |
12 | DENMARK |
13 | NORWAY |
14 | SWEDEN |
Customer table
CUSTOMER_KEY | CUSTOMER_EMAIL |
123 | 123@gmail.com |
125 | 125@gmail.com |
126 | 126@gmail.com |
127 | 127@gmail.com |
128 | 128@gmail.com |
129 | 129@gmail.com |
130 | 130@gmail.com |
131 | 131@gmail.com |
132 | 132@gmail.com |
133 | 133@gmail.com |
To be mention is that last order date within the sales table is a calculated column based brand, market, and customer key. The latest order date will always be max.
Expected output from sample data:
Total no customers in lens 1 | 7 |
Total no customers in lens 2 | 6 |
Total no customers in lens 3 | 4 |
Customers purchased in both lens1&lens2 (in same market) | 4 |
Customers purchased in both lens1&lens3 (in same market) | 2 |
Customers purchased in both lens2&lens3 (in same market) | 3 |
Customer purchased in lens1&lens2 with latest purchase in lens1 (in same market) | 2 |
Customer purchased in lens2&lens3 with latest purchase in lens2 (in same market) | 1 |
Customer purchased in lens1&lens3 with latest purchase in lens1 (in same market) | 2 |
Explanation in words
The following three questions need to be answered:
(1) What is the total number of customers for each brand?
First I need to understand how many customers I actually have for each brand. The brand is here called lens1, lens2, lens3.
(2) How many customers belongs to more than one brand?
When I know the answer of (1), I need to undertand how many customers that are customers/ have purchased from another brand as well. For example, no customer in lens1 & lens2 are in this sample data 4.
(3) Within which took the latest purchase place?
When knowing which customers that belongs to more than one brand (2), I need to undertand where the latest purchase took place. For example, customers in both lens1 &lens2 where the latest purchase was in lens1 is in the sample data 2.
Hope that someone has the an idea of how to to this - Thanks in advance!
Solved! Go to Solution.
Hi @Anonymous ,
How about this?
1. Create measures.
Customers purchased more brand =
COUNTROWS (
FILTER (
VALUES ( Sales[customer_key] ),
COUNTROWS (
INTERSECT (
CALCULATETABLE ( VALUES ( Sales[Brand_key] ) ),
VALUES ( Brand[Brand_key] )
)
)
= COUNTROWS ( VALUES ( Brand[Brand_key] ) )
)
)
Customers purchased more brand 2 =
VAR MinBrandKey =
MIN ( Brand[Brand_key] )
VAR t =
SUMMARIZE (
FILTER (
ADDCOLUMNS (
Sales,
"Last_", CALCULATE (
MAX ( Sales[latest_order_date] ),
FILTER (
ALLSELECTED ( Sales ),
Sales[market_key] = MAX ( Sales[market_key] )
&& Sales[customer_key] = MAX ( Sales[customer_key] )
)
)
),
[Last_] = [latest_order_date]
&& [Brand_key] = MinBrandKey
),
[customer_key]
)
RETURN
COUNTROWS (
FILTER (
t,
COUNTROWS (
INTERSECT (
CALCULATETABLE ( VALUES ( Sales[Brand_key] ) ),
VALUES ( Brand[Brand_key] )
)
)
= COUNTROWS ( VALUES ( Brand[Brand_key] ) )
)
)
Customers purchased more brand and latest order date in min selected brand =
SUMX ( VALUES ( Sales[customer_key] ), [Customers purchased more brand 2] )
Title 1 =
IF (
COUNTROWS ( VALUES ( Brand ) ) = 1,
"Total no customers in " & SELECTEDVALUE ( Brand[Brand_name] ),
"Customers purchased in "
& CONCATENATEX ( VALUES ( Brand[Brand_name] ), [Brand_name], " & " ) & " (in same market)"
)
Title 2 =
IF (
COUNTROWS ( VALUES ( Brand ) ) = 1,
"Total no customers in " & SELECTEDVALUE ( Brand[Brand_name] ),
"Customer purchased in "
& CONCATENATEX ( VALUES ( Brand[Brand_name] ), [Brand_name], " & " ) & " with latest purchase in "
& MIN ( Brand[Brand_name] ) & " in same market"
)
Total no. customers = DISTINCTCOUNT(Sales[customer_key])
2. Create visuals.
3. Test.
BTW, .pbix file attached.
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @Anonymous ,
How about this?
1. Create measures.
Customers purchased more brand =
COUNTROWS (
FILTER (
VALUES ( Sales[customer_key] ),
COUNTROWS (
INTERSECT (
CALCULATETABLE ( VALUES ( Sales[Brand_key] ) ),
VALUES ( Brand[Brand_key] )
)
)
= COUNTROWS ( VALUES ( Brand[Brand_key] ) )
)
)
Customers purchased more brand 2 =
VAR MinBrandKey =
MIN ( Brand[Brand_key] )
VAR t =
SUMMARIZE (
FILTER (
ADDCOLUMNS (
Sales,
"Last_", CALCULATE (
MAX ( Sales[latest_order_date] ),
FILTER (
ALLSELECTED ( Sales ),
Sales[market_key] = MAX ( Sales[market_key] )
&& Sales[customer_key] = MAX ( Sales[customer_key] )
)
)
),
[Last_] = [latest_order_date]
&& [Brand_key] = MinBrandKey
),
[customer_key]
)
RETURN
COUNTROWS (
FILTER (
t,
COUNTROWS (
INTERSECT (
CALCULATETABLE ( VALUES ( Sales[Brand_key] ) ),
VALUES ( Brand[Brand_key] )
)
)
= COUNTROWS ( VALUES ( Brand[Brand_key] ) )
)
)
Customers purchased more brand and latest order date in min selected brand =
SUMX ( VALUES ( Sales[customer_key] ), [Customers purchased more brand 2] )
Title 1 =
IF (
COUNTROWS ( VALUES ( Brand ) ) = 1,
"Total no customers in " & SELECTEDVALUE ( Brand[Brand_name] ),
"Customers purchased in "
& CONCATENATEX ( VALUES ( Brand[Brand_name] ), [Brand_name], " & " ) & " (in same market)"
)
Title 2 =
IF (
COUNTROWS ( VALUES ( Brand ) ) = 1,
"Total no customers in " & SELECTEDVALUE ( Brand[Brand_name] ),
"Customer purchased in "
& CONCATENATEX ( VALUES ( Brand[Brand_name] ), [Brand_name], " & " ) & " with latest purchase in "
& MIN ( Brand[Brand_name] ) & " in same market"
)
Total no. customers = DISTINCTCOUNT(Sales[customer_key])
2. Create visuals.
3. Test.
BTW, .pbix file attached.
Best Regards,
Icey
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
@Anonymous
I worked out a solution but I was not sure about the last question, please check.
You can download the file: HERE
________________________
Did I answer your question? Mark this post as a solution, this will help others!.
Click on the Thumbs-Up icon on the right if you like this reply 🙂
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
107 | |
99 | |
76 | |
64 | |
58 |
User | Count |
---|---|
148 | |
113 | |
97 | |
84 | |
67 |