Forum Discussion

alops194's avatar
alops194
Frequent Visitor
2 months ago
Solved

dynamic x-axis based on filter

Hi everyone, let me explain my problem. I have this graph where the x-axis needs to change from month-year to week-year. So I created a parameter that changes the graph via a filter, but the real need is for the change to happen dynamically. Let me explain. By default, the graph should show the month-year on the x-axis, but when the week is filtered by the slicer above, the x-axis should change to show weeks-year.

I've already tried with a DAX measure with is filtered, but I haven't found a solution.

Has anyone else had a similar problem?
Thanks in advance for your help.

14 Replies

  • This can be achieved using Field Parameters in Power BI.

    Steps

    1. Go to Modeling → New Parameter → Fields.
    2. Add both fields:
      date[Month-Year]
      date[Week-Year]
      Power BI will generate a parameter like:
      Date Axis Parameter =
      {
      ("Month", NAMEOF('Date'[Month-Year]), 0),
      ("Week", NAMEOF('Date'[Week-Year]), 1)
      }
    3. Place the Date Axis Parameter on the chart's X-axis.

      By default, the parameter can show Month-Year, and users can switch to Week-Year using the parameter slicer.

      Important Limitation

      Currently, Power BI does not support changing the selected field parameter dynamically through DAX based on another slicer's selection. Measures can change values and formatting, but they cannot programmatically change the field used on an axis.

      If you need the switch to happen automatically when a week is selected, you'll need to use a workaround such as:

      • Two visuals (one with Month-Year and one with Week-Year) and show/hide them based on slicer selections, or
      • Bookmarks and buttons.

      Reference

      Microsoft Documentation – Field Parameters:
      Create field parameters in Power BI Desktop

  • Create a field parameter with month-year and week-year as the members, and use that on the axis of the chart.

    Create a measure like

    TopN Filter =
    VAR CurrentSelection =
        SELECTEDVALUE ( 'Parameter'[Parameter Order] ) 
    // Assume that 0 = Week Year, 1 = Month Year
    VAR WeekIsFiltered =
        CALCULATE (
            ISFILTERED ( 'Date'[Week Year] ),
            ALLSELECTED ( 'Date'[Week Year] )
        )
    VAR Result =
        SWITCH (
            TRUE (),
            CurrentSelection = 0 && WeekIsFiltered, 1,
            CurrentSelection = 1, 1,
            0
        )
    RETURN
        Result
    

    Apply that as a Top N filter on the field parameter in the chart visual. Set it to show only the top 1 items.

    • alops194's avatar
      alops194
      Frequent Visitor

      Thanks a lot, I'm close to the solution. I created the measure as you told me. I just changed 0 and 1 based on my parameters fields. The only thing I need to fix is ​​that when I filter the week, the change on the x-axis occurs correctly, but the label it displays is concatenated "month-year week-year" and this can also be seen from the graph title. How can I fix this?

       

      • johnt75's avatar
        johnt75
        Super User

        I think the easiest way to fix the x-axis would be to turn the x-axis title off, and you should be able to edit the chart title manually.

        Worth noting that when I've used this technique in the past I applied the Top N filter directly on the parameter rather than on the parameter order. I don't think that would make a difference, but it could be worth trying.

  • alops194  Hey,

    Create a Date Calandar using below dax.

    Calandar = 
    ADDCOLUMNS(CALENDAR(DATE(2010,1,1),DATE(2030,12,31)),
    "Month year", FORMAT([Date],"Mmm-YY"),
    "Month sort",FORMAT([Date],"YYYYmm"),
    "Week Year","Week " & WEEKNUM([Date], 2) & FORMAT([Date],"-YY")
    , "Week sort" , FORMAT([Date],"YYYY")& WEEKNUM([Date],2)
    )

    2 - Mark this new table date table 

     

    3 - Create relationship between Date table and Fact table .

    4) Create a Field parameter.

    5) Select your Month year and Week Year from Date table abd rename it as per your choice.

     

     

    7- Add it as slicer and drag it in your viz X axis  like below image

     

     

    😎 Then you can use above slicer to switch in between Month year and Week year as per choice

     

     

    9) Sort it as per your requirement and use Month sort and week sort column to sort asc or desc order.

     

     

    Thanks

    Harish M

    If this is your soultion then Kindly give Kudos and accept it as solution to help other community member.

     

    • alops194's avatar
      alops194
      Frequent Visitor

      Hi, thanks for your reply, but unfortunately this solution is not suitable for my needs. The change from month to week must be done dynamically, therefore without using a slicer.

  • v-achippa's avatar
    v-achippa
    Community Support

    Hi alops194,

     

    Thank you for reaching out to Microsoft Fabric Community.

     

    Thank you johnt75Fenil_ItaliyaHarishKM and kushanNa for the prompt response.

     

    As we haven’t heard back from you, we wanted to kindly follow up to check if the solution provided by the user's for the issue worked? or let us know if you need any further assistance.

     

    Thanks and regards,

    Anjan Kumar Chippa

    • v-achippa's avatar
      v-achippa
      Community Support

      Hi @alops194,

       

      We wanted to kindly follow up to check if the solution provided by the user's for the issue worked? or let us know if you need any further assistance.

       

      Thanks and regards,

      Anjan Kumar Chippa