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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Power_up_high
Frequent Visitor

Help to find next location for every row.

Hi,

This is my first post in this forum, I have only been a spectator before and founding answers by looking at questions posted by other members. However, now I am in a situation where I can´t really find a answer to my problem, so hope someone can help me.

I am trying to find the next location for a specific article that is moved around within our production.
The attached document shows a example of one article, sorted by date and the desired outcome for that article (see column D). As you can see sometimes the location is not changed so the next location is still the same as the current location. When there is no newer transaction I wish the outcome to be 0.

This seems simple, however my orginal data table contains several thousand articles and is not sorted by either date or article.
I have tried several different solutions trying to use CALCULATE, MIN AND FILTER. However I seem to be doing something wrong each time.

Hope someone can help with this problem. Please ask if there is any information missing. 

Thanks. 


Edit: Seems that the csv file is not supported? Here is the table: 

DATEArticleLocation(Desired outcome): Next location
2022-08-26A85790
2018-08-25A85228579
2018-07-21A85228522
2018-06-16A85228522
2018-06-07A-18522
2018-05-19A8522-1
2018-04-21A85228522
2018-04-21A85228522
2018-04-16A85228522
2018-03-24A85228522
2018-03-24A85228522
2018-02-24A85228522
2018-02-24A85228522
2018-01-29A85228522
2018-01-20A85228522
2017-12-09A85228522
2017-12-09A85228522
2017-11-18A85228522
2017-11-18A85228522
2017-11-09A86938522
2017-10-21A87438693
2017-10-20A87438743
2017-10-20A87438743
2017-10-20A85228743
2017-10-20A85228522



1 ACCEPTED SOLUTION

Hi @Power_up_high ,

 

If you need a calculated column, please try:

Next location =
VAR _a =
    MINX (
        FILTER (
            'Table',
            [Article] = EARLIER ( 'Table'[Article] )
                && [Serial_number] > EARLIER ( 'Table'[Serial_number] )
        ),
        [Serial_number]
    )
VAR _b =
    CALCULATE (
        MAX ( 'Table'[Location] ),
        FILTER (
            'Table',
            [Article] = EARLIER ( 'Table'[Article] )
                && [Serial_number] = _a
        )
    )
RETURN
    _b + 0

Output:

vjianbolimsft_0-1672276616865.png

If you need a measure, please try:

Measure =
VAR _a =
    MINX (
        FILTER (
            ALL ( 'Table' ),
            [Article] = MAX ( 'Table'[Article] )
                && [Serial_number] > MAX ( 'Table'[Serial_number] )
        ),
        [Serial_number]
    )
VAR _b =
    CALCULATE (
        MAX ( 'Table'[Location] ),
        FILTER (
            ALL ( 'Table' ),
            [Article] = MAX ( 'Table'[Article] )
                && [Serial_number] = _a
        )
    )
RETURN
    _b + 0

Output:

vjianbolimsft_1-1672276963581.png

Best Regards,

Jianbo Li

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

View solution in original post

13 REPLIES 13
v-jianboli-msft
Community Support
Community Support

Hi @Power_up_high ,

 

In your orginal table  article A has two unique locations (8522 and 8743) for the date 2017-10-20. 

Your orginal data table contains several thousand articles and is not sorted by either date or article. So how to determine which one is the nex location?

 

Best Regards,

Jianbo Li

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

Hi @v-jianboli-msft,

Thanks for taking your time to answer. With your example I see that I have thought about this the wrong way, It is not possible to determine this based on the values given in the example table. For example if there was with the date 2017-10-19 it wouldn't know which location to pick up from the rows with the date 2017-10-20. 

Based on your input I have asked for other data internally and I have been given a new column called serial_number. This column is a number for a transaction (a row) given to a row based on when the row was created, so it gives us a better order instead of date. There is no dupliacte serial numbers, however the table is not ordered by the serial_number (see new example for article A below). 

I tried to change @serpiva64 code suggestion so it looks at serial number instead of date but still returns the wrong location.

So now my table looks like this:

Serial_number               DATE                                                                               Article                            Location                              Next location (desired                                                                                                                                                                                                                     outcome)
292022-11-12A-10
282022-11-12B85790
42017-10-20B85228579
32017-10-20B85228522
22017-10-20B85228522
272022-11-12A8579-1
262018-08-25A85228579
252018-07-21A85228522
242018-06-16A85228522
232018-06-07A-18522
222018-05-19A8522-1
212018-04-21A85228522
202018-04-21A85228522
192018-04-16A85228522
182018-03-24A85228522
172018-03-24A85228522
162018-02-24A85228522
152018-02-24A85228522
142018-01-29A85228522
132018-01-20A85228522
122017-12-09A85228522
112017-12-09A85228522
102017-11-18A85228522
92017-11-18A85228522
82017-11-09A86938522
72017-10-21A87438693
62017-10-20A87438743
52017-10-20A87438743
42017-10-20A85228743
12017-10-20A85228522
 



