Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreShape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.
What I am trying to implement is something like this. Query to obtain the same is given below.
Segment | Last Period | Current Period | Change | Chagne % | Retained | Retention |
Decreasing Customer | 150 | 78 | -72 | -48% | 78 | 52% |
Increasing Customer | 88 | 225 | 137 | 156% | 88 | 100% |
Lost Customer | 74 | 0 | -74 | -100% | 0 | 0% |
New Customer | 0 | 76 | 76 | 0 | ||
Total | 312 | 379 | 67 | 21% | 166 | 53% |
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
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
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?
User | Count |
---|---|
92 | |
88 | |
88 | |
79 | |
49 |
User | Count |
---|---|
156 | |
145 | |
105 | |
72 | |
55 |