Forum Discussion

JIRAMON's avatar
JIRAMON
Frequent Visitor
3 years ago
Solved

Create new table with: ADDCOLUMN, SELECTCOLUMN, FILTER

Hi all,

 

I have been trying without success to create a new table that 1st adds 2 columns from 2 related tables, then selects some of the columns from the resulting table, and then Filters. I explain the situation with examples:

 

Table 1 - Invoices

Invoice IDCustomer IDInvoice Date
35210001/01/2023
35310005/15/2023
35410102/01/2023
35510106/15/2023

 

Table 2 - Items

Item IDItem Name
200Chocolate
201Vanilla

 

 

Table 3 - Invoice lines

Invoice IDItem IDQuantitySalesAmountExtra column
35220010100Data-x
35220120200Data-y
353200550Data-z
35320110100Data-a
35420020200Data-b
35520030300Data-c

 

Relations

1 to many --> Invoices[Invoice ID] to 'Invoice lines'[Invoice ID]

1 to many --> Items[Item ID] to 'Invoice lines'[Item ID]

 

Expected result

What I need is a table that:

- 1st adds the columns [Customer ID] and [Item ID]

- 2nd removes [Extra column]

- 3rd filters the table to only include the latest invoice by customer (calculated eiather using [Invoice ID] or [Date])

 

Invoice IDCustomer IDInvoice DateItem IDItem NameQuantitySale Amount
35310005/15/2023200Chocolate550
35310005/15/2023201Vanilla10100
35510106/15/2023200Chocolate30300

 

Thank you,

JIRAMON

  • Latest Invoice.pbix

     

    Leverage physic relationships for better performance,

     

    Latest = 
    ADDCOLUMNS(
        CALCULATETABLE(
            'Invoice Lines',
            FILTER(
                Invoices,
                Invoices[Invoice Date]
                    = CALCULATE(
                        MAX( Invoices[Invoice Date] ),
                        ALLEXCEPT( Invoices, Invoices[Customer ID] )
                    )
            )
        ),
        "Cust ID", RELATED( Invoices[Customer ID] ),
        "Inv Date", RELATED( Invoices[Invoice Date] ),
        "Item Name", RELATED( Items[Item Name] )
    )

     

3 Replies

  • Hi,

    I am not sure if I understood your question correctly,  but pleas try something like below.

    Please check the below picture and the attached pbix file.

     

     

     

    Expected result table = 
    VAR _latestinvoicebycustomer =
        GROUPBY (
            Invoices,
            Invoices[Customer ID],
            "@lastestinvoice", MAXX ( CURRENTGROUP (), Invoices[Invoice ID] )
        )
    RETURN
        CALCULATETABLE (
            SUMMARIZE (
                'Invoice lines',
                Invoices[Invoice ID],
                Invoices[Customer ID],
                Invoices[Invoice Date],
                Items[Item ID],
                Items[Item Name],
                'Invoice lines'[Quantity],
                'Invoice lines'[SalesAmount]
            ),
            TREATAS (
                _latestinvoicebycustomer,
                Invoices[Customer ID],
                Invoices[Invoice ID]
            )
        )
  • Latest Invoice.pbix

     

    Leverage physic relationships for better performance,

     

    Latest = 
    ADDCOLUMNS(
        CALCULATETABLE(
            'Invoice Lines',
            FILTER(
                Invoices,
                Invoices[Invoice Date]
                    = CALCULATE(
                        MAX( Invoices[Invoice Date] ),
                        ALLEXCEPT( Invoices, Invoices[Customer ID] )
                    )
            )
        ),
        "Cust ID", RELATED( Invoices[Customer ID] ),
        "Inv Date", RELATED( Invoices[Invoice Date] ),
        "Item Name", RELATED( Items[Item Name] )
    )

     

  • JIRAMON's avatar
    JIRAMON
    Frequent Visitor

    Thank you both for the proposed solutions. Both worked great!