cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

new lost customers based on current and last year volume

What I am trying to implement is something like this. Query to obtain the same is given below.

 

SegmentLast PeriodCurrent PeriodChangeChagne %RetainedRetention
Decreasing Customer15078-72-48%7852%
Increasing Customer88225137156%88100%
Lost Customer740-74-100%00%
New Customer07676 0 
Total3123796721%16653%

I am trying to get a general idea on how to deal with subqueries and grouping. Checked some methods with "summarize" but can't get it to work. 

 

created a table with vol_cy and vol_ly grouped on account number and name. But trying to figure out to get the remaining part to work is not leading me anywhere

  

SELECT

SEGMENT
, COUNT(*) AS ACCOUNTS
, SUM(CY_VOL) AS CY_VOL
, SUM(LY_VOL) AS LY_VOL
, SUM(RETAINED_VOL) AS RETAINED_VOL
, CASE WHEN SUM(LY_VOL) > 0
THEN SUM(FLOAT(RETAINED_VOL))/SUM(FLOAT(LY_VOL))
ELSE 0 END AS RETENTION


FROM(

SELECT
AccountNumber
, AccountName
, CY_VOL
, LY_VOL
, CASE WHEN CY_VOL > 0 AND LY_VOL = 0
THEN 'New'
WHEN CY_VOL = 0 AND LY_VOL > 0
THEN 'Lost'
WHEN CY_VOL >= LY_VOL
THEN 'Increase'
ELSE 'DECREASE' END AS SEGMENT
, CASE WHEN CY_VOL > 0 AND LY_VOL = 0
THEN 0
WHEN CY_VOL = 0 AND LY_VOL > 0
THEN 0
WHEN CY_VOL >= LY_VOL
THEN LY_VOL
ELSE CY_VOL END AS RETAINED_VOL

FROM(

 

SELECT
AccountNumber
, AccountName
, SUM(CASE WHEN YEAR = 2018 THEN VOLUME ELSE 0 END) AS CY_VOL
, SUM(CASE WHEN YEAR = 2017 THEN VOLUME ELSE 0 END) AS LY_VOL

FROM Table1

WHERE YEAR IN (2017, 2018)

GROUP BY
AccountNumber
, AccountName


)T

)TT

GROUP BY SEGMENT

2 REPLIES 2
v-frfei-msft
Community Support
Community Support

Hi @Anonymous,

 

Could you please share the pbix or sample data to me, if you don't have confidential data. Please upload the files to One Drive and share the link here.

 

Regards,

Frank

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Anonymous
Not applicable

Data is Confidential. What I tried is

 

I split the query into 2 parts

 

First summarize() will do the  

 

SELECT
AccountNumber
, AccountName
, SUM(CASE WHEN YEAR = 2018 THEN VOLUME ELSE 0 END) AS CY_VOL
, SUM(CASE WHEN YEAR = 2017 THEN VOLUME ELSE 0 END) AS LY_VOL

FROM Table1

WHERE YEAR IN (2017, 2018)

GROUP BY
AccountNumber

DAX:

Grouped =

SUMMARIZE('Table1, 

AccountNumber, 

AccountName,

"VOlume CY", CALCULATE(SUM(Table1[Volume]), FILTER(Calendar, Calendar[Year] =  2018])),

"VOlume LY", CALCULATE(SUM(Table1[Volume]), FILTER(Calendar, Calendar[Year] =  2017])),

Year

)

 

then use calculated columns to obtain SEGMENT and RETAINED_VOL of the below query.

, CASE WHEN CY_VOL > 0 AND LY_VOL = 0
THEN 'New'
WHEN CY_VOL = 0 AND LY_VOL > 0
THEN 'Lost'
WHEN CY_VOL >= LY_VOL
THEN 'Increase'
ELSE 'DECREASE' END AS SEGMENT
, CASE WHEN CY_VOL > 0 AND LY_VOL = 0
THEN 0
WHEN CY_VOL = 0 AND LY_VOL > 0
THEN 0
WHEN CY_VOL >= LY_VOL
THEN LY_VOL
ELSE CY_VOL END AS RETAINED_VOL

 

 

Then used SUMMARIZE() to create final table from the result table "Grouped" created using the first SUMMARIZE()

SEGMENT
, COUNT(*) AS ACCOUNTS
, SUM(CY_VOL) AS CY_VOL
, SUM(LY_VOL) AS LY_VOL
, SUM(RETAINED_VOL) AS RETAINED_VOL
, CASE WHEN SUM(LY_VOL) > 0
THEN SUM(FLOAT(RETAINED_VOL))/SUM(FLOAT(LY_VOL))
ELSE 0 END AS RETENTION

 

Final =SUMMARIZE(Grouped,

Grouped[Segment],

COUNTROWS(Grouped),

Grouped[Volume CY],

Grouped[Volume LY],

Grouped[RetainedVolume],

"Retention", DIVIDE(SUM(Grouped[RetainedVolume),SUM(Grouped[Volume LY]))

)

 

However, the resulting values I get doesn't match with the result I get from the SQL query. Can someone let me know if the approach is correct or not?

 

 

 

 

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

BizApps LATAM 2023

Business Application LATAM Summit 2023

Join the biggest FREE Business Applications Event in LATAM this February.

Power Platform Bootcamp

Global Power Platform Bootcamp

In this bootcamp we will deep-dive into Microsoft’s Power Platform stack with hands-on sessions and labs, delivered to you by experts and community leaders.