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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Alph4
Frequent Visitor

Moth to day - Adjustes

I would like to adjust this measure so that when it identifies the highest accumulated value, it does not replicate that value until the end of the 'dCalendario' table.

 

File exemple: 
https://www.dropbox.com/s/ifm5octlpkq5wng/Forecast_Comunnity.pbix?dl=0

 

 

IF(
    ISFILTERED('dCalendario'[DataBase]),
    ERROR("erro"),
    CALCULATE(
        [Qty.Liabilities - Forecast],
        'dCalendario'[DataBase].[Date],
        ALL('dCalendario'[DataBase].[Mês]),
        'dCalendario'[DataBase].[Date] <= MAX('dCalendario'[DataBase].[Date])
    )
)

 

 

 


For example, when the measure identifies that the highest value is 32, it will only display values up to: 2023 November 32

Year Month FORECAST

2022April3
2022May4
2022June4
2022July5
2022August5
2022September5
2022October5
2022November5
2022December5
2023January5
2023February6
2023March9
2023April9
2023May16
2023June16
2023July19
2023August26
2023September30
2023October30
2023November32
2023December32
2024January32
2024February32
2024March32
2024April32
2024May32
2024June32
2024July32
2024August32
2024September32
2024October32
2024November32
2024December32

 @tamerj1  @Jihwan_Kim  @Martin_D 

 

 

 

1 ACCEPTED SOLUTION

This is the code that works as specified:

 

IF(
    ISFILTERED('dCalendario'[DataBase]),
    ERROR("erro"),
    VAR _DateValue =
        // Create a virtual table with a row per date and each cumulated forecast value,
        // to then pick the overall maximum forecast value,
        // to then pick the minimum date with this maximum forecast value,
        // to then return no more values after that date (i.e. any period, e.g. month, that consists only of dates after this date)
        CALCULATETABLE (
            ADDCOLUMNS (
                SUMMARIZECOLUMNS (
                    'tbl_tasks'[Valor]
                ),
                "@MonthlyValue",
                VAR _LastDate = CALCULATE ( MAX ( 'dCalendario'[DataBase].[Date] ) )
                RETURN
                CALCULATE (
                    [_Qty.baseline - Forecast],
                    ALLSELECTED('dCalendario'),
                    'dCalendario'[DataBase].[Date] <= _LastDate
                )
            ),
            ALLSELECTED ( 'dCalendario' )
        )
    // get the maximum forecast value
    VAR _MaxValue = MAXX ( _DateValue, [@MonthlyValue] )
    // get the first month with the maximum forecast value
    VAR _EarliestDateWithMaxValue = MINX ( FILTER ( _DateValue, [@MonthlyValue] = _MaxValue ), [Valor] )
    // get the first date of the selected period, e.g. month
    VAR _FirstDateInContext = MIN ( 'dCalendario'[DataBase].[Date] )
    RETURN
    IF (
        _FirstDateInContext <= _EarliestDateWithMaxValue,
        CALCULATE(
            [_Qty.baseline - Forecast],
            'dCalendario'[DataBase].[Date], // only include values from dates that exist in the fact table ([Valor] column) and in the date table, 
                                            // i.e. no values for before jan 2019 (date table), and no values for before nov 2022 (first month with date on or after jan 2019 with data)
            'dCalendario'[DataBase].[Date] <= MAX('dCalendario'[DataBase].[Date]),
            ALLSELECTED ( 'dCalendario' )
        )
    )
)

 

Thoughts:

  • If you load your own date table anyway, you can mark it as date table and turn off auto datetime. This makes the behavior of DAX code in the model easier to understand because no more hidden auto-date-tables impact the behavior. Be aware that if you change this after you have written your DAX measures you need to change the DAX measures, e.g. change 'Date'[Date].[Month] (referenece to the Month column of the auto-date-table which then no longer exists) to 'Date'[Month] (reference to the Month column of your date table).
  • Your date table starts 2019, your data starts 2015, and in the measure you make sure that only values from dates in the date table are included in the calcualtion. Best practice would be to load a complete date table, covering all your facts, write a universal measure, and then apply a date filter in the report as needed. Thus your peak curently shows 43, instead of 46 if all values since 2015 were included.
  • You could simplify this code if the following assumptions apply, which I'm just guessing:
    • The actual requirement is not to end showing data at the maximum cumulative value but at the last month with data for the measure. At least this is the way more popular requirement. Then calculating the date at which to stop showing data could be done with less code and would be faster. The measure now stops showing data at the peak, even if the cumulative value goes down afterwards (according to requirements).
    • Explicitly specifying that all values that go into the cumulative total are positive. Then it would also be clear that the last date with a non-zero value is also the last date at which to show data.

BR

Martin

github.pnglinkedin.png

View solution in original post

5 REPLIES 5
Martin_D
Super User
Super User

