cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
varundevaraj Frequent Visitor
Frequent Visitor

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
Community Support Team
Community Support Team

Re: new lost customers based on current and last year volume

Hi @varundevaraj,

 

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 other members find it more quickly.
varundevaraj Frequent Visitor
Frequent Visitor

Re: new lost customers based on current and last year volume

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
Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Power BI Helps Homeless and Trouble Youth

Power BI Helps Homeless and Trouble Youth

We spoke with Power BI Super User, Greg Deckler, about his charity work

MBAS Gallery

Watch Sessions On Demand!

Continue your learning in our online communities.

Top Kudoed Authors
Users Online
Currently online: 260 members 2,955 guests
Please welcome our newest community members: