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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Previous invoice number (Looking for more efficient calculation)

Hi,

 

I have a sales table with around 1 mio. records, where each row is one item row on each individuel invoice.

The invoices are created in a numerical order, so the newest will always have the highest number.

 

For filtering purposes - thus calculating in a calculated column - I'm trying to retrieve each clients previous invoice number to the current invoice row.

 

For this purpose I've so far tried two different approaches, which both work on a small sample size, but as soon as I implement the same measure on the main 1 mio. row table, all memory is used up, Power BI is stuck on "Making changes" or simply just crashes.

 

Method 1 using a variable:

Previous Invoice V1 =
VAR
PreviousInvoice = Sales[InvoiceID]
RETURN
CALCULATE( MAX(Sales[InvoiceID] ) ;
            VALUES( Sales[ClientID] ) ;
            Filter( All(Sales); Sales[InvoiceID] < PreviousInvoice
            )
            

Method 2 using EARLIER:

Previous Invoice v2 = 
 CALCULATE( MAX(Sales[InvoiceID]) ;
     VALUES(Sales[InvoiceID]);
     FILTER(ALL(Sales); Sales[InvoiceID] < EARLIER(Sales[InvoiceID]) )
 )

I thought about trying to achieve the same in Power Query, but so far haven't come up with a solution.

Anyone care to suggest a different approach that would avoid the memory issue?

1 ACCEPTED SOLUTION

@Anonymous

 

Hi, lets try with Query Editor:

 

Step 1: Sort your Data 

 

Step1.png

 

Step 2: Add a Index Column

 

Step2.png

 

Step 3: Add a Custom Column with the PrevIndex (Index - 1)

 

Step3.png

 

Step 4: Merge the Query Using Index and PrevIndex Columns

 

Step4.png

 

Step 5: Expand the Custom Column (only the Invoice ID) ***The image has a wrong column

 

Step5.png

 

Step 6: The result is:

 

Step 6.png

 

Step 7: Close & Apply

 

Step 8: Go to Data Tab

 

I don't review why happen this but the Null PreInvoiceID change to the last InvoiceID of the ClientID. So need a little flix.

 

Step 8.png

 

Step 9: Add a calculated column to evaluate this condition.

 

Step 9.png

 

Ready. I hope helps in your case.

 

Regards

 

Victor

Lima - Peru




Lima - Peru

View solution in original post

8 REPLIES 8
Zubair_Muhammad
Community Champion
Community Champion

Hi @Anonymous

 

See if this is slightly faster

 

Previous Invoice V1 =
VAR PreviousInvoice = Sales[InvoiceID]
RETURN
    CALCULATE (
        MAX ( Sales[InvoiceID] ),
        FILTER (
            ALLEXCEPT ( Sales, Sales[ClientID] ),
            Sales[InvoiceID] < PreviousInvoice
        )
    )

Regards
Zubair

Please try my custom visuals
Anonymous
Not applicable

I'm not noticing the same memory consumption using ALLEXPECT in both methods (consumption is just flat), but PBI seems stuck on "working on it". - Left it for 5 min before shutting the application down (if it actuallly works, then it's to inefficient anyways).

 


Thought about that instead of passing through the entire Sales table, I could maybe use a SUMMARIZECOLUMNS based on InvoiceID and Client ID, filtered by ClientID for that row, so PBI would need to pass through fever records?

 

Is that possible in a calculated column and how so?

@Anonymous

 

How about a MEASURE instead of Calculated Column?

 

 


Regards
Zubair

Please try my custom visuals
Anonymous
Not applicable

I need to use the output for filtering which is why I've opted for a calculated column.

 

My goal is to find all the invoices that contain some specific items, and based on those invoice find each clients previous invoice from that.

 

My process should then be:

1. Find invoices that contains items x,y,z

2. Find each clients previous invoice before the invoice that contained x,y,z

3. Find the revenue of all previous invoices.

 

If you can achieve the same in a measure, then fine by me.

@Anonymous

 

Lets give it a try. It may make things faster since calculated columns consume memory and disk space

 

Try this MEASURE

 

Previous Invoice V1 =
VAR PreviousInvoice =
    SELECTEDVALUE ( Sales[InvoiceID] )
RETURN
    CALCULATE (
        MAX ( Sales[InvoiceID] ),
        FILTER (
            ALLEXCEPT ( Sales, Sales[ClientID] ),
            Sales[InvoiceID] < PreviousInvoice
        )
    )

 

 


Regards
Zubair

Please try my custom visuals
Anonymous
Not applicable

Unfortunatly that doesn't work.

 

Filtered out a single customer and couldn't get PowerBI to turn out a result.

 

Think I'm going to try with the summarizecolumns approach and see what happens.

@Anonymous

 

Hi, lets try with Query Editor:

 

Step 1: Sort your Data 

 

Step1.png

 

Step 2: Add a Index Column

 

Step2.png

 

Step 3: Add a Custom Column with the PrevIndex (Index - 1)

 

Step3.png

 

Step 4: Merge the Query Using Index and PrevIndex Columns

 

Step4.png

 

Step 5: Expand the Custom Column (only the Invoice ID) ***The image has a wrong column

 

Step5.png

 

Step 6: The result is:

 

Step 6.png

 

Step 7: Close & Apply

 

Step 8: Go to Data Tab

 

I don't review why happen this but the Null PreInvoiceID change to the last InvoiceID of the ClientID. So need a little flix.

 

Step 8.png

 

Step 9: Add a calculated column to evaluate this condition.

 

Step 9.png

 

Ready. I hope helps in your case.

 

Regards

 

Victor

Lima - Peru




Lima - Peru
Anonymous
Not applicable

Great contribution!

Going to try and implement it now.

 

Would you need to add a table buffer to ensure the sorting is maintained, or is that fixed through the merging?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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