Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

July 2024 Power BI Update

Power BI Monthly Update - July 2024

Check out the July 2024 Power BI update to learn about new features.

July Newsletter

Fabric Community Update - July 2024

Find out what's new and trending in the Fabric Community.