Forum Discussion
Dynamic starting date based on filter for bar chart
I get data via DirectQuery and we publish our report our Power BI app, but I provided some dummy data below. We have Product P sales data starting May 2024. We have Product D and Product E sales data starting September 2024. I want the starting date to be dynamic based on the Product Slicer:
- If the user selects Product D and/or Product E (Product P selected or deselected), the starting date must be September 2024.
- If the user selects ONLY Product P, then the starting date must be May 2024.
Basically, the starting date of the bar chart must be the most recent (max) date between Product D, Product E, and Product P. (Product D and Product E have the same starting date.)
I was able to make a measure (Sum_Sales) that adds up correctly. However, I need the bar chart start date to change dynamically. There are some months with with NULL/BLANK sales. That is fine and I need that show up as empty (zero) on the chart.
We will implement Product C (and possibly other products) in the future, so the solution should work if we include more products in the future.
Also, it must work when D is selected (and/or E is selected) and P is selected, but SEP through DEC are deselected. Since there is no data for D or E, the starting date must be for P. I was having trouble with this when I was trying to figure something out.
Sum_Sales =
var _edselected = IF(HASONEVALUE('Table'[PRODUCT])
&&OR("E" in ALLSELECTED('Table'[PRODUCT]), "D" in ALLSELECTED('Table'[PRODUCT])),
TRUE(), FALSE())
var _eddate = IF(_edselected, "202409", "202405")
RETURN CALCULATE(SUMX('Table', 'Table'[SALES]), 'Table'[YRMO] >= _eddate)+0
Scenario 1. D and/or E selected (P doesn't matter)
Scenario 2. P selected only
Notice that Product E has no sales in JAN and FEB and shows up blank on the chart. Yes, this is desired.
Desired result: D and/or E selected (start the bar chart on the green arrow)
Desired behavior if D (and/or E) is selected, and P is selected, but SEP through DEC are deselected (meaning, when there is no data for D and/or E)
Dummy data (note that YRMO is intended to be TEXT type):
| PRODUCT | COMPANY | COMPANY_ORDER | YEAR | MONTH | YRMO | MONTH_TEXT | SALES |
| P | B | 2 | 2024 | 5 | 202405 | MAY | |
| P | A | 1 | 2024 | 5 | 202405 | MAY | 18 |
| P | B | 2 | 2024 | 6 | 202406 | JUN | |
| P | A | 1 | 2024 | 6 | 202406 | JUN | 16 |
| P | B | 2 | 2024 | 7 | 202407 | JUL | |
| P | A | 1 | 2024 | 7 | 202407 | JUL | 12 |
| P | B | 2 | 2024 | 8 | 202408 | AUG | |
| P | A | 1 | 2024 | 8 | 202408 | AUG | 16 |
| D | B | 2 | 2024 | 9 | 202409 | SEP | 2 |
| D | A | 1 | 2024 | 9 | 202409 | SEP | 14 |
| E | B | 2 | 2024 | 9 | 202409 | SEP | |
| E | A | 1 | 2024 | 9 | 202409 | SEP | 2 |
| P | B | 2 | 2024 | 9 | 202409 | SEP | |
| P | A | 1 | 2024 | 9 | 202409 | SEP | 12 |
| D | B | 2 | 2024 | 10 | 202410 | OCT | 4 |
| D | A | 1 | 2024 | 10 | 202410 | OCT | 16 |
| E | B | 2 | 2024 | 10 | 202410 | OCT | |
| E | A | 1 | 2024 | 10 | 202410 | OCT | 8 |
| P | B | 2 | 2024 | 10 | 202410 | OCT | |
| P | A | 1 | 2024 | 10 | 202410 | OCT | 16 |
| D | B | 2 | 2024 | 11 | 202411 | NOV | |
| D | A | 1 | 2024 | 11 | 202411 | NOV | 2 |
| E | B | 2 | 2024 | 11 | 202411 | NOV | |
| E | A | 1 | 2024 | 11 | 202411 | NOV | 8 |
| P | B | 2 | 2024 | 11 | 202411 | NOV | |
| P | A | 1 | 2024 | 11 | 202411 | NOV | 14 |
| D | B | 2 | 2024 | 12 | 202412 | DEC | 4 |
| D | A | 1 | 2024 | 12 | 202412 | DEC | 6 |
| E | B | 2 | 2024 | 12 | 202412 | DEC | |
| E | A | 1 | 2024 | 12 | 202412 | DEC | 6 |
| P | B | 2 | 2024 | 12 | 202412 | DEC | |
| P | A | 1 | 2024 | 12 | 202412 | DEC | 6 |
| D | B | 2 | 2025 | 1 | 202501 | JAN | 8 |
| D | A | 1 | 2025 | 1 | 202501 | JAN | 6 |
| E | B | 2 | 2025 | 1 | 202501 | JAN | |
| E | A | 1 | 2025 | 1 | 202501 | JAN | |
| P | B | 2 | 2025 | 1 | 202501 | JAN | |
| P | A | 1 | 2025 | 1 | 202501 | JAN | 12 |
| D | B | 2 | 2025 | 2 | 202502 | FEB | 2 |
| D | A | 1 | 2025 | 2 | 202502 | FEB | 6 |
| E | B | 2 | 2025 | 2 | 202502 | FEB | |
| E | A | 1 | 2025 | 2 | 202502 | FEB | |
| P | B | 2 | 2025 | 2 | 202502 | FEB | |
| P | A | 1 | 2025 | 2 | 202502 | FEB | 24 |
| D | B | 2 | 2025 | 3 | 202503 | MAR | 6 |
| D | A | 1 | 2025 | 3 | 202503 | MAR | 18 |
| E | B | 2 | 2025 | 3 | 202503 | MAR | 2 |
| E | A | 1 | 2025 | 3 | 202503 | MAR | 4 |
| P | B | 2 | 2025 | 3 | 202503 | MAR | |
| P | A | 1 | 2025 | 3 | 202503 | MAR | 6 |
| D | B | 2 | 2025 | 4 | 202504 | APR | 4 |
| D | A | 1 | 2025 | 4 | 202504 | APR | 18 |
| E | B | 2 | 2025 | 4 | 202504 | APR | |
| E | A | 1 | 2025 | 4 | 202504 | APR | 4 |
| P | B | 2 | 2025 | 4 | 202504 | APR | |
| P | A | 1 | 2025 | 4 | 202504 | APR | 12 |
| D | B | 2 | 2025 | 5 | 202505 | MAY | |
| D | A | 1 | 2025 | 5 | 202505 | MAY | 6 |
| E | B | 2 | 2025 | 5 | 202505 | MAY | |
| E | A | 1 | 2025 | 5 | 202505 | MAY | 2 |
| P | B | 2 | 2025 | 5 | 202505 | MAY | |
| P | A | 1 | 2025 | 5 | 202505 | MAY | 4 |
Adding: To be clear, if the user filters out 2024 and selects only 2025 (for example), then the bar chart start should behave like normal. The chart should start on whatever date is the oldest given by the filter, unless it includes the above stated start dates. If the start dates are within the filtered date range, then the start of the bar chart should behave as described above.
I re-did my approach. I made one measure to indicate whether or not E or D is selected by counting the E/D rows by using SUMX. Then I wrote another measure based on the first one.
EDRows = SUMX(ALLSELECTED('Table'), IF('Table'[PRODUCT] in {"E", "D"}, 1, 0)) sum_salesx = var _ed = [EDRows2] > 0 var _eddate = IF(_ed, "202409", "202405") return SUMX('Table',IF('Table'[YRMO] >= _eddate, 'Table'[SALES] + 0, BLANK()))I put sum_salesx as the Y-axis in the bar chart and it seems to work fine. And in the X-axis, "Show items with no data" is not selected.
5 Replies
- user01
Resolver I
I re-did my approach. I made one measure to indicate whether or not E or D is selected by counting the E/D rows by using SUMX. Then I wrote another measure based on the first one.
EDRows = SUMX(ALLSELECTED('Table'), IF('Table'[PRODUCT] in {"E", "D"}, 1, 0)) sum_salesx = var _ed = [EDRows2] > 0 var _eddate = IF(_ed, "202409", "202405") return SUMX('Table',IF('Table'[YRMO] >= _eddate, 'Table'[SALES] + 0, BLANK()))I put sum_salesx as the Y-axis in the bar chart and it seems to work fine. And in the X-axis, "Show items with no data" is not selected.
- GrowthNatives
Super User
Hi user01 ,
I understand you are having an issue with dynamically starting date based on filter for bar chart. You are on the right track, if you change your formula it should work.Sum_Sales =
var _edselected = IF("P" in ALLSELECTED('Table'[PRODUCT]),
FALSE(),TRUE())
var _eddate = IF(_edselected, "202409", "202405")
RETURN CALCULATE(SUMX('Table', 'Table'[SALES]), 'Table'[YRMO] >= INT(_eddate)+0)Here is the .pbix file Dynamic Starting Date
⭐Hope this solution helps you make the most of Power BI! If it did, click 'Mark as Solution' to help others find the right answers.
💡Found it helpful? Show some love with kudos 👍 as your support keeps our community thriving!
🚀Let’s keep building smarter, data-driven solutions together! 🚀 [Explore more]- user01
Resolver I
Hi GrowthNatives,
Thank you, but this did not work. If (example) D and P are selected (your third image), the chart should start in SEP (max of (D start date, P start date)). Or, if D or E selected (P selected or unselected), the bar chart should start in SEP.
Desired result (example, D and E and P selected)
- GrowthNatives
Super User
Hi user01 ,
You are right, I missed that part. The formula below should work for all the cases.Sum_Sales =
var _edselected = IF("E" in ALLSELECTED('Table'[PRODUCT]) || "D" in ALLSELECTED('Table'[PRODUCT]) ,
TRUE(),FALSE())
var _eddate = IF(_edselected, "202409", "202405")
RETURN CALCULATE(SUMX('Table', 'Table'[SALES]), 'Table'[YRMO] >= INT(_eddate)+0)⭐Hope this solution helps you make the most of Power BI! If it did, click 'Mark as Solution' to help others find the right answers.
💡Found it helpful? Show some love with kudos 👍 as your support keeps our community thriving!
🚀Let’s keep building smarter, data-driven solutions together! 🚀 [Explore More]