0

Dynamic formulation for full year / rolling 13 months

Hello everyone,

We currently use the following formulations to ultimately display the rolling last 13 months ("Rollierend") or the entire year ("Gesamtjahr") via a button in the dashboard. 

- Dashboard - 

 - Formula rolling 13 months - 

 

- Formula entire year - 

Since the formulation only refers to the current year and will have to be changed at the turn of the year 2026, for example, it is still very static.
It would also be better if the formula were based on the calendar used in the dashboard, so that the rolling 13 months could be displayed based on the current month ("Jahr" 2025, "Monat" April), but also further back in the past.

Does anyone have any ideas and can help me optimise the mdx formula?

Thanks and best regards,
Jona

10 replies

null
    • Redeemed from the Dark Side (Power BI)
    • Obee1
    • 6 days ago
    • Reported - view

    There is a much simpler way of doing this - but it requires some formula logic. You need to use a parameter and a list and it should (hopefully) solve all the issues.

    1. First, build a new Parameter (from the Formulate section).
      1. Start by choosing your "model" parameter, then choose your cube.
      2. Select 'data driven' and then pick your months hierarchy ("Jan 2025"), and then select all your months in the list. These will be the starting months that you pick from later.
      3. For 'default', choose single select drop down, and 'last' value. 
      4. Save it.
    2. Next, build a new List (also from Formulate section). We'll start with point and click and then change to script mode.
      1. Select your cube again.
      2. Choose a "Range List" from the "add" menu.
      3. In the bottom panel, for the first 'pick a member', select your months attribute. In the elements list, click the 'FX" button and choose the parameter you created above.
      4. Repeat this for the second 'pick a member'.
      5. Now change to script mode. You see syntax like this. It says pick a range of elements starting with the parameter (the strange ID) and ending in the parameter.
      6. {([Date].[Month].@[5d9bcfea-4d70-48f0-a6b6-e348d737d3d8]
        :[Date].[Month].@[5d9bcfea-4d70-48f0-a6b6-e348d737d3d8])}
      7.  Of course, this is not what we want. We want to pick from the date and range it back 13 months. so adjust the formula manually with the lag function as follows:
      8. {([Date].[Month].@[5d9bcfea-4d70-48f0-a6b6-e348d737d3d8].lag(12)
        :[Date].[Month].@[5d9bcfea-4d70-48f0-a6b6-e348d737d3d8])}

    That's it.  Go into Discover and use your list. When you do, you'll automatically be presented with the parameter drop down. Pick your end month from the drop down, and the query will show you a list of months going back 13 periods. 

      • Jona_Loneke
      • 6 days ago
      • Reported - view

       thank you very much. The implementation option you described works as desired.
      How can I adjust the formula to also display the entire year as an option instead of the rolling months?

      Kind Regards, 
      Jona

      • NPANS
      • 6 days ago
      • Reported - view

       Not sure I understand. You mean to roll up all the 13 months into a single value?

    • Jona_Loneke
    • 6 days ago
    • Reported - view

    No I mean to show the total year (Jan - Dez) in the lines when selecting a random Month of the year instead of the rolling 13 months.
     

      • NPANS
      • 6 days ago
      • Reported - view

       Still not sure what you mean. In your chart, the darker blue line is the amount per month. The lighter blue line (which is flat), is the total for the year, shown as the same number in every month? And you want the lighter blue to be the total of year in which the month falls in? 

      • Lead Consultant Advanced Analytics
      • Michael_Daun
      • 6 days ago
      • Reported - view

      , I guess I know what Jona wants to achieve: The two buttons above his charts work as a toggle button. When you click Gesamtjahr/Total Year the category axis should show Jan - Dec of the current/selected year. And when you click Rollierend/Rolling the category axis should show the last 13 months before the current/selected month.

      So, he wants a dynamic visualization, I suppose, based on the status of the toggle button. And that's what he presented to us in his initial post - but hardcoded for the current year (cf. his screenshots with the formulas). 

      I am not sure if the following post may help - it is about dynamic dimensions as well: https://community.pyramidanalytics.com/t/p8y69k0/tip-of-the-day-dynamic-dimensions-and-measures
      Perhaps you can combine these ideas with the implementation approach  has given...

      Best regards,
      Michael 

    • Jona_Loneke
    • 5 days ago
    • Reported - view

    Thank you  for helping out. That's exactly what I want to achieve with the additional Formula. 

    Unfortunately, the post doesn't really help me with this specific issue.
    So it would be great if someone could take another look at it so that I can replace the static code for the entire year with a dynamic formula.

    The static formula currently has to be adjusted and replaced at the beginning of each new year. This involves additional work that we could potentially save ourselves.

     

    Thank you in advance!

      • NPANS
      • 5 days ago
      • Reported - view

       Here is the solution. 

      1. Create a new global parameter.
        1. I am using a "number" parameter with 2 discrete values: "Full Year" with value 0, "Rolling13" with value 1.
        2. Save
      2. Create a new List "B" like before. Go straight into script mode and add the following logic.
        1. The list logic is a little complicated, but it says: (from the inside - out) find me a non-empty year from the year hierarchy members, based on my original month parameter and the sales metric. So if I choose "Jun 2024", it finds me the non-empty year that aligns with this, which should be "2024".
        2. We then take the year from the previous step, and put it inside another non-empty function, to find all the months that are not empty for that year. So now, the year 2024, will find all the months Jan-2024 to Dec-2024.
        3. Putting it altogether, our parameter will find all its sibling periods for a given year. without any complicated counting or hand-written elements.
        4. nonempty([Date].[Month].[Month].members,
           nonempty({[Date].[Year].[Year].members},
               ( [Date].[Month].@[5d9bcfea-4d70-48f0-a6b6-e348d737d3d8] ,measures.sales)
            )
          )

           

      3. Create another list "C", to glue it altogether, in script mode.
        1. We use an IIF statement to check the parameter value. If 0, use the new list "B". Otherwise, use our original list "A" from above. In building your own syntax, find the parameters and lists in the metadata trees below the editor and drag and drop them into the editor (or double click to insert at cursor).
        2. iif([global].#[c1afc906-da28-4ff3-82ce-e9b9447af556]=0,
            [*77fbb048-5881-4dbf-99ca-22b6e16e0996*] ,
            [*6eeb183e-2a36-4532-a2b5-7173c2eddd8a*]
          )

           

      Drop list C into Discover, you'll have 2 switches: 1 to control the months, and 1 to control the listing logic. And your chart problem is solved.

      • Jona_Loneke
      • 5 days ago
      • Reported - view

       thank you very much for the detailed answer!

      I tried to recreate it but somehow it's not working at the moment.

       

      First I created the parameter with 2 discret values ("Gesamtjahr" = Full Year with value 0, "Rollierend" = Rolling13 with value 1)

       

      After that I created the List "B". 
       

       

      There I selected the item-ID "@[26b441b6-ab75-4e54-8944-dae29f9fbb88]" from the parameter that I have already created for the rolling 13 months "Parameter Monate".

       

      The measure is called "Buchungsbetrag". The years hierarchy is as follows: [Kalender].[Jahre]

      However, in the reports we use the third level in the hierarchy in the rows (KJ Monat). For example, Jan - Dec 2025

       

       

      As it says the Formula is successfull at the moment. 

       

      In list C, I first use the globally created parameter. Then I use the list for the entire year and then for the rolling 13 months. Again, I use the item ID in each case.

      However, when I try to test the formula, I get the following error message

       

       

      Do you maybe know what the reason for the error is?

      Kind Regards,

      Jona

      • NPANS
      • 5 days ago
      • Reported - view

       It's hard to say what the error is. But List/set "B" is not correct. You are using months ("KJ Monat") in both nonempty functions. The inner one should be years, and the outer one months. 

      However, I now notice that you are using a regular hierarchy for dates - not 2 attributes. Which means the logic I gave you is not correct anyway - since I assumed it was 2 flat attributes for months and years. 

      So, instead, I would be using this logic for set B:

      DESCENDANTS(
        [Date].[Month Dates].@[434e8e89-c44a-47e9-869a-15b98d6b264c].parent.parent,
        [Date].[Month Dates].[Month]
      )
      

      This takes the month from the parameter; finds its parent's parent (the year); and then get that year's descendants at the "month" level. 

Content aside

  • Status Answered
  • 5 days agoLast active
  • 10Replies
  • 30Views
  • 4 Following