Based on this, I am hoping that the next location for a article can be found based on the article name and the first serial number for that article after current row. So for example the next location for article A with serial number 27 should be -1 since this the locations for serial number 29, which is the first serial number for article A after serial number 27. 

Long post, but I hope this makes more sense with a new column. @v-jianboli-msft or @serpiva64 do you have any suggestions on a solution for this?

Thanks!


Hi,

This calculated column formula works

Next Location = COALESCE(LOOKUPVALUE(Data[Location],Data[Serial_number],CALCULATE(MIN(Data[Serial_number]),FILTER(data,Data[Article]=EARLIER(Data[Article])&&Data[Serial_number]>EARLIER(Data[Serial_number]))),Data[Article],Data[Article]),0)

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @Power_up_high ,

 

If you need a calculated column, please try:

Next location =
VAR _a =
    MINX (
        FILTER (
            'Table',
            [Article] = EARLIER ( 'Table'[Article] )
                && [Serial_number] > EARLIER ( 'Table'[Serial_number] )
        ),
        [Serial_number]
    )
VAR _b =
    CALCULATE (
        MAX ( 'Table'[Location] ),
        FILTER (
            'Table',
            [Article] = EARLIER ( 'Table'[Article] )
                && [Serial_number] = _a
        )
    )
RETURN
    _b + 0

Output:

vjianbolimsft_0-1672276616865.png

If you need a measure, please try:

Measure =
VAR _a =
    MINX (
        FILTER (
            ALL ( 'Table' ),
            [Article] = MAX ( 'Table'[Article] )
                && [Serial_number] > MAX ( 'Table'[Serial_number] )
        ),
        [Serial_number]
    )
VAR _b =
    CALCULATE (
        MAX ( 'Table'[Location] ),
        FILTER (
            ALL ( 'Table' ),
            [Article] = MAX ( 'Table'[Article] )
                && [Serial_number] = _a
        )
    )
RETURN
    _b + 0

Output:

vjianbolimsft_1-1672276963581.png

Best Regards,

Jianbo Li

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

Hi @Ashish_Mathur & @v-jianboli-msft 

Thanks for taking your time and giving suggestions on two different solutions.
@Ashish_Mathur : I tried the solution you provided, however when Power BI tries creating the column I receive the error that there is not enough memory to complete the action. The reason is probably because the table is very large, almost 27 million rows.

@v-jianboli-msft : I also tried your suggestions, and both the measure and the calculated columned worked. The measure seems to work better with regards to performance, the calculated column took a long time before showing a result. 
However I would like to have a calculated column. Do you have any suggestion on how to increase performance for your calculated column solution? I only have two other columns in the table I provided and that data are not relevant for this. Could I perhaps remove these in the calculation in some way? Or do you have any other suggestions?

Thanks for helping me to find a solution. 

serpiva64
Solution Sage
Solution Sage

Hi,

there isn't an attached file

Hi,

Sorry, I had some problems attaching the document. I have edited my first post showing the table. 

Thanks!

Hi,

You can try 

Column = LOOKUPVALUE(Locations[Location],Locations[DATE], OFFSET(1,ALLSELECTED(Locations[DATE]),ORDERBY(Locations[DATE]),,))

serpiva64_0-1671794968742.png

If this post is useful to help you to solve your issue consider giving the post a thumbs up and accepting it as a solution !

 

Hi,
Thanks for your response. The suggested solutions returns #Error on all rows when creating a new column and the message : A table with several values were supplied, where a single value was expected. 
Could the problem be because of some formatting? The locations and article column is in

decimal number. 

Thanks!

Please can you post your new column

Hi,

I copied your code. But here it is from my side: 

Next location = LOOKUPVALUE(LOCATIONS[LOCATION],LOCATIONS[DATE], OFFSET(1,ALLSELECTED(LOCATIONS[DATE]),ORDERBY(LOCATIONS[DATE]),,)).

Thanks!

Hi,

i get this error if i use that formula in a measure insted of a calculated column

serpiva64_0-1671807834472.png

 

Hi again,

Thanks for taking your time to response and helping me out.
I have tried your code with the table example I provided in my orginal post (just as you did) and it worked. 

However, it is still not working in my real table. Could one of the reasons for the error be:

- That the same article can have the same dates but different locations? For example, in my orginal table  article A has two unique locations (8522 and 8743) for the date 2017-10-20. 
- That different articles have the same dates and same or different locations?
- How does the code know which article to look at? As I wrote in my orginal post, the table consists of several thousand articles and not one article as I the example I provided.

I have tried myself to also make changes to your suggestions without success. For example I used the function: FIRSTNONBLANK instead of lookupvalue, but I only received the same value as the current row value for location.

Thanks for supporting me and hope there is someone out there who have a solution to this problem. 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.