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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
marie_joy
Frequent Visitor

Get values from the previous row - STOCK ON HAND

Hi,

 

Sorry I've posted the same question before but maybe it was not well explained...

 

I'm building a Power BI report to track stock and inventory levels for various items across different locations. My data source is the item ledger entry table from Navision stored in SQL Server. This table captures item movements for each date and location.

I've imported the data using a SQL query: now I have a table where I have stock data for days where I had a movement.

 

Example:

Posting DateItem NoLocation CodeQuantity
2024-05-10 ITEM-ALOC-110
2024-05-11 ITEM-BLOC-25
2024-05-12ITEM-ALOC-1-2
2024-05-14ITEM-CLOC-215

 

Now I would like to have stock values for the previous days. This stock value is equal to the stock value from the previous day:

 

Expected results (0 are not necessary):

Posting DateItem NoLocation CodeQuantity
2024-05-10ITEM-ALOC-110
2024-05-11ITEM-ALOC-110
2024-05-11ITEM-BLOC-25
2024-05-12ITEM-ALOC-1-2
2024-05-12ITEM-BLOC-25
2024-05-13ITEM-ALOC-1-2
2024-05-13ITEM-BLOC-25
2024-05-14ITEM-ALOC-1-2
2024-05-14ITEM-BLOC-25
2024-05-14ITEM-CLOC-215

 

I've tried to do that with a SQL query and a cross join but I could not manage it. 

Is there a way to do that with a DAX function?

 

Thanks!

 

1 ACCEPTED SOLUTION

@marie_joy - Ok thanks, that data refit has helped.

 

You can do this with the data all in one table, but the DAX is not the easiest to get your head around, so I will do my best to explain below the code. Create a measure with the following DAX and change the table names:

 

VAR _item =
    SELECTEDVALUE ( 'Table (2)'[Item No] )
VAR _loc =
    SELECTEDVALUE ( 'Table (2)'[Location Code] )
VAR current_date =
    SELECTEDVALUE ( 'Table (2)'[Posting Date] )
VAR _table =
    CALCULATETABLE (
        ADDCOLUMNS ( 'Table (2)', "max_date", MAX ( 'Table (2)'[Posting Date] ) ),
        'Table (2)'[Item No] = _item,
        'Table (2)'[Location Code] = _loc,
        'Table (2)'[Posting Date] < current_date
    )
VAR prev_date =
    MAXX ( _table, [max_date] )
RETURN
    CALCULATE (
        SUM ( 'Table (2)'[Quantity] ),
        FILTER (
            ALL ( 'Table (2)' ),
            'Table (2)'[Posting Date] = prev_date
                && 'Table (2)'[Item No] = _item
                && 'Table (2)'[Location Code] = _loc
        )
    )

 

VAR creates Variables for the necessary data to be stored and referenced back to. 

SELECTEDVALUE takes the items from the current filter context. 

CALCULATETABLE creates a virtual table with the filters I have set after ADDCOLUMNS which is used to find the maximum date that is before the current date. 

MAXX allows me to iterate through this virtual table and find that date

CALCULATE allows me to change the current filter context

ALL removes any filter context and the filters afterwards replace it. 

 

As the screenshot below shows, the last column shows the previous days Quantity Value. 

 

If this works for you, please mark this as the solution. 

mark_endicott_0-1715862996942.png

 

View solution in original post

4 REPLIES 4
mark_endicott
Super User
Super User

If you are asking for a count of stock values from previous days, I think the EARLIER function may be able to help you. 

 

But due to the samples of data that you have supplied being difficult to interpret, I cannot give you the exact DAX - you will need to make sure you are using filters on Item and Location code for example.  

 

Have a review of this article and it may help. 

https://dax.guide/earlier/

Hi, 

thank you for your reply. 

I don't want to count values, I want the value from the previous day. I've tried to update the question for the tables but for some reasons, the table is not in a good format. Sorry for that.

@marie_joy - Ok thanks, that data refit has helped.

 

You can do this with the data all in one table, but the DAX is not the easiest to get your head around, so I will do my best to explain below the code. Create a measure with the following DAX and change the table names:

 

VAR _item =
    SELECTEDVALUE ( 'Table (2)'[Item No] )
VAR _loc =
    SELECTEDVALUE ( 'Table (2)'[Location Code] )
VAR current_date =
    SELECTEDVALUE ( 'Table (2)'[Posting Date] )
VAR _table =
    CALCULATETABLE (
        ADDCOLUMNS ( 'Table (2)', "max_date", MAX ( 'Table (2)'[Posting Date] ) ),
        'Table (2)'[Item No] = _item,
        'Table (2)'[Location Code] = _loc,
        'Table (2)'[Posting Date] < current_date
    )
VAR prev_date =
    MAXX ( _table, [max_date] )
RETURN
    CALCULATE (
        SUM ( 'Table (2)'[Quantity] ),
        FILTER (
            ALL ( 'Table (2)' ),
            'Table (2)'[Posting Date] = prev_date
                && 'Table (2)'[Item No] = _item
                && 'Table (2)'[Location Code] = _loc
        )
    )

 

VAR creates Variables for the necessary data to be stored and referenced back to. 

SELECTEDVALUE takes the items from the current filter context. 

CALCULATETABLE creates a virtual table with the filters I have set after ADDCOLUMNS which is used to find the maximum date that is before the current date. 

MAXX allows me to iterate through this virtual table and find that date

CALCULATE allows me to change the current filter context

ALL removes any filter context and the filters afterwards replace it. 

 

As the screenshot below shows, the last column shows the previous days Quantity Value. 

 

If this works for you, please mark this as the solution. 

mark_endicott_0-1715862996942.png

 

@marie_joy - Can you please confirm if this solution has worked for you?

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.