cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sieed
Helper II
Helper II

Dynamically compute a calculated column based on date slicer

I have two tables namely category and sales. The category table contains categoryName, categoryID, and subcategoryIDs (which is unique).  The sales table contanis the subcategoryID and sales date. 

 

The sales tables is connected to the category table via subcategoryid. ( One to many relation) 


1) I have created a calculated column called count of subcategoryID in the category table which basically counts the occurence of each subcategory ID in the sales table. 

 

count of subcategoryID = COUNTROWS(RELATEDTABLE(sales))

 

2) I have created another calculated column called New Category Name which sums up the values from the column created above based on category ID . If  SUM is < 5, then the value is "Other", else we retain the Category Name. 
   Here's the code: 

   New Category Name  = IF(CALCULATE (
SUM ('category'[count of subcategoryID] ),
FILTER (
ALL ( 'category' ),
'category'[categoryID] = EARLIER('category'[categoryID])
)
) < 5, "Other", 'category'[category name])

 

 

I have a pie chart which displays the New Category Name based on the sales dates. I also have a date slicer for sales date. The problem is, when I change the date slider, the pie chart displays the CategoryName of categories whose count is less than 5 instead of labelling them as "other" and counting them in the "Other" bucket. 

I am assuming the issue is because the calculated column New Category Name has the calculation for the entire span of sales dates instead of the date range selected in the slider. How can I dynamically change New Category Name based on slider dates ? 

6 REPLIES 6
Eric_Zhang
Microsoft
Microsoft


@sieed wrote:

I have two tables namely category and sales. The category table contains categoryName, categoryID, and subcategoryIDs (which is unique).  The sales table contanis the subcategoryID and sales date. 

 

The sales tables is connected to the category table via subcategoryid. ( One to many relation) 


1) I have created a calculated column called count of subcategoryID in the category table which basically counts the occurence of each subcategory ID in the sales table. 

 

count of subcategoryID = COUNTROWS(RELATEDTABLE(sales))

 

2) I have created another calculated column called New Category Name which sums up the values from the column created above based on category ID . If  SUM is < 5, then the value is "Other", else we retain the Category Name. 
   Here's the code: 

   New Category Name  = IF(CALCULATE (
SUM ('category'[count of subcategoryID] ),
FILTER (
ALL ( 'category' ),
'category'[categoryID] = EARLIER('category'[categoryID])
)
) < 5, "Other", 'category'[category name])

 

 

I have a pie chart which displays the New Category Name based on the sales dates. I also have a date slicer for sales date. The problem is, when I change the date slider, the pie chart displays the CategoryName of categories whose count is less than 5 instead of labelling them as "other" and counting them in the "Other" bucket. 

I am assuming the issue is because the calculated column New Category Name has the calculation for the entire span of sales dates instead of the date range selected in the slider. How can I dynamically change New Category Name based on slider dates ? 


Yes, the calculated column won't vary according to slicer selection. You can take some tricks by using measures to show [category name] or "Other" values, but you can't use that as a legend/details in a pie chart.

@Eric_Zhang, what are the tricks? Wold appreciate if you can share some more info. 


@sieed wrote:

@Eric_Zhang, what are the tricks? Wold appreciate if you can share some more info. 


The trick is like creating a measure as below

 

Capture0.PNGCapture.PNGCapture2.PNG

@sieed, did you created new columns or measures? Based on formulas, seems like you added new measures. Can you share some sample data to help provide the solution? Thanks






Did I answer your question? Mark my post as a solution.

Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.





Anonymous
Not applicable

What happens if you use ALLEXCEPT like this:

New Category Name  = IF(CALCULATE (
SUM ('category'[count of subcategoryID] ),
FILTER (
ALLEXCEPT ( 'category', 'category'[Date] ),
'category'[categoryID] = EARLIER('category'[categoryID])
)
) < 5, "Other", 'category'[category name])

Where 'category'[Date] is your date field and the date used in your slicer.

@Anonymous, there is no category date in the category table. There i showever sales_date in the sales table. Do you want me to use that instead? 

Helpful resources

Announcements
November 2022 Update

Check it Out!

Click here to read more about the November 2022 updates!

Microsoft 365 Conference â__ December 6-8, 2022

Microsoft 365 Conference - 06-08 December

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.