Forum Discussion
Counting based on a condition
- 5 years ago
Hi reynaldo_malave . Try this...
VAR StoreSummary = SUMMARIZE( VALUES('stores'[store_key]), "CurrentYearSales", [Sales TY], "PriorYearSales", [Sales LY] ) RETURN CALCULATE( COUNT('stores'[store_key]), FILTER( StoreSummary, [PriorYearSales] > [CurrentYearSales] ) ) - 5 years ago
Hi reynaldo_malave . Remember when I said I was surprised it was correct because I wrote it in Notepad? 🙄
There were two issues. First, the measure for [Sales TY] is returning all sales regardless of year (unless it's filtered by year). The second is my measure left out the Store Key in the summary table variable.Total Sales = SUM(Sales[sales]) Sales TY = TOTALYTD( [Total Sales], 'Calendar'[Date] ) Sales LY = CALCULATE( [Sales TY], SAMEPERIODLASTYEAR('Calendar'[Date]) ) -- Used to check the counts only Sales YOY Change = [Sales TY] - [Sales LY] Negative Stores = VAR StoreSummary = SUMMARIZE( VALUES('stores'[store_key]), Stores[store_key], "CurrentYearSales", [Sales TY], "PriorYearSales", [Sales LY] ) RETURN CALCULATE( COUNT('stores'[store_key]), FILTER( StoreSummary, [PriorYearSales] > [CurrentYearSales] ) )
reynaldo_malave No worries! 🙂
Can you give me some raw data to work with and I'll figure this out
littlemojopuppy you can download it from here. The correct answer should be 4. Thanks
https://drive.google.com/file/d/1VQNfOtFrIJu_maXjFqJeoxTiNz0X0R_3/view?usp=sharing
- littlemojopuppy5 years agoCommunity Champion
Hi reynaldo_malave . Remember when I said I was surprised it was correct because I wrote it in Notepad? 🙄
There were two issues. First, the measure for [Sales TY] is returning all sales regardless of year (unless it's filtered by year). The second is my measure left out the Store Key in the summary table variable.Total Sales = SUM(Sales[sales]) Sales TY = TOTALYTD( [Total Sales], 'Calendar'[Date] ) Sales LY = CALCULATE( [Sales TY], SAMEPERIODLASTYEAR('Calendar'[Date]) ) -- Used to check the counts only Sales YOY Change = [Sales TY] - [Sales LY] Negative Stores = VAR StoreSummary = SUMMARIZE( VALUES('stores'[store_key]), Stores[store_key], "CurrentYearSales", [Sales TY], "PriorYearSales", [Sales LY] ) RETURN CALCULATE( COUNT('stores'[store_key]), FILTER( StoreSummary, [PriorYearSales] > [CurrentYearSales] ) )- reynaldo_malave5 years agoHelper III
littlemojopuppy You are a life saver. Thanks for taking the time. I did learn how to summarize. The hard way!
- littlemojopuppy5 years agoCommunity Champion
reynaldo_malave it's no trouble. Wish I hadn't flubbed it yesterday 😑