@Alph4 would you mind providing the file or an ananomyzed sample file?

https://www.dropbox.com/s/ifm5octlpkq5wng/Forecast_Comunnity.pbix?dl=0

Are there other way to share the file here ? 

This is the code that works as specified:

 

IF(
    ISFILTERED('dCalendario'[DataBase]),
    ERROR("erro"),
    VAR _DateValue =
        // Create a virtual table with a row per date and each cumulated forecast value,
        // to then pick the overall maximum forecast value,
        // to then pick the minimum date with this maximum forecast value,
        // to then return no more values after that date (i.e. any period, e.g. month, that consists only of dates after this date)
        CALCULATETABLE (
            ADDCOLUMNS (
                SUMMARIZECOLUMNS (
                    'tbl_tasks'[Valor]
                ),
                "@MonthlyValue",
                VAR _LastDate = CALCULATE ( MAX ( 'dCalendario'[DataBase].[Date] ) )
                RETURN
                CALCULATE (
                    [_Qty.baseline - Forecast],
                    ALLSELECTED('dCalendario'),
                    'dCalendario'[DataBase].[Date] <= _LastDate
                )
            ),
            ALLSELECTED ( 'dCalendario' )
        )
    // get the maximum forecast value
    VAR _MaxValue = MAXX ( _DateValue, [@MonthlyValue] )
    // get the first month with the maximum forecast value
    VAR _EarliestDateWithMaxValue = MINX ( FILTER ( _DateValue, [@MonthlyValue] = _MaxValue ), [Valor] )
    // get the first date of the selected period, e.g. month
    VAR _FirstDateInContext = MIN ( 'dCalendario'[DataBase].[Date] )
    RETURN
    IF (
        _FirstDateInContext <= _EarliestDateWithMaxValue,
        CALCULATE(
            [_Qty.baseline - Forecast],
            'dCalendario'[DataBase].[Date], // only include values from dates that exist in the fact table ([Valor] column) and in the date table, 
                                            // i.e. no values for before jan 2019 (date table), and no values for before nov 2022 (first month with date on or after jan 2019 with data)
            'dCalendario'[DataBase].[Date] <= MAX('dCalendario'[DataBase].[Date]),
            ALLSELECTED ( 'dCalendario' )
        )
    )
)

 

Thoughts:

  • If you load your own date table anyway, you can mark it as date table and turn off auto datetime. This makes the behavior of DAX code in the model easier to understand because no more hidden auto-date-tables impact the behavior. Be aware that if you change this after you have written your DAX measures you need to change the DAX measures, e.g. change 'Date'[Date].[Month] (referenece to the Month column of the auto-date-table which then no longer exists) to 'Date'[Month] (reference to the Month column of your date table).
  • Your date table starts 2019, your data starts 2015, and in the measure you make sure that only values from dates in the date table are included in the calcualtion. Best practice would be to load a complete date table, covering all your facts, write a universal measure, and then apply a date filter in the report as needed. Thus your peak curently shows 43, instead of 46 if all values since 2015 were included.
  • You could simplify this code if the following assumptions apply, which I'm just guessing:
    • The actual requirement is not to end showing data at the maximum cumulative value but at the last month with data for the measure. At least this is the way more popular requirement. Then calculating the date at which to stop showing data could be done with less code and would be faster. The measure now stops showing data at the peak, even if the cumulative value goes down afterwards (according to requirements).
    • Explicitly specifying that all values that go into the cumulative total are positive. Then it would also be clear that the last date with a non-zero value is also the last date at which to show data.

BR

Martin

github.pnglinkedin.png

Thank you, in the given example, it worked perfectly, but in the actual case, there are still some errors,. My objective is to create an S-curve for this table, an S-curve for the planned values based on the "End" column, an S-curve for the projected values based on the "End" column, and a curve for the actual values up to the current day. Could you suggest another approach based on this model that I provided, keeping in mind that this model only has one project? In the original database, I have a column that identifies the projects, and the idea is that when applying a filter on the project name, it would display the curves for each project. 

With some shortcomings, you can create an s-curve from your data like this. You can load a Projekt column, then the solution works for multiple projects as well.
S-curveS-curve

See attached file for solution.
The two major shortcoming are:

  1. With your data you cannot distinguish between tasks of different size. I'd suggest to load absolute data in hours or currency, then you can calculate true progress, not just based on task count.
  2. With your data your s-curve makes significant jumps, because you have only on value per task (and attribute) and not one value per task and day/week/month (whatever you want to report on) traking the progress periodically. I'd suggest to load a snapshot of all tasks and their progress every day, week, or month, depending on your reporting needs, per project as long as the project is in progress. Of course you need to collect this data first, this is not just a transformation of the data in the current file.

BR

Martin

 

github.pnglinkedin.png

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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