Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

Rolling Stock Position With Gaps In Months

Hi

 

I've loaded Item Ledger Entries table from Business Central into Power BI.

I'd like to see my rolling stock position by month, my issue is that if there's a month with no stock change / no data, it will show as 0 when it should take the last available data point e.g. if as of February there was £1.3m of stock value and no data in March then show £1.3m.

 

I'm currently using the below measure:

 

Cumulative Inventory Value =
    CALCULATE(
        SUM('Item Ledger Entries'[costAmountActual]),
            FILTER(
                ALL('Item Ledger Entries'),
                    'Item Ledger Entries'[postingDate] <= MAX('Item Ledger Entries'[postingDate]
            )
        )
    )
 

 

 

  • Anonymous,

     

    This can be solved with a date table. There are various ways to create a date table--here's a DAX calculated table:

     

    Date = 
    ADDCOLUMNS (
        CALENDARAUTO (),
        "Month Start Date", DATE ( YEAR ( [Date] ), MONTH ( [Date] ), 1 )
    )

     

    Create a 1:* relationship between the date table and data (fact) table:

     

     

    Create measures:

     

    Sum of Stock Value = SUM ( 'Table'[Stock Value] )
    Running Total = 
    CALCULATE ( [Sum of Stock Value], FILTER ( ALLSELECTED ( 'Date' ), 'Date'[Date] <= MAX ( 'Date'[Date] ) ) )

     

    Result:

     

     

    Whenever possible, use dimension table fields in visuals (e.g., 'Date'[Month Start Date]).

8 Replies

  • Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

    Do not include sensitive information or anything not related to the issue or question.

    If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-p/963216

    Please show the expected outcome based on the sample data you provided.

    Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    I create a table as you mentioned.

    Then I create a measure and here is the DAX code.

    Measure = 
    VAR _Value =
        SUMX (
            FILTER (
                ALL ( 'Table' ),
                'Table'[Date] <= MAX ( 'Table'[Date] )
                    && 'Table'[Stock Value] > 0
            ),
            'Table'[Stock Value]
        )
    RETURN
        IF ( SUM ( 'Table'[Stock Value] ) <> 0, SUM ( 'Table'[Stock Value] ), _Value )

     

     

     

     

    Best Regards

    Yilong Zhou

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • Anonymous's avatar
      Anonymous
      Not applicable

      Appreciate the help. Unfortunately isn't the solution.

       

      Below is what I should expect to see.

       

       

      The issue is if there's absolutely no data for a specific month such as below (missing May) then it should take the previous months total

       

       

       

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous ,

        I changed my DAX code.

        Measure = 
        VAR _currentdate =
            MAX ( 'Table'[Date] )
        RETURN
            CALCULATE (
                SUM ( 'Table'[Stock Value] ),
                FILTER ( ALLSELECTED ( 'Table' ), 'Table'[Date] <= _currentdate )
            )

        Then I get what you want.

         

         

        Best Regards

        Yilong Zhou

        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Anonymous,

     

    This can be solved with a date table. There are various ways to create a date table--here's a DAX calculated table:

     

    Date = 
    ADDCOLUMNS (
        CALENDARAUTO (),
        "Month Start Date", DATE ( YEAR ( [Date] ), MONTH ( [Date] ), 1 )
    )

     

    Create a 1:* relationship between the date table and data (fact) table:

     

     

    Create measures:

     

    Sum of Stock Value = SUM ( 'Table'[Stock Value] )
    Running Total = 
    CALCULATE ( [Sum of Stock Value], FILTER ( ALLSELECTED ( 'Date' ), 'Date'[Date] <= MAX ( 'Date'[Date] ) ) )

     

    Result:

     

     

    Whenever possible, use dimension table fields in visuals (e.g., 'Date'[Month Start Date]).