Forum Discussion

user01's avatar
user01
Icon for Resolver I rankResolver I
1 year ago
Solved

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:

  1. If the user selects Product D and/or Product E (Product P selected or deselected), the starting date must be September 2024.
  2. 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):

 

PRODUCTCOMPANYCOMPANY_ORDERYEARMONTHYRMOMONTH_TEXTSALES
PB220245202405MAY 
PA120245202405MAY18
PB220246202406JUN 
PA120246202406JUN16
PB220247202407JUL 
PA120247202407JUL12
PB220248202408AUG 
PA120248202408AUG16
DB220249202409SEP2
DA120249202409SEP14
EB220249202409SEP 
EA120249202409SEP2
PB220249202409SEP 
PA120249202409SEP12
DB2202410202410OCT4
DA1202410202410OCT16
EB2202410202410OCT 
EA1202410202410OCT8
PB2202410202410OCT 
PA1202410202410OCT16
DB2202411202411NOV 
DA1202411202411NOV2
EB2202411202411NOV 
EA1202411202411NOV8
PB2202411202411NOV 
PA1202411202411NOV14
DB2202412202412DEC4
DA1202412202412DEC6
EB2202412202412DEC 
EA1202412202412DEC6
PB2202412202412DEC 
PA1202412202412DEC6
DB220251202501JAN8
DA120251202501JAN6
EB220251202501JAN 
EA120251202501JAN 
PB220251202501JAN 
PA120251202501JAN12
DB220252202502FEB2
DA120252202502FEB6
EB220252202502FEB 
EA120252202502FEB 
PB220252202502FEB 
PA120252202502FEB24
DB220253202503MAR6
DA120253202503MAR18
EB220253202503MAR2
EA120253202503MAR4
PB220253202503MAR 
PA120253202503MAR6
DB220254202504APR4
DA120254202504APR18
EB220254202504APR 
EA120254202504APR4
PB220254202504APR 
PA120254202504APR12
DB220255202505MAY 
DA120255202505MAY6
EB220255202505MAY 
EA120255202505MAY2
PB220255202505MAY 
PA120255202505MAY4

 

 

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

  • 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.

  • 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's avatar
      user01
      Icon for Resolver I rankResolver 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's avatar
        GrowthNatives
        Icon for Super User rankSuper 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]