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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Employee
Employee


@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



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors